-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
48 lines (42 loc) · 1.32 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let theta;
function setup() {
createCanvas(1830, 1000);
}
function draw() {
background(236, 111, 24);
frameRate(30);
stroke(000);
// Let's pick an angle 0 to 90 degrees based on the mouse position
let a = (mouseX / 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 500 pixels
line(0,0,0,-500);
// Move to the end of that line
translate(0,-500);
// Start the recursive branching!
branch(240);
}
function branch(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is π pixels or less
if (h > Math.PI) {
push(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
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!!
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
push();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
pop();
}
}