/* PYTHAGORAS TREE - ICOSOLES and NO SCALING Jeff Thompson Fall 2011 Another version of the classic fractal - this time the squares aren't scaled smaller. Instead the size is kept the same throughout and the angle is changed to 60º. Click-and-drag to change the rotation angle, transforming from a single square to a blooming tree to various tiled patterns. www.jeffreythompson.org */ float boxSize = 15; // size of box (starting size, in this case) int steps = 10; // number of steps (since traditional example uses size of box) float rotL = -60; // rotation angle L float rotR = 60; // rotation angle R float twist; // twist the tree's angle using the mouse void setup() { size(500, 500); smooth(); noStroke(); rectMode(CENTER); } void draw() { // clear background background(255); // update twist if mouse is pressed if (mousePressed) { twist = map(mouseX, 0, width, -150, 210); // scale to window } else { twist = 0; // reset when the mouse is released } // first box translate(width/2, height/2); // starting location fill(0); rect(0, 0, boxSize, boxSize); // initial box // fractalize! branch(steps); // draw branches } void branch(float s) { // current step (decrease to 0) s--; // if we haven't gone through all steps, iterate if (s > 0) { // change color each step, from (0,120,250) > (15,130,0) int r = int(map(s, steps,0, 0,15)); int g = int(map(s, steps,0, 120,130)); int b = int(map(s, steps,0, 250,0)); // rotate left pushMatrix(); // local transformations translate(-boxSize/2, -boxSize/2); // move up to L corner rotate(radians(rotL-twist)); // rotate 45º translate(boxSize/2, -boxSize/2); // move to lower corner of new box fill(r,g,b, 100); rect(0, 0, boxSize, boxSize); // draw the rectangle branch(s); // recursively call the function - makes the fractal! popMatrix(); // rotate right pushMatrix(); // ditto right translate(boxSize/2, -boxSize/2); rotate(radians(rotR+twist)); translate(-boxSize/2, -boxSize/2); fill(r,g,b, 100); rect(0, 0, boxSize, boxSize); branch(s); popMatrix(); } } void keyPressed() { // if spacebar pressed, print the twist angle if (key == 32) { // (for debugging and hacking) println("Twist: " + twist); } }