-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.js
363 lines (286 loc) · 8.5 KB
/
astar.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
const gridEl = document.getElementById("grid")
const grid = []
const DEFAULTS = {
width: 8,
height: 8,
startPos: { x: 0, y: 0 },
endPos: { x: 7, y: 5 },
obstacles: [
{ x: 4, y: 4 },
{ x: 5, y: 4 },
{ x: 6, y: 4 },
{ x: 7, y: 4 },
],
showCost: false
}
let startPos = DEFAULTS.startPos
let endPos = DEFAULTS.endPos
let width = DEFAULTS.width, height = DEFAULTS.height
let obstacles = DEFAULTS.obstacles
let showCost = DEFAULTS.showCost
let isHoldingShift = false
let isHoldingAlt = false
const setInputValue = (name, value) => {
const input = document.querySelector(`#info input[name=${name}]`)
input.setAttribute("value", value)
}
const changeInfoPosText = (id, pos) => {
const div = document.querySelector(`#info div[id=${id}]`)
const [ xSpan, ySpan ] = Array.of(...div.children)
xSpan.textContent = pos.x
ySpan.textContent = pos.y
}
const handleCellClick = (event) => {
const pos = {
x: parseInt(event.target.getAttribute("x")),
y: parseInt(event.target.getAttribute("y")),
}
if (isHoldingShift) {
const obstacleIndex = obstacles.findIndex(ob => eqPos(ob, pos))
if (obstacleIndex !== -1) {
obstacles.splice(obstacleIndex, 1)
} else {
obstacles.push(pos)
}
}
else {
if (isHoldingAlt) {
endPos = pos
changeInfoPosText("end", endPos)
} else {
startPos = pos
changeInfoPosText("start", startPos)
}
}
redraw()
}
const clearGrid = () => {
const children = Array.of(...gridEl.children)
children.forEach(child => gridEl.removeChild(child))
grid.children = []
}
const createGrid = () => {
for (let w = 0; w < width; w++) {
const rowEl = document.createElement("div")
rowEl.classList.add("row")
const row = []
for (let h = 0; h < height; h++) {
const cellEl = document.createElement("span")
cellEl.classList.add("cell")
cellEl.setAttribute("x", w)
cellEl.setAttribute("y", h)
rowEl.appendChild(cellEl)
cellEl.addEventListener("mousedown", handleCellClick)
const cell = { x: w, y: h }
row.push(cell)
}
grid.push(row)
gridEl.appendChild(rowEl)
}
}
const markCell = ({ x, y }, cellType, remove) => {
const row = gridEl.children[x]
const cell = row.children[y]
if (remove) {
const classes = Array.of(...cell.classList)
classes.forEach(aClass => cell.classList.remove(aClass))
cell.classList.add("cell")
}
cell.classList.add(cellType)
}
const unmarkCell = ({ x, y }, cellType) => {
const row = gridEl.children[x]
const cell = row.children[y]
cell.classList.remove(cellType)
}
const placeCost = (pos, gCost, hCost, fCost) => {
if (!showCost) return
const { x, y } = pos
const row = gridEl.children[x]
const cell = row.children[y]
const children = Array.of(...cell.children)
children.forEach(child => cell.removeChild(child))
const costContainer = document.createElement("div")
costContainer.classList.add("cost-container")
const fCostEl = document.createElement("div")
fCostEl.classList.add("f-cost")
fCostEl.textContent = fCost.toFixed(0)
const gCostAndHCost = document.createElement("div")
gCostAndHCost.classList.add("g-and-h-cost")
const gCostEl = document.createElement("span")
gCostEl.classList.add("g-cost")
gCostEl.textContent = gCost.toFixed(0)
const hCostEl = document.createElement("span")
hCostEl.classList.add("h-cost")
hCostEl.textContent = hCost.toFixed(0)
gCostAndHCost.appendChild(gCostEl)
gCostAndHCost.appendChild(hCostEl)
costContainer.appendChild(gCostAndHCost)
costContainer.appendChild(fCostEl)
cell.appendChild(costContainer)
}
const eqPos = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => x1 === x2 && y1 === y2
const deepClone = obj => JSON.parse(JSON.stringify(obj))
/*
* Expect that 0,0 should return 0,1 and 1,0
*
*/
const getNeighbours = ({ x, y }, width, height, obstacles) => {
const neighbours = [
{ x: x, y: y - 1 },
{ x: x - 1, y: y },
{ x: x, y: y + 1 },
{ x: x + 1, y: y },
]
const fulfillsConstraints = (pos) => {
const notInObstacles = () => !obstacles.find(ob => eqPos(ob, pos))
return notInObstacles() && pos.x >= 0 && pos.y >= 0 && pos.x < width && pos.y < height
}
return neighbours.filter(n => fulfillsConstraints(n))
}
const retrace = (pos) => {
const path = []
let current = pos
while(current !== null) {
path.push(current.pos)
current = current.parent
}
return path.reverse()
}
const countCost = (posA, posB) => {
const a = Math.pow(posA.x - posB.x, 2)
const b = Math.pow(posA.y - posB.y, 2)
return Math.sqrt(a + b)
}
/* A-Star */
const getPath = (startPos, endPos, obstacles) => {
const open = [], closed = []
// Add startNode in open
open.push({
pos: deepClone(startPos),
fCost: 0,
gCost: 0,
hCost: 0,
parent: null
})
let end = null
while(open.length > 0) {
const current = deepClone(open.pop())
markCell(current.pos, "current-pos")
const neighbours = getNeighbours(current.pos, width, height, obstacles)
.map(pos => ({
pos,
gCost: 99999,
hCost: 99999,
fCost: 9999,
parent: deepClone(current)
}))
for (let n = 0; n < neighbours.length; n++) {
const neighbour = neighbours[n]
markCell(neighbour.pos, "neighbour-pos")
if (eqPos(neighbour.pos, endPos)) {
unmarkCell(current.pos, "current-pos")
unmarkCell(neighbour.pos, "neighbour-pos")
return retrace(deepClone(neighbour))
}
else {
neighbour.gCost = current.gCost + countCost(neighbour.pos, current.pos)
neighbour.hCost = countCost(neighbour.pos, endPos)
neighbour.fCost = neighbour.gCost + neighbour.hCost
const inOpen = pos => open.find(obj => eqPos(obj.pos, pos))
const inClosed = pos => closed.find(obj => eqPos(obj.pos, pos))
const inOpenPos = inOpen(neighbour.pos)
const inClosedPos = inClosed(neighbour.pos)
if ((inOpenPos && (inOpenPos.fCost < neighbour.fCost)) || (inClosedPos && (inClosedPos.fCost < neighbour.fCost))) {
unmarkCell(neighbour.pos, "neighbour-pos")
continue
}
else {
placeCost(neighbour.pos, neighbour.gCost, neighbour.hCost, neighbour.fCost)
if (!open.find(obj => eqPos(obj.pos, neighbour.pos))) {
open.push(deepClone(neighbour))
open.sort((a, b) => {
if (a.fCost === b.fCost) return b.hCost - a.hCost
return b.fCost - a.fCost
})
}
}
}
unmarkCell(neighbour.pos, "neighbour-pos")
}
closed.push(deepClone(current))
unmarkCell(current.pos, "current-pos")
}
if (end === null) {
console.log("Did not find the path")
return []
}
return retrace(end)
}
const redraw = () => {
clearGrid()
createGrid()
markCell(startPos, "start-pos", true)
markCell(endPos, "end-pos", true)
obstacles.forEach(ob => markCell(ob, "obstacle-pos", true))
const path = getPath(startPos, endPos, obstacles)
path
.filter(p => !eqPos(startPos, p))
.filter(p => !eqPos(endPos, p))
.forEach(p => markCell(p, "path-pos"))
}
const initGlobalListeners = () => {
window.document.addEventListener("keydown", event => {
switch(event.key) {
case "Shift": {
isHoldingShift = true
break
}
case "Alt": {
isHoldingAlt = true
break
}
}
})
window.document.addEventListener("keyup", event => {
switch(event.key) {
case "Shift": {
isHoldingShift = false
break
}
case "Alt": {
isHoldingAlt = false
break
}
}
})
const checkbox = document.querySelector("#info input[type='checkbox']")
checkbox.addEventListener("change", (event) => {
showCost = event.target.checked
redraw()
})
const button = document.querySelector("#clear-obstacles")
button.addEventListener("click", () => {
obstacles = []
redraw()
})
const setInputListener = name => {
const input = document.querySelector(`#info input[name=${name}]`)
if (input) {
input.addEventListener("change", (event => {
if (name === "width") width = event.target.value
else if (name === "height") height = event.target.value
redraw()
}))
}
}
setInputListener("width")
setInputListener("height")
}
setInputValue("width", width)
setInputValue("height", height)
setInputValue("costCheckbox", showCost)
changeInfoPosText("start", startPos)
changeInfoPosText("end", endPos)
redraw()
initGlobalListeners()