Method: Recursion

So what does a fern and a cocoa ad have in common?

Self-Similarity

Fractals

L-Systems

L-System Tree, Daniel Shiffman, p5.js

Mandatory Tree Exercise

int maxDepth=10; 
int maxBranches=6;
float branchLength=200; 
int branchCount=0;

void setup()
{
  size(900,900);
  smooth();
  noLoop(); 
}

void draw()
{
  background(255);  
  strokeWeight(10);
  translate(width/2,height); // start at the lower center

  branch(0);

}

void branch(int depth)
{ branchCount++;
    if (depth < maxDepth) 
    {
      stroke((depth)*16+0); // stroke color > starting with black 
      line(0,0,0,-branchLength); // Coordinate system is upside down
      int b=int(random(1,maxBranches));
      for (int i=0;i<b;i++)
      {
        pushMatrix();
          translate(0,-branchLength);
          rotate(random(-PI/4,PI/4));
          scale(random(0.6,0.9));   // branches get smaller
          branch(depth + 1);
        popMatrix();
      }
    }
    println(branchCount);
}

void mouseClicked()
{
  redraw();
}