-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
67 lines (65 loc) · 1.78 KB
/
node.js
File metadata and controls
67 lines (65 loc) · 1.78 KB
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
64
65
66
67
class Node {
constructor(x, y, r, num) {
this.x = x;
this.y = y;
this.r = r;
this.num = num;
}
show(col) {
push();
push();
noFill();
strokeWeight(3);
stroke(col);
circle(this.x, this.y, this.r);
pop();
fill(col);
textSize(20);
noStroke();
textAlign(CENTER, CENTER);
text(this.num, this.x, this.y);
pop();
}
mouseIn() {
return ((mouseX - this.x) ** 2 + (mouseY - this.y) ** 2 <= (this.r / 2) ** 2);
}
update() {
if (this.x + sz / 2 >= width) {
this.x = width - sz / 2;
}
if (this.x - sz / 2 <= 0) {
this.x = sz / 2;
}
if (this.y + sz / 2 >= height) {
this.y = height - sz / 2;
}
if (this.y - sz / 2 <= 0) {
this.y = sz / 2;
}
}
separate(close_node) {
if (dist(this.x, this.y, close_node.x, close_node.y) < 2.5 * sz) {
if (this.x <= close_node.x && close_node.y >= this.y) {
this.x--;
this.y--;
close_node.x++;
close_node.y++;
} else if (this.x >= close_node.x && close_node.y >= this.y) {
this.x++;
this.y--;
close_node.x--;
close_node.y++;
} else if (close_node.x <= this.x && this.y >= close_node.y) {
close_node.x--;
close_node.y--;
this.x++;
this.y++;
} else {
close_node.x++;
close_node.y--;
this.x--;
this.y++;
}
}
}
}