/* The RecursiveTree is a fairly simple class that wraps a series of recursive calls to draw a tree like structure. */ public class RecursiveTree { private float startLength; private float theta; private float minLength; public RecursiveTree() { // create default values for everything startLength = 60.0f; theta = radians(45.0f); minLength = 2.0f; } public RecursiveTree(float l_, float t_) { // store our parameters startLength = l_; theta = radians(t_); minLength = 2.0f; } public void useLength(float l_) { startLength = l_; } public void useTheta(float t_) { theta = radians(t_); } public void setMinLength(float m_) { minLength = m_; } /* Our simulate method is a little different than usual to account for our recursive drawing technique. There isn't any need for an 'update' method, because we redraw the whole recursive tree each frame. The simulate method will draw the first part of the tree, and then pass off the remaining drawing steps to the render() method, which will call itself recursively until the branches are smaller than our minLength. */ public void simulate() { // move into position to start drawing translate(width / 2, height); // draw the first branch line(0, 0, 0, -60); // move to the end of the line translate(0, -60); // start branching render(startLength); } public void render(float l_) { // scale our branch size l_ = l_ * 0.66f; // only draw if our branches are long enough if (l_ > minLength) { // store our position so we can come back to it pushMatrix(); // rotate to draw our branch rotate(theta); // draw the branch line(0, 0, 0, -l_); // translate to the end of the line translate(0, -l_); // draw more branches render(l_); // go back to the start of our branch popMatrix(); // draw the other branch // store position pushMatrix(); // position ourselves rotate(-theta); // draw line(0, 0, 0, -l_); // translate to end of branch translate(0, -l_); // create more branches render(l_); // reset position popMatrix(); } } }