classes "Vormcirkel" en "Ster" met voorbeeld. terug naar de inleiding
De classes staan onder de schets. Ga voor het ruitjespapier naar de class "Ruitjes"
De schets maakt gebruik van objecten van de class "VormCirkel" en de class "Ster"
VormCirkel vorm0;
Ruitjes ruitjespap;
Ster ster1;
Ster ster2;
void setup() {
//fullScreen();
size(960, 540);
float x = width/2;
float y = height/2;
float n = height/20;
//Argumenten van de VormCirkel constructor
//1) x, 2) y, 3) begingstraal, 4) eindgrootte, 5) diameter,
//6) snelh, 7) aantal vormen, 8) kleur
//constructor 1 2 3 4 5 6 7 8
vorm0 = new VormCirkel(x, y, -n*2, 4*n, n*3, 1, 8, color(37,200,214,100));
ruitjespap = new Ruitjes(n,x,y);
//argumenten Ster constructor 1) x positie, 2) y positie, 3) binnenstraal,
//4) buitenstraal, 5) einstraal buitencirkel 6) snelh, 7) aantal punten 8) color
//constructor 1, 2, 3, 4, 5, 6 7 8
ster1 = new Ster(x, y, n, 2*n, 8*n, 1, 4, color(255, 255, 0,100));
ster2 = new Ster(x, y, n, 8*n, -8*n, 1, 6, color(100, 220, 129, 100));
}
void draw() {
background(#E6FBFF);
vorm0.displayE ();
vorm0.variatieKG();
ster1.display ();
ster1.variatieKG();
ster2.display ();
ster2.variatieGK();
// ruitjespap.display();
}
void keyPressed() {
if (key == 's') {
noLoop();
}
if (key == 'r') {
loop();
}
}
de class "VormCirkel"
argumenten van de constructor
1) en 2) x,y positie middelpunt van de cirkel, 3) beginstraal, 4) eindstraal, 5) diameter van de vorm, 6) snelheid, 7) aantal vormen 8) kleur
class VormCirkel {
//vormen die in een cirkel zijn gegroepeerd.
float x; float y; float begin; float eind; float diam;
float snelh; int nvormen; color c;
float beginConst;
//argumenten van de constructor
//1) x, 2) y, 3) beginstraal, 4) eindstraal, 5) diameter,
//6) snelh, 7) aantal vormen 8) kleur
// 1 2 3 4 5
VormCirkel(float x_, float y_, float begin_, float eind_, float diam_,
// 6 7 8
float snelh_, int nvormen_, color c_) {
x = x_; y = y_; begin = begin_; eind = eind_; diam = diam_;
snelh = snelh_; nvormen = nvormen_; c = c_;
beginConst = begin;
}
void displayE() {
float vormen = TWO_PI/nvormen;
for (float a = 0; a < TWO_PI; a += vormen) {
float sx = x + cos(a) * begin;
float sy = y + sin(a) * begin;
fill(c);
ellipse(sx, sy, diam, diam);
}
}
void variatieKG() {
begin = begin - snelh;
if ((begin > eind) || (begin < beginConst))
{
snelh = snelh * -1;
}
}
}
de class "Ster"
argumenten van de ster constructor
1 en 2) x,y positie van het middelpunt, 3) binnenstraal, 4) buitenstraal, 5) eindstraal buitencirkel, 6) snelheid, 7) aantal punten, 8) kleur
class Ster {
float x; float y;
float radius1; float radius2;
int np;
float eindB;
float snelh;
color c;
float beginConst;
//argumenten ster constructor 1) x positie, 2) y positie, 3) binnenstraal,
// 4) buitenstraal, 5) einstraal buitencirkel 6) snelh, 7) aantal punten 8) color
// 1, 2, 3, 4, 5, 6 7 8
Ster(float x_,float y_,float radius1_,float radius2_,float eindB_,float snelh_,int np_,color c_){
x = x_;
y = y_;
radius1 = radius1_;
radius2 = radius2_;
c = c_;
np = np_;
eindB = eindB_;
snelh = snelh_;
//radius2 is de buitenstraal en het begin van de variatieKG
beginConst = radius2;
}
void display() {
float hoek = TWO_PI / np;
float halfHoek = hoek/2.0;
fill(c);
stroke(0);
beginShape();
for (float a = 0; a < TWO_PI; a += hoek) {
float sx = x + cos(a) * radius2;
float sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a+halfHoek) * beginConst;
sy = y + sin(a+halfHoek) * beginConst;
vertex(sx, sy);
}
endShape(CLOSE);
}
//radius2 is de buitenstraal van de ster die varieert van klein naar groot
void variatieKG() {
radius2 = radius2 - snelh;
if ((radius2 > eindB) || (radius2 < beginConst))
{
snelh = snelh * -1;
}
}
//nu is de buitenstraal kleiner dan de binnenstraal
void variatieGK() {
radius2 = radius2 - snelh;
if ((radius2 < eindB) || (radius2 > beginConst))
{
snelh = snelh * -1;
}
}
}