// Tree sim class Pt { float x, y; Pt(float nx, float ny) { x = nx; y = ny; } } class Branch { float squigBias, lengthBias, direction, fade; Pt points[]; Branch branches[]; Branch(Branch parent, int limit) { createBranch(parent); if (limit < 8) createSubBranches(limit); } void createBranch(Branch parent) { points = new Pt[3]; float dirChange; if (parent != null) { // Change little things for the branches squigBias = random(parent.squigBias * .75, parent.squigBias * 1.1); lengthBias = random(parent.lengthBias * .45, parent.lengthBias*.90); dirChange = random(-squigBias, squigBias); direction = parent.direction + dirChange; points[0] = parent.points[points.length-1]; fade = random(parent.fade*.6, parent.fade); } else { // Basic parameters for the trunk squigBias = 55; lengthBias = 120; direction = -90.0f; dirChange = 0; points[0] = new Pt(width/2, height); fade = 1.0f; } // End point float tX = (lengthBias * cos(radians(direction))) + points[0].x; float tY = (lengthBias * sin(radians(direction))) + points[0].y; points[points.length-1] = new Pt(tX, tY); // Mid point (with a little bend) float dX = (points[0].x + points[points.length-1].x) / 2; float dY = (points[0].y + points[points.length-1].y) / 2; points[1] = new Pt(dX + random(lengthBias * .3) - lengthBias * .15, dY + random(lengthBias * .3) - lengthBias * .15); } void createSubBranches(int limit) { branches = new Branch[4]; for (int i=0; i < branches.length; i++) branches[i] = new Branch(this, limit+1); } void drawBranch() { push(); translate(points[0].x, points[0].y); stroke(255 - 255*fade); beginShape(LINE_STRIP); bezierVertex(0, 0); bezierVertex(points[1].x - points[0].x, points[1].y - points[0].y); bezierVertex(points[2].x - points[0].x, points[2].y - points[0].y); bezierVertex(points[2].x - points[0].x, points[2].y - points[0].y); endShape(); pop(); if (branches != null) for (int i=0; i < branches.length; i++) branches[i].drawBranch(); } } Branch root; void setup() { push(); size (600, 600); root = new Branch(null, 0); } void loop() { Branch current = root; background(255); current.drawBranch(); delay(10); } void mousePressed() { root = new Branch(null, 0); }