xxxxxxxxxx
50
// Fractal Tree
// Renders a static tree-like structure via recursion
// Angles and number of branches are consistent due to a fixed random seed
function setup() {
createCanvas(640, 240);
noLoop(); // Ensures the draw function runs only once
}
function draw() {
randomSeed(42); // Set a fixed seed for consistent randomness
background(255);
stroke(0);
push();
// Start the tree from the bottom of the screen
translate(width / 2, height);
strokeWeight(2);
// Start the recursive branching!
branch(80);
pop();
}
function branch(length) {
// Draw the actual branch
line(0, 0, 0, -length);
// Move along to end
translate(0, -length);
// Each branch will be 2/3rds the size of the previous one
length *= 0.67;
// All recursive functions must have an exit condition!
// Here, ours is when the length of the branch is 2 pixels or less
if (length > 2) {
// A random number of branches
let n = floor(random(1, 5));
for (let i = 0; i < n; i++) {
// Picking a random angle
let angle = random(-PI / 2, PI / 2);
push(); // Save the current state of transformation (i.e., where are we now)
rotate(angle); // Rotate by the angle
branch(length); // Call myself to branch again
pop(); // Restore the previous matrix state
}
}
}