-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
61 lines (55 loc) · 1.45 KB
/
node.js
File metadata and controls
61 lines (55 loc) · 1.45 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
class Node {
constructor(i, j) {
this.i = i;
this.j = j;
this.f = 999999999;
this.g = 999999999;
this.h = undefined;
this.neighbors = [];
this.cameFrom = undefined;
this.blocked = random(1) < 0.3;
}
show(col) {
if (this.blocked) {
col = color(0);
}
fill(col);
noStroke();
// rect(this.i * w, this.j * h, w-1, h-1);
circle(this.i * w + w/2, this.j * h + h/2, w/2);
}
addNeighbors(grid) {
if (this.blocked) {
return;
}
let i = this.i;
let j = this.j;
if (i > 0) {
this.neighbors.push(grid[i - 1][j]);
if (j > 0) {
this.neighbors.push(grid[i - 1][j - 1]);
}
if (j < rows - 1) {
this.neighbors.push(grid[i - 1][j + 1]);
}
}
if (i < cols - 1) {
this.neighbors.push(grid[i + 1][j]);
if (j > 0) {
this.neighbors.push(grid[i + 1][j - 1]);
}
if (j < rows - 1) {
this.neighbors.push(grid[i + 1][j + 1]);
}
}
if (j > 0) {
this.neighbors.push(grid[i][j - 1]);
}
if (j < rows - 1) {
this.neighbors.push(grid[i][j + 1]);
}
}
setHeuristic(goalNode) {
this.h = dist(this.i, this.j, goalNode.i, goalNode.j);
}
}