/** * Recursive Trees * by Rick Reed. * * Modified code by Daniel Shiffman. * Renders a simple tree-like structure via recursion. * Loop with changing tree parameters to generate variations. */ float theta; float length; int curx, cury; int xoffset; boolean toggle; float ma; float ml; float trunkScale; float lrscale, llscale; void setup() { size(640, 640); frameRate(12); curx = 1; cury = 1; smooth(); background(0); toggle = true; ma = random(800); ml = random(500); trunkScale = 50.0; lrscale = 0.6; llscale = 0.8; } void draw() { if (toggle){ fill(0,10); // use black with alpha 10 xoffset = 0; rectMode(CORNER); stroke(0,255); rect(0,0,width,height); stroke(random(100) + 155,random(100) + 155,0,255); // Let's pick an angle 0 to 90 degrees based on the mouse position curx = curx + 1; cury = cury + 1; if (curx >= 45) { curx = 0; ma = random(500) + 100; ml = random(400) + 100; lrscale = 0.6 + (random(100) / 500.0); llscale = 0.6 + (random(100) / 500.0); println(random(100) / 500.0); } if (cury >= 45) { cury = 0; } float a = ((sin(curx / 180.0 * TWO_PI) * ma) / width) * 90f; float l = ((sin(cury / 180.0 * TWO_PI) * ml) / height) * 150.0; // Convert it to radians theta = radians(a); length = l; // Start the tree from the bottom of the screen translate(width/2,height); // Draw a line 120 pixels line(0 + xoffset,0,0 + xoffset,-length); if (curx == 35) { strokeWeight(1); fill(255); stroke(255, 255, 255,100); quad((-4 * (length / trunkScale)) + xoffset, 0, (4 * (length / trunkScale)) + xoffset, 0, (2 * (length / trunkScale)) + xoffset, -length, (-2 * (length / trunkScale)) + xoffset, -length); fill(200); stroke(200, 200, 200,100); quad((0 * (length / trunkScale)) + xoffset, 0, (4 * (length / trunkScale)) + xoffset, 0, (2 * (length / trunkScale)) + xoffset, -length, (0 * (length / trunkScale)) + xoffset, -length); //line(0 + xoffset, 0, 0 + xoffset, -h); // Draw the branch } else { line(0 + xoffset , -length, 0 + xoffset, -length); // Draw the branch } // Move to the end of that line translate(0,-length); // Start the recursive branching! branch(length, 1, 1); } if (mousePressed){ toggle = !toggle; } } void branch(float h, int side, int pushCount) { if (pushCount <= 30) { // Each branch will be 2/3rds the size of the previous one // All recursive functions must have an exit condition!!!! // Here, ours is when the length of the branch is 2 pixels or less strokeWeight(2); if (h >= 3) { if(side == 1){ side = -1; } else { side = 1; } pushMatrix(); // Save the current state of transformation (i.e. where are we now) rotate(theta * side * 0.6 + random(0.1)); // Rotate by theta if (curx == 35) { strokeWeight(1); fill(255); stroke(255, 255, 255,100); quad((-2 * (h / trunkScale)) + xoffset, 0, (2 * (h / trunkScale)) + xoffset, 0, (1 * (h / trunkScale)) + xoffset, -h, (-1 * (h / trunkScale)) + xoffset, -h); fill(200); stroke(200, 200, 200,100); quad((0 * (h / trunkScale)) + xoffset, 0, (2 * (h / trunkScale)) + xoffset, 0, (1 * (h / trunkScale)) + xoffset, -h, (0 * (h / trunkScale)) + xoffset, -h); //line(0 + xoffset, 0, 0 + xoffset, -h); // Draw the branch } else { int xr = (int) random(10); strokeWeight((int) random(4)); line(xr + xoffset, -h, xr + xoffset, -h); // Draw the branch } translate(0, -h); // Move to the end of the branch branch(h * llscale, side, pushCount + 1); // Ok, now call myself to draw two new branches!! popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state pushMatrix(); rotate(-theta * side * 0.3 + random(0.1)); if (curx == 35) { strokeWeight(1); fill(255); stroke(255, 255, 255,100); quad((-2 * (h / trunkScale)) + xoffset, 0, (2 * (h / trunkScale)) + xoffset, 0, (1 * (h / trunkScale)) + xoffset, -h, (-1 * (h / trunkScale)) + xoffset, -h); fill(200); stroke(200, 200, 200,100); quad((0 * (h / trunkScale)) + xoffset, 0, (2 * (h / trunkScale)) + xoffset, 0, (1 * (h / trunkScale)) + xoffset, -h, (0 * (h / trunkScale)) + xoffset, -h); //line(0 + xoffset, 0, 0 + xoffset, -h); } else { int xr = (int) random(10); strokeWeight((int) random(4)); line(xr + xoffset, -h, xr + xoffset, -h); } translate(0, -h); branch(h * lrscale, side, pushCount + 1); popMatrix(); } } }