vormen overlappen, diameter varieert met ster class       terug naar de inleiding
 
Aan de class zijn toegevoegd de methodes xLiRe() en yUpDown().

De rode cirkel1 beweegt zich van li naar re, door regel 40 in te schakelen beweegt cirkel1 zich schuin omhoog

onder de schets staan de class "DiamVar"    de class "Ster" en "DiamVar" staan onder deze schets      bekijk de sterren en cirkels

 
 

DiamVar cirkel1;   // is een rode cirkel die geel wordt
DiamVar cirkel2;   // is een groene cirkel
DiamVar rechth1;   // is een blauwe rechthoek
DiamVar cirkel3;   // is een lichtblauwe cirkel
DiamVar cirkel4;   // is een lichtgroene cirkel
Ster ster1;  
Ster ster2; 
 
void setup() {   
  size(960, 540); 
 // fullScreen();  
  float x = width/2;  
  float y =height/2;  
  float n = height/15;  
 
 //argumenten van de constructor: 1) x positie, 2) y positie, 3) kleinste diameter  
 //4) grootste diameter, 5) snelheid 6) color  
  
 //constructor         1        2     3     4    5           6    
 cirkel1 = new DiamVar(x,       y,     n,   4*n,  4.0, color(255, 20, 20, 100));   
 cirkel2 = new DiamVar(x,       y, 3.5*n,  10*n,  2,   color(0, 255, 50, 100));   
 cirkel3 = new DiamVar(x-4.5*n, y,     n,   7*n,  1.0, color(0, 200, 100, 100));   
 cirkel4 = new DiamVar(x+4.5*n, y,     n, 4.5*n,  0.5, color(25, 255, 20, 100)); 
 rechth1 = new DiamVar(x,       y, 3.5*n,   7*n,  0.5, color(23,229,228,100));  
 
//argumenten ster constructor 1) x positie, 2) y positie, 3) binnenstraal,   
//4) buitenstraal, 5) aantal punten 6) color  
 
//constructor     1,       2,       3,     4,    5,         6 
 ster1 = new Ster(x,       y,       0.1*n, 4*n,  8,  color(255, 0, 0,100));  
 ster2 = new Ster(x-4.5*n, y-2.5*n, 0.3*n, 4*n,  10, color(180, 23, 229, 100));  
}
 
void draw() {   
  background(#E6FBFF); 
  cirkel1.displayE();   
  cirkel1.variatieKG();  
  cirkel1.xLiRe(); 
 // cirkel1.yUpDown();
  
  cirkel2.displayE();   
  cirkel2.variatieKG(); 
  cirkel2.displayE();   
  cirkel2.variatieKG(); 
 
  cirkel3.displayE();   
  cirkel3.variatieKG(); 
  
  cirkel4.displayE();   
  cirkel4.variatieKG();
  
 if (cirkel1.overlappen(cirkel2)) {  
    cirkel1.c = color(255, 255, 0, 100);  
  }
   
 if (cirkel3.overlappen(cirkel2)) {  
    cirkel2.c = color(255, 255, 0, 100);  
    ster1.display();  
  }
 else
  {
    cirkel2.c = color(0, 255, 50, 100);  
  } 
 
 if (cirkel3.overlappen(cirkel1)) {  
    cirkel1.c = color(255, 20, 20, 100);  
    rechth1.displayR();  
    rechth1.variatieKG();  
    ster2.display();  
  }  
} 
 
void keyPressed() {   
   if (key == 's') {   
   noLoop();  
 }   
   if (key == 'r') {   
   loop();  
 }  
}

 

De class "DiamVar"  met toegevoegd de methodes xLiRe()  (regel 57) en yUpDown(); (regel 63)

Door aan de constructor ysnelhUp en xsnelhLi toe te voegen kan je de vormen onde verschillende hoeken laten bewegen je heb dan 3 snelheden
Nu zijn ysnelhUp en xsnelhLi gekoppeld aan snelh
 
 

class DiamVar {   
  float x;  float y;  float b;   
  float begin;  
  float eind;  
  color c;   
  float ysnelhUp; 
  float xsnelhLi;
  float snelh;
  float beginConst; 
  
  DiamVar(float x_, float y_, float begin_, float eind_, float snelh_, color c_) {   
    x = x_;   
    y = y_;   
    begin = begin_;   
    eind = eind_; 
    //ysnelhUp en xsnelhRe kunnen eventueel aan de constructor worden toegevoegd
    ysnelhUp = snelh_; 
    xsnelhLi  = snelh_; 
    snelh    = snelh_;  //is de snelh waarmee de cirkels groter en kleiner worden
    c = c_; 
   
    beginConst = begin; 
  } 
  
  boolean overlappen(DiamVar met) {  
    float d = dist (x, y, met.x, met.y) ;  
 //d  is de afstand tussen de middelpunten van de cirkels 
 if (d < begin/2 + met.begin/2) {  
 // begin en met.begin zijn de diameters van de cirkels 
     return true; 
 } else { 
     return false; 
   } 
 }
 
  void displayE() {  
    fill(c);   
    ellipse(x, y, begin, begin); 
  } 
  
  void displayR() {   
    rectMode(CENTER);   
    fill(c);   
    rect(x, y, begin, begin, begin/10); 
  } 
  
  void variatieKG() {  
    begin = begin + snelh; 
    //  || is een logische OR. De snelheid moet in twee situaties omdraaien
    if ((begin > eind) || (begin < beginConst)) 
    {  
      snelh = snelh * -1; 
    } 
  } 
  
  void xLiRe() {  
    x = x + xsnelhLi;  
    if ((x > width) || (x < 0)) {  
    xsnelhLi = xsnelhLi * -1; 
   }
 }
    void yUpDown() {
    y = y + ysnelhUp;  
    if ((y > height) || (y < 0)) {  
    ysnelhUp = ysnelhUp * -1; 
   } 
 } 
}  

de class  "Ster"

 

class Ster { 
 float x; float y;  
 float radius1; float radius2; 
 int np; color c;
//argumenten ster constructor 1) x positie, 2) y positie, 3) buitenstraal,   
//4) binnenstraal, 5) aantal punten 6) color  
 
//      1,        2,        3,              4,             5,        6 
 Ster(float x_, float y_, float radius1_, float radius2_, int np_,  color c_) {  
 x = x_; 
 y = y_; 
 radius1 = radius1_; 
 radius2 = radius2_; 
 c = c_; 
 np = np_; 
 }
 
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) * radius1;  
 float sy = y + sin(a) * radius1;  
 vertex(sx, sy);  
 sx = x + cos(a+halfHoek) * radius2;  
 sy = y + sin(a+halfHoek) * radius2;  
 vertex(sx, sy); 
 }  
 endShape(CLOSE); 
 } 
}