class Tree { private float size; private float[] abberation; private Tree[] branches; private float[] leaf; private float[] wind; public Tree(float complexity, float size, float abb) { abberation = new float[2]; wind = new float[2]; abberation[0] = random(abb * 3.0) - abb * 1.5; abberation[1] = random(abb * 0.5); this.size = size; int nchildren; float spread = random(complexity); if (spread > 1.0) { nchildren = 2; } else if (spread > 0.5) { nchildren = 1; } else { nchildren = 0; } if (nchildren > 0) { branches = new Tree[nchildren]; for (int i=0; i < nchildren; i++) { branches[i] = new Tree(complexity - 1.0, size * 0.8, abb * 0.8); } } else { leaf = new float[2]; leaf[0] = 15.0 + random(5.0); leaf[1] = random(2*PI); } } public void setWind(float wx, float wy) { float isize = 0.1 / sqrt(size); this.wind[0] = wx * isize; this.wind[1] = wy * isize; if (branches != null) { for (int i=0; i < branches.length; i++) { branches[i].setWind(wx, wy); } } } public void draw(float t) { stroke(40,40,0); strokeWeight(20.0 * size); float dx = 100.0 * abberation[0] + sin(t) * wind[0]; float dy = 100.0 * (-size / 2.0 + abberation[1]) + cos(t) * wind[1]; line(0.0, 0.0, dx, dy); if (branches != null) { for (int i=0; i < branches.length; i++) { pushMatrix(); translate(dx, dy); if (branches.length > 1) { rotate((0.5 - (float)i / (float)(branches.length-1)) * PI / 6.0); } branches[i].draw(t); popMatrix(); } } else { pushMatrix(); fill(10, 240, 5); strokeWeight(0.5); translate(dx, dy); rotate(leaf[1] + random(wind[0] / 10.0)); quad(0, 0, -leaf[0] * 0.2, leaf[0] * 0.5, 0, leaf[0], leaf[0] * 0.2, leaf[0] * 0.5); popMatrix(); } } } Tree tree; float t; void setup() { size(500,500); tree = new Tree(9.0, 1.0, 0.3); tree.setWind(3.0, 1.0); } void draw() { background(255); translate(250,500); tree.draw(millis() / 1000.0); }