-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
339 lines (294 loc) · 8.22 KB
/
main.js
File metadata and controls
339 lines (294 loc) · 8.22 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// MinHeap Implementation
class MinHeap {
constructor() {
this.heap = [];
}
push(element) {
this.heap.push(element);
this.bubbleUp(this.heap.length - 1);
}
pop() {
if (this.isEmpty()) return null;
const min = this.heap[0];
const last = this.heap.pop();
if (!this.isEmpty()) {
this.heap[0] = last;
this.sinkDown(0);
}
return min;
}
isEmpty() {
return this.heap.length === 0;
}
contains(element) {
return this.heap.includes(element);
}
bubbleUp(index) {
const element = this.heap[index];
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
let parent = this.heap[parentIndex];
if (element.f >= parent.f) break;
this.heap[parentIndex] = element;
this.heap[index] = parent;
index = parentIndex;
}
}
sinkDown(index) {
const length = this.heap.length;
const element = this.heap[index];
while (true) {
let leftChildIndex = 2 * index + 1;
let rightChildIndex = 2 * index + 2;
let leftChild, rightChild;
let swap = null;
if (leftChildIndex < length) {
leftChild = this.heap[leftChildIndex];
if (leftChild.f < element.f) {
swap = leftChildIndex;
}
}
if (rightChildIndex < length) {
rightChild = this.heap[rightChildIndex];
if (
(swap === null && rightChild.f < element.f) ||
(swap !== null && rightChild.f < leftChild.f)
) {
swap = rightChildIndex;
}
}
if (swap === null) break;
this.heap[index] = this.heap[swap];
this.heap[swap] = element;
index = swap;
}
}
}
// Global Variables
let cols = 25;
let rows = 25;
let grid = [];
let openSet;
let closedSet;
let start;
let end;
let w, h;
let path = [];
let isRunning = false;
let isFinished = false;
let noSolution = false;
// Setup Function
function setup() {
const canvas = createCanvas(600, 600);
canvas.parent('canvas-container');
w = width / cols;
h = height / rows;
resetGrid();
// Event Listeners
document.getElementById('start-btn').addEventListener('click', startAlgorithm);
document.getElementById('reset-btn').addEventListener('click', resetPath);
document.getElementById('clear-btn').addEventListener('click', resetGrid);
document.getElementById('random-btn').addEventListener('click', randomizeWalls);
}
// Draw Function
function draw() {
background(220); // Changed to light grey to see canvas boundaries
if (isRunning && !isFinished) {
if (!openSet.isEmpty()) {
let current = openSet.pop();
closedSet.push(current);
if (current === end) {
isFinished = true;
isRunning = false;
reconstructPath(current);
document.getElementById('status-message').innerText = "Path Found!";
console.log("DONE!");
}
let neighbors = current.neighbors;
for (let neighbor of neighbors) {
if (!closedSet.includes(neighbor) && !neighbor.wall) {
let tempG = current.g + heuristic(neighbor, current);
let newPath = false;
if (openSet.contains(neighbor)) {
if (tempG < neighbor.g) {
neighbor.g = tempG;
newPath = true;
}
} else {
neighbor.g = tempG;
newPath = true;
openSet.push(neighbor);
}
if (newPath) {
neighbor.h = heuristic(neighbor, end);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
} else {
console.log('no solution');
isFinished = true;
isRunning = false;
noSolution = true;
document.getElementById('status-message').innerText = "No Solution Found.";
}
}
// Draw Grid
if (grid && grid.length > 0) {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].show(color(255));
}
}
}
// Draw Closed Set
for (let i = 0; i < closedSet.length; i++) {
closedSet[i].show(color(255, 100, 100)); // Light Red
}
// Draw Open Set
for (let item of openSet.heap) {
item.show(color(100, 255, 100)); // Light Green
}
// Draw Path
if (!noSolution) {
// If finished, draw the final path
if (isFinished) {
noFill();
stroke(0, 0, 255);
strokeWeight(w / 2);
beginShape();
for (let p of path) {
vertex(p.i * w + w / 2, p.j * h + h / 2);
}
endShape();
}
}
// Draw Start and End
if (start) start.show(color(0, 255, 0)); // Green
if (end) end.show(color(255, 0, 0)); // Red
// Mouse Interaction
if (mouseIsPressed && !isRunning && !isFinished) {
handleMouseInput();
}
}
function handleMouseInput() {
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
let i = floor(mouseX / w);
let j = floor(mouseY / h);
if (grid[i] && grid[i][j]) {
if (grid[i][j] !== start && grid[i][j] !== end) {
grid[i][j].wall = true;
}
}
}
}
function startAlgorithm() {
if (isFinished) resetPath();
isRunning = true;
document.getElementById('status-message').innerText = "Searching...";
}
function resetPath() {
isRunning = false;
isFinished = false;
noSolution = false;
path = [];
openSet = new MinHeap();
closedSet = [];
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].previous = undefined;
grid[i][j].g = 0;
grid[i][j].h = 0;
grid[i][j].f = 0;
}
}
openSet.push(start);
document.getElementById('status-message').innerText = "Path reset. Press Start.";
}
function resetGrid() {
isRunning = false;
isFinished = false;
noSolution = false;
path = [];
grid = new Array(cols);
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 Node(i, j);
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].addNeighbors(grid);
}
}
start = grid[0][0];
end = grid[cols - 1][rows - 1];
start.wall = false;
end.wall = false;
openSet = new MinHeap();
closedSet = [];
openSet.push(start);
document.getElementById('status-message').innerText = "Grid cleared. Draw walls and press Start.";
}
function randomizeWalls() {
resetGrid();
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j] !== start && grid[i][j] !== end) {
if (random(1) < 0.3) {
grid[i][j].wall = true;
}
}
}
}
document.getElementById('status-message').innerText = "Walls randomized. Press Start.";
}
function reconstructPath(current) {
path = [];
let temp = current;
path.push(temp);
while (temp.previous) {
path.push(temp.previous);
temp = temp.previous;
}
}
function heuristic(a, b) {
return dist(a.i, a.j, b.i, b.j);
}
function Node(i, j) {
this.i = i;
this.j = j;
this.f = 0;
this.g = 0;
this.h = 0;
this.neighbors = [];
this.previous = undefined;
this.wall = false;
this.show = function (col) {
stroke(200); // Add light grey stroke to see grid
if (this.wall) {
fill(50); // Dark Grey for walls
rect(this.i * w, this.j * h, w - 1, h - 1);
} else if (col) {
fill(col);
rect(this.i * w, this.j * h, w - 1, h - 1);
}
}
this.addNeighbors = function (grid) {
let i = this.i;
let j = this.j;
if (i < cols - 1) this.neighbors.push(grid[i + 1][j]);
if (i > 0) this.neighbors.push(grid[i - 1][j]);
if (j < rows - 1) this.neighbors.push(grid[i][j + 1]);
if (j > 0) this.neighbors.push(grid[i][j - 1]);
// Diagonals
if (i > 0 && j > 0) this.neighbors.push(grid[i - 1][j - 1]);
if (i < cols - 1 && j > 0) this.neighbors.push(grid[i + 1][j - 1]);
if (i > 0 && j < rows - 1) this.neighbors.push(grid[i - 1][j + 1]);
if (i < cols - 1 && j < rows - 1) this.neighbors.push(grid[i + 1][j + 1]);
}
}