ster, veelhoek en ellipse vormen, een toepassing van polymorfisme                  naar de inleiding
 
 
De draaiende vormen in de schets op mijn open processing kanaal zijn uitgelijnd naar height. rotateX (toegepast op 2 draaiende sterren) werkt niet in open processing
 
 

Vormen[] vormen = new Vormen[100];
void setup() {  
 fullScreen(P3D); 
 //let op: length = 100, de index loopt van 0 t/m 99
 
//de cellen 0 t/m 24 bevatten veelhoeken 
 for(int i = 0; i < vormen.length/4; i++) {  
 vormen[i] = new Veelhoek(random(0, width), random(0, height), 50, 8, color(random(255), random(255), random(255), 100)); 
 println(vormen.length/4); 
 }
 
 // de cellen 25 t/m 49 bevatten in cirkels gegroepeerde ellipsen 
 for(int i = vormen.length/4; i < vormen.length/2; i++) {  
 vormen[i] = new Cirkels(random(0, width), random(0, height), 40, 40, 20, 8, color(random(255), random(255), random(255), 100)); 
 }
 
 //De cellen 50 t/m 94 van de array bevatten sterren  
 for (int i = vormen.length/2; i < vormen.length-7; i++ ) {  
 vormen[i] = new Ster(random(0, width), random(0, height), random(10, 100), 30, 20, color(random(255), random(255), random(255), 100)); 
 }  
 //Het initieeren van de draaiende vormen in cellen 93 t/m 99 
 vormen[vormen.length-1] = new Ster(0, 0, 300, 100, 20, color(255, 0, 0, 100)); 
 vormen[vormen.length-2] = new Veelhoek(0, 0, 500, 6, color(255, 255, 0, 100)); 
 vormen[vormen.length-3] = new Ster(0, 0, 300, 100, 20, color(0, 255, 0, 100)); 
 vormen[vormen.length-4] = new Ster(0, 0, 300, 100, 20, color(0, 255, 255, 100)); 
 vormen[vormen.length-5] = new Ster(0, 0, 300, 100, 20, color(0, 0, 255, 100)); 
 vormen[vormen.length-6] = new Ster(0, 0, 200, 500, 6, color(0, 0, 255, 100));
vormen[vormen.length-7] = new Cirkels(0, 0, 250, 50, 50, 35, color(266, 0, 255, 100)); 
}
void draw() { 
background(189, 237, 240); 
 //de max waarde van i = 92 dus cel 93 t/m 99 wordt niet getekend 
 for (int i = 0; i < vormen.length-7; i++ ) {  
 //Lijntjes vanuit het midden naar centrum van iedere vorm 
 //stroke(255, 255, 0);  
 //line(vormen[i].x, vormen[i].y, width/2, height/2); 
 vormen[i].display();
}
//De draaiende sterren in de 4 kwadranten 
 pushMatrix(); 
 //Een ster,in cel 99 draait linksom in het eerste kwadant 
 translate(width/4*1, height/4*1); 
 vormen[vormen.length-1].draaienXLi(); 
 vormen[vormen.length-1].display(); 
 popMatrix();
pushMatrix(); 
 //Een ster, in cel 97 draait rechtsom in het tweede kwadrant 
 translate(width/4*3, height/4*1);  
 vormen[vormen.length-3].draaienRe(); 
 vormen[vormen.length-3].display(); 
 popMatrix();
pushMatrix(); 
 //Een ster, in cel 96 draait in het derde kwadrant 
 translate(width/4*1, height/4*3);  
 vormen[vormen.length-4].draaienLi(); 
 vormen[vormen.length-4].display(); 
 popMatrix();
pushMatrix(); 
 //Een ster, in cel 95 draait in het vierde kwadrant 
 translate(width/4*3, height/4*3);  
 vormen[vormen.length-5].draaienXRe(); 
 vormen[vormen.length-5].display(); 
 popMatrix();
//De draaiende polygon, ster en ellipscirkel in het centrum 
 pushMatrix(); 
 //De polygon in cel 98 draait linksom in het centrum 
 translate(width/4*2, height/4*2);  
 vormen[vormen.length-2].draaienLi(); 
 vormen[vormen.length-2].display(); 
 popMatrix();
pushMatrix(); 
 //De Ster in cel 94 draait in het centrum 
 translate(width/4*2, height/4*2);  
 vormen[vormen.length-6].draaienRe(); 
 vormen[vormen.length-6].display(); 
 popMatrix();
pushMatrix(); 
 //De ellipsecirkel in cel 93 draait linksom in het centrum 
 translate(width/4*2, height/4*2);  
 vormen[vormen.length-7].draaienLi(); 
 vormen[vormen.length-7].display(); 
 popMatrix(); 
}

