Sketches after the Book Generative Art – Matt Pearson

Chapter 8.3 – Fractals – Exponential growth

[Variations]
After Changing Number of Children and Level

 

[Variations]
After Changing Color

 

int numChildren = 3;
int maxLevels = 4;

Branch trunk;//Instanz der Klasse Branch namens trunk initialisieren

void setup() {
  frameRate(24);
  size(550, 550);

  background(255);
  noFill();
  smooth();

    newTree();// Funktion newTree aufrufen
}

void newTree() {
  trunk = new Branch(1, 0, width/2, height/2);//Eigenschaften dem trunk zuweisen
  trunk.drawMe();
}

void draw(){
  background(255);
  trunk.updateMe(width/2,height/2);
  trunk.drawMe();
}

class Branch {

  float level, index;
  float x, y;
  float endx, endy;
  float strokeW, alph;
  float len, lenChange;
  float rot, rotChange;
  float colR, colG, colB;

  Branch [] children = new Branch[0];//children ist ein Array des Branch-Objektes

  Branch(float lev, float ind, float ex, float why) {//Eigenschaften von Branch-Objekt festlegen
    level = lev;
    index = ind;
    strokeW = (1/level) * 100;

    alph = 255 / level;
    len = (1/level) * random(200);
    rot = random(360);
    lenChange = random(10) – 5;
    rotChange = random(10) – 5;

    updateMe(ex, why);//endx und endy werden kreiert und zugewiesen

    if (level < maxLevels) {
      children = new Branch[numChildren];
      for (int x=0; x<numChildren; x++) {        
children[x] = new Branch(level+1, x, endx, endy);      
}  
  }  

  void updateMe(float ex, float why) { 
    x = ex;    
y = why;    
rot += rotChange;   
   if (rot > 360) { //dass es sich immer weiter dreht
      rot = 0;
    }
    else if (rot < 0) {
      rot = 360;
    }

    len -= lenChange;
    if (len < 0) {
      lenChange *= -1;    
}    
else if (len > 200) {
      lenChange *= -1;
    }
    colR = 255*(x/width);
    colG = 255*(y/height);
    colB = 255*((x+y)/(width+height));

    float radian = radians(rot);
    endx = x + (len * cos(radian));
    endy = y + (len * sin(radian));
    for (int i=0; i<children.length; i++) {
      children[i].updateMe(endx, endy);
    }
  }

  void drawMe() {
    strokeWeight(2);
    stroke(colR, colG, colB);
    fill(255, alph);
    line(x, y, endx, endy);
    ellipse(endx, endy, len/12, len/12);
    for (int i=0; i<children.length; i++) {
      children[i].drawMe();
    }
    // println(children.length);
  }
}