Object-Oriented Fractal Trees

On Thursday, Apr. 7, we did this illustration of making fractals object-oriented.

Implementation

// This code illustrates a fully object oriented approach to building fractal trees.
// It also illustrates heavy use of pushMatrix, translate, rotate, and popMatrix.
// We made various embellishments during class (such as starting with five trees
// instead of one, and making the growth more organic). The next embellishment
// would be to have the simulation terminate after some reasonable amount of growth,
// because it is going to turn into such a tangle of branches, each represented by
// an object, that it will bog the computer down after 20 or so branchings.

static final int branchesCount = 3;
static final float childAngleSeparation = radians(30);
static final float maturity = 60.0;

class Branch {
  float length;
  ArrayList<Branch> branches;

  Branch(float length_) {
    length = length_;
    branches = new ArrayList<Branch>();
  }

  void draw() {
    // draw ourselves
    line(0, 0, 0, length);
    // draw our children
    pushMatrix();
    translate(0, length);
    rectMode(CENTER);
    rect(0, 0, 5, 5);
    float childAngle = -1.0 * childAngleSeparation;
    for (Branch child : branches) {
      pushMatrix();
      rotate(childAngle);
      child.draw();
      popMatrix();
      childAngle += childAngleSeparation;
    }
    popMatrix();
    // we need to draw our children
  }

  void grow() {
    float newLength = length + 0.25;

    if (newLength > maturity) {
      // resize our children
      for (Branch child : branches) {
        child.grow();
      }
    } else if (newLength == maturity) {
      for (int i = 0; i < branchesCount; ++i) {
        branches.add(new Branch(0));
      }
      length = newLength;
    } else {
      length = newLength;
    }
  }
}

ArrayList<Branch> trees = new ArrayList<Branch>();

void setup() {
  size(600, 400);
  for (int i = 0; i < 5; ++i) {
    trees.add(new Branch(0.25));
  }
}

void draw() {
  background(255);
  for (int i = 0; i < 5; ++i) {
    pushMatrix();
    translate((i + 1) * 100, 400);
    rotate(radians(180));
    trees.get(i).draw();
    popMatrix();
    trees.get(i).grow();
  }
}