-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
57 lines (49 loc) · 1.05 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
49
50
51
52
53
54
55
56
57
let rectSize = 15
let startRow = 0
let root = null;
let offsetH = 10;
class Node{
constructor(x, y, size){
this.x = x
this.y = y
this.size = size
this.children = []
}
addNode(){
this.children.push(new Node(0, 0, rectSize))
}
draw(){
fill("red")
rect(this.x, this.y, this.size)
}
drawAllChild(){
let nChild = this.children.length;
for(let i = 0; i < nChild; ++i){
if(nChild > 0){
this.children[i].drawAllChild()
}
this.children[i].x = (width/(nChild + 1))*(i+1) - (this.size/2)
this.children[i].y = this.y + rectSize + offsetH
this.children[i].draw()
}
this.draw()
}
loadRecursive(n){
if(n > 1){
this.addNode()
this.children[this.children.length - 1].loadRecursive(n - 1)
this.loadRecursive(n - 1)
}else{
return
}
}
}
function setup() {
createCanvas(1000, 600);
root = new Node((width - rectSize)/2, startRow, rectSize);
root.loadRecursive(16)
}
function draw() {
background(0);
root.drawAllChild()
}