De superclass Vormen
 
 

class Vormen {  
 float x; float y;  
 float radius1;  
 int np; 
 color c; 
 float r; 
 Vormen(float x_, float y_, float radius1_, int np_, color c_) {  
 x = x_;  
 y = y_;  
 radius1 = radius1_;  
 np = np_; 
 c = c_; 
 }
void display() {  
 }
void draaienRe() {  
 r = r + 0.5;  
 rotate(radians(r));  
 if (r == 360) r = 0; 
 }  
  
 void draaienLi() {  
 r = r + 0.5;  
 rotate(-radians(r));  
 } 
  
 void draaienXLi() {  
 r = r + 0.5;  
 rotateX(-radians(r));  
 } 
  
 void draaienXRe() {  
 r = r + 0.5; 
 rotateX(radians(r));  
 } 
} 

 
de subclass Ster
 
 

class Ster extends Vormen {
//radius2 is de variabele die alleen voor de sterren worden gebruikt. 
 float radius2; 
Ster(float x_, float y_, float radius1_, float radius2_, int np_, color c_) {  
// x_, y_, radius1_, np_ en c_ worden via de constructor van de superclass "Vormen" global gemaakt

super(x_, y_, radius1_, np_, c_);  
 radius2 = radius2_; //De constructor van Ster heeft de extra variabele 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) * radius1; 
 sy = y + sin(a+halfHoek) * radius1; 
 vertex(sx, sy); 
 } 
 endShape(CLOSE); 
 } 
}
De subclass Veelhoek
 


class Veelhoek extends Vormen {
Veelhoek(float x_, float y_, float radius1_, int np_, color c_) {  
 // x_, y_, en radius1_ radius np_ aantal hoeken van de veelvorm 
 super(x_, y_, radius1_, np_, c_); 
 }
void display() { 
 float hoek = TWO_PI/np; 
 fill(c); 
 stroke(0); 
 beginShape(); 
 for (float a = 0; a < TWO_PI; a += hoek) { 
 float sx = x + cos(a) * radius1; 
 float sy = y + sin(a) * radius1; 
 vertex(sx, sy); 
 } 
 endShape(CLOSE); 
 } 
}
De subclass Cirkels
 

class Cirkels extends Vormen {  
  //b en h zijn variabelen die alleen voor de ellipsen worden gebruikt. 
  float h; 
  float b; 
 
  //Ellipsen in een cirkel gegroepeerd 
  Cirkels(float x_, float y_, float radius1_, float b_, float h_, int np_, color c_) {  
  //radius1_ is de straal van de cirkel, np_ is het aantal ellipsen 
    super(x_, y_, radius1_, np_, c_); 
    h = h_; 
    b = b_; 
  }  
 
  void display() { 
    float cirkels = TWO_PI/np; 
    for  (float a =  0; a < TWO_PI; a += cirkels) { 
      float sx = x + cos(a) * radius1; 
      float sy = y + sin(a) * radius1; 
      fill(c); 
      ellipse(sx, sy, b, h); 
    } 
  } 
}