xxxxxxxxxx
43
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Recursive Tree
// Renders a simple tree-like structure via recursion
// Branching angle calculated as a function of horizontal mouse position
let angle;
function setup() {
createCanvas(640, 240);
background(255);
angle = PI/6+0.1;
// Start the tree from the bottom of the canvas
translate(width / 2, height);
stroke(0);
strokeWeight(2);
branch(80);
}
//{!1} Each branch now receives its length as an argument.
function branch(len) {
line(0, 0, 0, -len);
translate(0, -len);
//{!1} Each branch’s length shrinks by two-thirds.
len *= 0.67;
if (len > 16) {
push();
rotate(angle);
//{!1} Subsequent calls to branch() include the length argument.
branch(len);
pop();
push();
rotate(-angle);
branch(len);
pop();
}
}