-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
63 lines (54 loc) · 1.07 KB
/
node.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
58
59
60
61
62
63
class Node {
constructor({
id,
x,
y,
z,
i,
j,
c,
}) {
this.id = id
this.pos = createVector(x, y, z)
this.children = {}
this.childrenOrder = []
this.i = i
this.j = j
this.c = color(c)
}
get numOfChildren() {
return Object.keys(this.children).length
}
get x() {
return this.pos.x
}
get y() {
return this.pos.y
}
get z() {
return this.pos.z
}
get mergedColor() {
let baseColor = color(this.c)
for (const cid of this.childrenOrder) {
const { node, strength } = this.children[cid]
baseColor = lerpColor(baseColor, node.c, strength)
}
return baseColor
}
addChild(node, depth, strength) {
if (this.children[node.id]) return
this.children[node.id] = { depth, node, strength }
this.childrenOrder.push(node.id)
}
getDepth(other) {
const dx = abs(this.i - other.i)
const dy = abs(this.j - other.j)
return max(dx, dy)
}
sortChildren() {
this.childrenOrder.sort(
(a, b) => this.children[a].depth - this.children[b].depth
)
}
}