-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateGrid.js
More file actions
179 lines (121 loc) · 4.08 KB
/
createGrid.js
File metadata and controls
179 lines (121 loc) · 4.08 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
/*************************************************/
/* Global Data Structures */
/*************************************************/
// Global data structure to store all blocks on the grid
let grid = [];
let path = [];
/*************************************************/
/* Block Constructor */
/*************************************************/
function Block( row, col, f, g, h) {
this.row = row;
this.col = col;
this.id = (this.row).toString() + "-" + (this.col).toString();
// Used to store the previous block for path tracing
this.previous = null;
// Used to define if a block is a barrier node - default to false
this.barrier = false;
// Used to calculate path-finding algorithm properties
this.f = f;
this.g = g;
this.h = h;
// Stores the neighboring blocks to this block
this.neighbors = [];
// Adds neighboring blocks to the neighbors array of this block
this.addNeighbors = function() {
let i = this.row;
let j = this.col;
if( i < grid[0].length - 1) {
this.neighbors.push(grid[i+1][j]);
}
if( i > 0 ) {
this.neighbors.push(grid[i-1][j]);
}
if( j < grid.length - 1 ) {
this.neighbors.push(grid[i][j+1]);
}
if( j > 0 ) {
this.neighbors.push(grid[i][j-1]);
}
}
}
/*************************************************/
/* Grid Generator */
/*************************************************/
// Creates a Grid of Blocks
function createGrid( rows, cols ) {
let html = '';
const con = document.querySelector('.con');
// Create and Store the Grid
for( let i = 0; i < rows; i++ ) {
grid[i] = [];
let rowHtml = '';
let rowConHtml = `<div class='row' id='${i}'></div>`;
con.innerHTML += rowConHtml;
let rowCon = document.getElementById(i.toString());
for( let j=0; j < cols; j++ ) {
let block = new Block(i, j);
grid[i].push(block);
rowHtml = `<div class='block' id=${block.id}> </div>`
rowCon.innerHTML += rowHtml;
}
}
// Add each block's neighbors
for( let i = 0; i < rows; i++ ) {
for( let j = 0; j < cols; j++ ) {
grid[i][j].addNeighbors();
}
}
}
/*************************************************/
/* Asynchronous Functions */
/*************************************************/
// Sleep function to slow down algorithms and show visualization
function sleep( timeout ) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
// Sets a path's element's style to show path visualization
async function setPathStyle( el ) {
await sleep(50);
document.getElementById(el.id).classList.add('path');
}
async function setEvaluatedStyle( el ) {
await sleep(10);
document.getElementById(el.id).classList.add('evaluated');
}
/*************************************************/
/* Accessory Functions */
/*************************************************/
function removeFromArray( arr, el ) {
for( let i = arr.length - 1; i >= 0; i-- ) {
if( arr[i] === el ) {
arr.splice( i, 1 );
}
}
}
/*************************************************/
/* Heuristic Functions */
/*************************************************/
function euclideanDistance( a, b ) {
let x = Math.pow((a.row - b.row), 2);
let y = Math.pow((a.col - b.col), 2);
let z = x + y;
return Math.sqrt( z );
}
function manhattanDistance( a, b ) {
let x = Math.abs(a.row - b.row);
let y = Math.abs(a.col - b.col);
return x + y;
}
function chebyshevDistance( a, b ) {
let x = Math.abs(a.row - b.row);
let y = Math.abs(a.col - b.col);
return Math.max( x, y );
}
function octileDistance ( a, b ) {
let x = Math.abs(a.row - b.row);
let y = Math.abs(a.col - b.col);
let z = Math.min( x, y );
let z2 = Math.abs(x - y );
return z + z2;
}