-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
173 lines (165 loc) · 3.82 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Spot {
constructor(i, j) {
this.i = i;
this.j = j;
this.f = 0;
this.g = 0;
this.h = 0;
this.x = this.i * w + w / 2;
this.y = this.j * h + h / 2;
this.neighbors = [];
this.prev = undefined;
this.wall = false;
if (random(1) < ObstacleRate) {
this.wall = true;
}
}
show(col) {
noStroke();
fill(col);
this.wall && fill(0);
ellipse(this.x, this.y, w / 2, h / 2);
}
addNeighbors(grid) {
let i = this.i;
let j = this.j;
i < cols - 1 && this.neighbors.push(grid[i + 1][j]);
i > 0 && this.neighbors.push(grid[i - 1][j]);
j < rows - 1 && this.neighbors.push(grid[i][j + 1]);
j > 0 && this.neighbors.push(grid[i][j - 1]);
i > 0 && j > 0 && this.neighbors.push(grid[i - 1][j - 1]);
i < cols - 1 && j > 0 && this.neighbors.push(grid[i + 1][j - 1]);
i > 0 && j < rows - 1 && this.neighbors.push(grid[i - 1][j + 1]);
i < cols - 1 && j < rows - 1 && this.neighbors.push(grid[i + 1][j + 1]);
}
}
function heuristic(a, b) {
return dist(a.i, a.j, b.i, b.j);
}
const ObstacleRate = 0.5;
let cols = 55,
rows = cols;
let grid = new Array(cols);
let openSet = [];
let closedSet = [];
let start, end, current;
let path = [];
let w, h;
let solution = true;
function setup() {
noSmooth();
pixelDensity(1);
createCanvas(min(600, windowWidth), min(600, windowWidth));
w = width / cols;
h = height / rows;
for (let i = 0; i < cols; i++) {
grid[i] = new Array(rows);
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j] = new Spot(i, j);
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].addNeighbors(grid);
}
}
start = grid[floor(cols / 2)][0];
end = grid[floor(cols / 2)][rows - 1];
start.wall = false;
end.wall = false;
openSet.push(start);
strokeCap(ROUND);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].wall && grid[i][j].show(color(0));
}
}
}
function draw() {
// background(0);
// clear();
// for (let i = 0; i < cols; i++) {
// for (let j = 0; j < rows; j++) {
// grid[i][j].wall && grid[i][j].show(color(0));
// }
// }
if (openSet.length > 0) {
let winner = 0;
for (let i = 0; i < openSet.length; i++) {
if (openSet[i].f < openSet[winner].f) {
winner = i;
}
}
current = openSet[winner];
if (openSet[winner] === end) {
console.log("DONE!!!!");
noLoop();
}
closedSet.push(current);
openSet.splice(winner, 1);
let neighbors = current.neighbors;
for (let i = 0; i < neighbors.length; i++) {
let neighbor = neighbors[i];
if (!closedSet.includes(neighbor) & !neighbor.wall) {
let tempg = current.g + 1;
let newPath = false;
if (openSet.includes(neighbor)) {
if (tempg < neighbor.g) {
neighbor.g = tempg;
newPath = true;
}
} else {
neighbor.g = tempg;
openSet.push(neighbor);
newPath = true;
}
if (newPath) {
neighbor.h = heuristic(neighbor, end);
neighbor.f = neighbor.g + neighbor.h;
neighbor.prev = current;
}
}
}
} else {
console.log("NO SOLUTION");
solution = false;
noLoop();
//no solution
}
// for (let i = 0; i < cols; i++) {
// for (let j = 0; j < rows; j++) {
// grid[i][j].wall && grid[i][j].show(color(0));
// }
// }
// for (let i = 0; i < openSet.length; i++) {
// openSet[i].show(color(0, 255, 0));
// }
// for (let i = 0; i < closedSet.length; i++) {
// closedSet[i].show(color(255, 0, 0));
// }
if (solution) {
path = [];
let temp = current;
path.push(current);
let i = 0;
while (temp.prev && i < 1) {
// while (temp.prev) {
path.push(temp.prev);
temp = temp.prev;
i++;
}
}
// for (let i = 0; i < path.length; i++) {
// path[i].show(color(0, 0, 255));
// }
noFill();
stroke(255, 0, 255);
strokeWeight(w / 3);
beginShape();
for (let i = 0; i < min(path.length, 2); i++) {
vertex(path[i].x, path[i].y);
}
endShape();
}