-
Notifications
You must be signed in to change notification settings - Fork 6
/
growFractalTree.js
44 lines (41 loc) · 1.03 KB
/
growFractalTree.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
var tree = [];
var treeGeneration = 0;
var leaves = [];
function setup() {
createCanvas(400, 400);
var initalPointx = createVector(width/2, height);
var initalPointy = createVector(width/2, height-100);
var treeRoot = new Branch(initalPointx, initalPointy);
tree[0] = treeRoot;
}
function draw() {
background(50);
for (var i = 0; i < tree.length; i++) {
tree[i].show();
}
for (var i = 0; i < leaves.length; i++) {
fill(255, 0, 100);
noStroke();
ellipse(leaves[i].x, leaves[i].y, 10, 10);
leaves[i].y += random(0, 5);
}
}
function mousePressed() {
for (var i = tree.length-1; i >= 0; i--) {
if(!tree[i].finished) {
var {right, left} = tree[i].branches();
tree.push(right);
tree.push(left);
}
tree[i].finished = true;
}
treeGeneration++;
if(treeGeneration === 5 || treeGeneration === 10 || treeGeneration === 20) {
for (var i = 0; i < tree.length; i++) {
if(!tree[i].finished) {
var leaf = tree[i].end.copy();
leaves.push(leaf);
}
}
}
}