/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.xXHNmM-VuMn/rev.124
*
* authors:
* J.R.
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// This sketch builds on a prior work, "JRs Tree", created by J.R.
// http://studio.sketchpad.cc/sp/pad/view/ro.9tnmdQ$VNqRiB/rev.1191
// This sketch builds on a prior work, "Untitled Sketch", created by J.R. Logan
// http://sketchpad.cc/sp/pad/view/ro.tN9WBjP2GFN/rev.75
// All Examples Written by Casey Reas and Ben Fry
// unless otherwise stated.
float theta;
void setup() {
size(400,400);
frameRate(30);
}
int bloom = random(75,150);
int treesize = random(75,100);
int trunkthickness = treesize/4;
int branchthickness = trunkthickness*.6;
void draw() {
// Sky
background(192,192,255);
// Ground
color c1 = color(204, 153, 0);
noStroke();
fill(c1);
rect(0, height-25, width, 25);
// Sets tree color
stroke(160,120,80);
// Sets angle of branches
float a = (bloom / (float) width) * 90f;
// the bloom variable used to be mouseX to get it to be interactive
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2,height);
// Draw a line 60 pixels
strokeWeight(trunkthickness);
line(0,0,0,-mouseY);
// Move to the end of that line
translate(0,-mouseY);
// Start the recursive branching!
branch(treesize);
}
void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66f;
strokeWeight(branchthickness);
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
scale(mouseX*.004);
rotate(theta); // Rotate by theta
if (h < 5) { stroke(80,120,10); }
line(0,0,0,-h); // Draw the branch
translate(0,-h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
strokeWeight(1);
popMatrix();
// Whenever we get back here, we "pop" in order to restore the previous matrix state
// Resets to tree color
stroke(160,120,80);
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
scale(mouseX*.004);
rotate(-theta);
if (h < 7) { stroke(80,120,10); }
line(0,0,0,-h);
translate(0,-h);
branch(h);
popMatrix();
}
}