-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
517 lines (403 loc) · 14.7 KB
/
main.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
class GridTile {
/**
* Instatiate a grid tile object
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
* @param {*} center
* @param {boolean} walkable
*/
constructor(x, y, width, height, center, walkable) {
this.x = x;
this.y = y;
this.w = width;
this.h = height;
this.center = center;
this.walkable = walkable;
}
}
class Grid {
constructor(width, height, canvas, color, fill_color, line_width) {
// set canvas context reference
this.context = canvas.getContext("2d");
// compute tile size
this.tile_size = Math.floor(canvas.clientHeight / height);
// set width and height grid
this.width = width * this.tile_size;
this.height = height * this.tile_size;
this.width_count = width;
this.height_count = height;
// set styles
this.color = color;
this.fill_color = fill_color;
this.line_width = line_width;
// initialize tiles matrix
this.tiles = [];
}
/**
* Calculate tiles inside grid with the right dimensions
* and push them inside this.tiles
*/
compute_tiles() {
// calculate tiles starting from tile_size, width, height
for (let i = 0; i < this.width_count; i++) {
this.tiles[i] = [];
for (let j = 0; j < this.height_count; j++) {
// create new tile with right coordinates
let new_tile = new GridTile(i, j, this.tile_size, this.tile_size, {
x: this.tile_size / 2 * (j + 1),
y: this.tile_size / 2 * (i + 1)
}, true);
// add tile inside matrix[i, j]
this.tiles[i].push(new_tile);
}
}
}
/**
* Draw grid and tiles. If the tiles are empty then don't draw them.
*/
draw() {
// draw grid borders
this.context.beginPath();
this.context.strokeStyle = this.color;
// set grid dimension and coordinates
this.context.rect(0, 0, this.width, this.height);
this.context.stroke();
// if there aren't any tiles then don't draw them
if (this.tiles.length == 0) {
console.info(`Grid.draw() => there aren't any tiles, do you have execute 'compute_tiles()' first?`);
return;
}
this.context.beginPath();
this.context.fillStyle = this.fill_color;
this.context.strokeStyle = this.color;
this.tiles.forEach((tiles, i) => {
tiles.forEach((tile, j) => {
// draw borders
this.context.rect(tile.x * tile.w, tile.y * tile.h, tile.w, tile.h);
// fill rect
this.context.fillRect(tile.x * tile.w, tile.y * tile.h, tile.w, tile.h);
this.context.stroke();
});
})
}
clear_path() {
for (let x = 0; x < this.width_count; x++) {
for (let y = 0; y < this.height_count; y++) {
if (this.tiles[x][y].walkable) {
this.color_tile(x, y, this.fill_color, this.color);
}
}
}
}
color_tile(x, y, color, border_color) {
let tile = this.tiles[x][y];
this.context.beginPath();
this.context.fillStyle = color;
this.context.strokeStyle = this.color;
this.context.rect(tile.x * tile.w, tile.y * tile.h, tile.w, tile.h);
this.context.fillRect(tile.x * tile.w, tile.y * tile.h, tile.w, tile.h);
this.context.stroke();
}
}
class PathNode {
constructor(x, y, walkable, parent = null) {
this.x = x;
this.y = y;
this.walkable = walkable;
this.f = 0;
this.g = 0;
this.h = 0;
this.parent = parent;
}
get_name() {
return `node${this.x}${this.y}`;
}
get_coordinates() {
return [this.x, this.y];
}
}
class PathFinder {
/**
*
* @param {Grid} grid
*/
constructor(grid) {
this.grid = grid;
}
/**
* If the heuristic function of A* is 0 then only the cost is evaluated
* @callback heuristic
* @param {PathNode} n
* @param {PathNode} v
* @returns {number}
*/
dijikstra(n, v) {
return 0;
}
/**
* Evaluate best path if the movements allowed are in 4 directions.
* @callback heuristic *
* @param {PathNode} n
* @param {PathNode} v
* @return {number} manatthan distance between n and v
*/
manatthan_distance(n, v) {
const dx = (v.x - n.x), dy = (v.y - n.y);
return dx + dy;
}
/**
* @callback heuristic
* @param {PathNode} n
* @param {PathNode} v
* @returns {number}
*/
euclidian_distance(n, v) {
const dx = Math.pow(v.x - n.x, 2), dy = Math.pow(v. y - n.y, 2);
return Math.sqrt(dx + dy);
}
/**
* @deprecated
* @callback heuristic
* @param {PathNode} n
* @param {PathNode} v
* @returns {number}
*/
euclidian_distance2(n, v) {
// http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
const dx = Math.pow(v.x - n.x, 2), dy = Math.pow(v. y - n.y, 2);
return dx + dy;
}
/**
*
* @param {PathNode} u
* @param {number} offset_x
* @param {number} offset_y
* @param {PathNode[]} pool_nodes
* @return {PathNode}
*/
create_adjacent_node(u, offset_x, offset_y, pool_nodes) {
const x = u.x + offset_x;
const y = u.y + offset_y;
if (x < 0 || y < 0) return null;
if (x >= this.grid.width_count) return null;
if (y >= this.grid.height_count) return null;
if (!this.grid.tiles[x][y].walkable) {
return null;
}
// check if nodes exists indside the pool
// using the coordinates of node and return it
let adj_node = new PathNode(x, y, this.grid.tiles[x][y].walkable);
return adj_node;
}
is_in_open_list(j, open_list) {
return open_list.find(e => e.x == j.x && e.y == j.y);
}
/**
* Find path using A* using an heuristic function
* @param {number[]} start
* @param {number[]} end
* @param {heuristic} h the heuristic function
* @param {boolean} only_four only 4 axis?
*/
find_path(start, end, h, only_four) {
// set the open list as a priority queue
// and use a comparsion function
// that evaluates the f value of a path node
let open_list = new PriorityQueue({
comparator: (u, v) => u.f - v.f
})
// declare a closed list as dictonary
let closed_list = {};
let copy_list = [];
// declare start and end node
const start_node = new PathNode(start[0], start[1], this.grid.tiles[start[0]][start[1]].walkable);
const end_node = new PathNode(end[0], end[1], this.grid.tiles[end[0]][end[1]].walkable);
// create all path nodes
const pool_nodes = [start_node, end_node];
// Create path
let final_path = [];
// Push the first node inside open list
open_list.queue(start_node);
// while openList is
while (open_list.length > 0) {
// pop the lowest f node from priority queue
const u = open_list.dequeue();
copy_list = copy_list.filter((e) => e.x != u.x && e.y == u.y);
closed_list[u.get_name()] = u;
final_path.push(u);
// do i have reached the final node?
if (u.x == end_node.x && u.y == end_node.y) break;
let adjacent_nodes = [];
if (only_four) {
adjacent_nodes = [
this.create_adjacent_node(u, 0, -1, pool_nodes),
this.create_adjacent_node(u, -1, 0, pool_nodes),
this.create_adjacent_node(u, +1, 0, pool_nodes),
this.create_adjacent_node(u, 0, +1, pool_nodes)
];
}
else {
adjacent_nodes = [
this.create_adjacent_node(u, -1, -1, pool_nodes),
this.create_adjacent_node(u, 0, -1, pool_nodes),
this.create_adjacent_node(u, +1, -1, pool_nodes),
this.create_adjacent_node(u, -1, 0, pool_nodes),
this.create_adjacent_node(u, +1, 0, pool_nodes),
this.create_adjacent_node(u, -1, +1, pool_nodes),
this.create_adjacent_node(u, 0, +1, pool_nodes),
this.create_adjacent_node(u, +1, +1, pool_nodes)
];
}
// remove null nodes inside adjacents list
adjacent_nodes = adjacent_nodes.filter((n) => n != null);
for (let index = 0; index < adjacent_nodes.length; index++) {
let j = adjacent_nodes[index];
if (j.get_name() in closed_list) continue;
j.g = u.g + 1;
j.h = h(j, end_node);
j.f = j.g + j.h;
let ex_j = this.is_in_open_list(j, copy_list);
if (ex_j != null && j.g > ex_j.g) continue;
j.parent = u;
open_list.queue(j);
copy_list.push(j);
}
}
// check if end node is inside of path
if (!(end_node.get_name() in closed_list)) return [];
let pi_node = final_path[final_path.length - 1];
let real_path = [];
while (pi_node != null) {
real_path.push(pi_node);
pi_node = pi_node.parent;
}
return real_path.reverse();
}
}
let execute = () => {};
let save_walls = () => {};
let load_walls = () => {};
let remove_walls = () => {};
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById("playground");
const context = canvas.getContext("2d");
const width = 8;
const height = 8;
const tile_color = "#121212";
const border_color = "#363636";
document.getElementById("startPosition").value = `1, 1`;
document.getElementById("endPosition").value = `${width}, ${height}`
let grid = new Grid(width, height, canvas, border_color, tile_color, "2");
grid.compute_tiles();
grid.draw();
canvas.addEventListener("click", (e) => {
let offset_x = e.offsetX, offset_y = e.offsetY;
let indicies = [];
// find coordinates inside matrix
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
if (offset_x >= grid.tile_size * x && offset_x <= grid.tile_size * ( x + 1) &&
offset_y >= grid.tile_size * y && offset_y <= grid.tile_size * ( y + 1)) {
indicies = [x, y];
break;
}
}
}
if (indicies.length == 0) return;
let tile = grid.tiles[indicies[0]][indicies[1]];
if (!tile.walkable) {
grid.color_tile(tile.x, tile.y, tile_color, border_color);
}
else {
grid.color_tile(tile.x, tile.y, "#e53935", border_color);
}
tile.walkable = !tile.walkable;
}, false);
execute = () => {
// clear path
grid.clear_path();
const path_finder = new PathFinder(grid);
const four_axis = document.getElementById("four_axis").checked;
const start_value = document.getElementById("startPosition").value;
const end_value = document.getElementById("endPosition").value;
const start = start_value.split(", ").map((splitted) => {
return parseInt(splitted) - 1
});
const end = end_value.split(", ").map((splitted) => {
return parseInt(splitted) - 1
});
console.log(`From ${start} to ${end}`);
const heuristic_name = document.getElementById("heuristic").value;
let heuristic_chosen = () => {};
if (heuristic_name == "euclide") {
heuristic_chosen = path_finder.euclidian_distance;
}
else if (heuristic_chosen == "manatthan") {
heuristic_chosen = path_finder.manatthan_distance;
}
else {
heuristic_chosen = path_finder.dijikstra;
}
const path = path_finder.find_path(start, end, heuristic_chosen, four_axis);
console.log(path);
if (path.length == 0) {
bulmaToast.toast({
message: "No paths were found!",
type: "is-danger",
closeOnClick: true,
pauseOnHover: true,
animate: { in: "fadeIn", out: "fadeOut" }
});
return;
}
path.forEach((node) => grid.color_tile(node.x, node.y, "#43A047", border_color));
// color start and end
grid.color_tile(start[0], start[1], "#FB8C00", border_color);
grid.color_tile(end[0], end[1], "#1E88E5", border_color);
};
remove_walls = () => {
if (localStorage.getItem('wall_pattern') == undefined) {
bulmaToast.toast({
message: "There aren't walls saved into storage!",
type: "is-danger",
closeOnClick: true,
pauseOnHover: true,
});
return;
}
localStorage.removeItem('wall_pattern');
};
save_walls = () => {
// load tiles and coordinates
const coordinates = grid.tiles.map((row) => row.filter((tile) => !tile.walkable)).filter(e => e.length > 0).flat().map((e) => [e.x, e.y]);
const cookie = { walls: coordinates }
// save walls in web storage
localStorage.setItem('wall_pattern', JSON.stringify(cookie));
bulmaToast.toast({
message: "Walls pattern saved into storage!",
type: "is-warning",
closeOnClick: true,
pauseOnHover: true,
});
};
load_walls = () => {
// load walls form web storage
// {walls: [[x0, y0], [x1, y1], ..., [xn, yn]]}
const walls = JSON.parse(localStorage.getItem('wall_pattern')).walls;
// restore saved walls
walls.forEach((coord) => {
let tile = grid.tiles[coord[0]][coord[1]];
// color tile and set walkable
grid.color_tile(tile.x, tile.y, "#e53935", border_color);
tile.walkable = false;
});
bulmaToast.toast({
message: "Walls loaded from storage!",
type: "is-primary",
closeOnClick: true,
pauseOnHover: true,
});
};
});