xxxxxxxxxx
66
//The following is recursive code which generates branches of lines with equal symmetry.
//Processing was used to generate this fractal, which was inspired by Daniel Shiffman,
// the main resource for this code. He has many tutorials for generating fractals
// on YouTube and in his guidebook titled, "The Nature of Code."
//Available online at: https://natureofcode.com/book/chapter-8-fractals/
let theta;
function setup() {
createCanvas(800, 400);
}
function draw() {
frameRate(30);
//Stylizing parameters
background(mouseY/1.57, 255, mouseX/3.14);
stroke(mouseX/3.14,0,mouseY/1.57);
// Angle 0 to 90 degrees based on the mouseY (up-down) position
let a = (mouseY*2 / width) * 90;
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2,height);
// Draw a line 120 pixels
line(0,0,0,-120);
// Move to the end of that line
translate(0,-120);
//Generate Branches
branch(122);
}
function branch(h) {
// Each branch will be 0.7 the size of the one before it
h *= 0.7;
// When the length of the branch is 2 pixels or less
if (h > 2) {
push(); //move matrix
//Uses positive theta
// Rotate by theta
rotate(theta); //Rotate right
// Draw the branch
line(0, 0, 0, -h);
// Move to the end of the branch
translate(0, -h);
branch(h);
pop();
//Uses negative theta
push();
rotate(-theta); // Rotate left
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
pop();
}
}