-
Notifications
You must be signed in to change notification settings - Fork 3
/
road-search.html
220 lines (199 loc) Β· 5.49 KB
/
road-search.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.cell {
width: 6px;
height: 6px;
background-color: lightgreen;
border-bottom: solid 1px white;
border-right: solid 1px white;
vertical-align: middle;
}
#container {
display: flex;
flex-wrap: wrap;
width: 700px;
}
</style>
</head>
<body>
<div id="container"></div>
<button onclick="localStorage.map = JSON.stringify(map)">save</button>
</body>
<script>
class Sorted {
constructor(data, compare) {
this.data = data
this.compare = compare
}
take() {
if (this.data.length === 0) {
return
}
let min = this.data[0]
let minIndex = 0
for (let i = 0; i < this.data.length; i++) {
if (this.compare(this.data[i], min) < 0) {
min = this.data[i]
minIndex = i
}
}
this.data[minIndex] = this.data[this.data.length - 1]
this.data.pop()
return min
}
insert(v) {
this.data.push(v)
}
get length() {
return this.data.length
}
}
class BinaryHeap {
constructor(data, compare) {
this.data = data
this.compare = compare
}
take() {
if (this.data.length === 0) {
return
}
let min = this.data[0]
let i = 0
while (i < this.data.length) {
if (i * 2 + 1 >= this.data.length) {
break
}
if (i * 2 + 2 >= this.data.length) {
this.data[i] = this.data[i * 2 + 1]
i = i * 2 + 1
break
}
if (this.compare(this.data[i * 2 + 1], this.data[i * 2 + 2]) < 0) {
this.data[i] = this.data[i * 2 + 1]
i = i * 2 + 1
} else {
this.data[i] = this.data[i * 2 + 2]
i = i * 2 + 2
}
}
if (i < this.data.length - 1) {
this.insertAt(i, this.data.pop())
} else {
this.data.pop()
}
return min
}
insertAt(i, v) {
this.data[i] = v
while (i > 0 && this.compare(v, this.data[Math.floor((i - 1) / 2)]) < 0) {
this.data[i] = this.data[Math.floor((i - 1) / 2)]
this.data[Math.floor((i - 1) / 2)] = v
i = Math.floor((i - 1) / 2)
}
}
insert(v) {
this.insertAt(this.data.length, v)
}
get length() {
return this.data.length
}
}
function sleep(t) {
return new Promise((resolve, reject) => {
setTimeout(resolve, t)
})
}
var map = localStorage.map ? JSON.parse(localStorage.map) : new Array(10000).fill(0)
let container = document.getElementById("container")
for (let y = 0; y < 100; y++) {
for (let x = 0; x < 100; x++) {
let cell = document.createElement("div")
cell.classList.add("cell")
if (map[y * 100 + x] === 1)
cell.style.backgroundColor = 'black'
cell.addEventListener("mouseover", () => {
if (mouse) {
if (clear) {
cell.style.backgroundColor = ''
map[y * 100 + x] = 0
} else {
cell.style.backgroundColor = 'black'
map[y * 100 + x] = 1
}
}
})
container.appendChild(cell)
}
}
let mouse = false
let clear = false
document.addEventListener('mousedown', e => {
mouse = true
clear = (e.which === 3)
})
document.addEventListener('mouseup', () => mouse = false)
document.addEventListener('contextmenu', e => e.preventDefault())
async function findPath(map, start, end) {
container.children[100 * start[1] + start[0]].style.backgroundColor = 'blue' // start
container.children[100 * end[1] + end[0]].style.backgroundColor = 'red' // end
map = map.slice()
function diatance([x, y]) {
return (x - end[0]) ** 2 + (y - end[1]) ** 2
}
// let queue = [start]
// let collection = new Sorted([start], (a, b) => diatance(a) - diatance(b))
let collection = new BinaryHeap([start], (a, b) => diatance(a) - diatance(b))
function distance([x, y]) {
console.log(x, y)
return ((x - end[0]) ** 2 + (y - end[1]) ** 2)
}
async function insert([x, y], pre) {
if (map[100 * y + x] !== 0) {
return
}
if (x < 0 || y < 0 || x >= 100 || y >= 100) {
return
}
map[100 * y + x] = pre
if (!(x === start[0] && y === start[1]) && !(x === end[0] && y === end[1])) {
container.children[100 * y + x].style.backgroundColor = 'red'
}
await sleep(1)
// queue.push([x, y])
collection.insert([x, y])
}
while (collection.length) {
let [x, y] = collection.take()
if (x === start[0] && y === start[1]) {
map[100 * y + x] = 'start'
}
if (x === end[0] && y === end[1]) {
let path = []
while (x !== start[0] || y !== start[1]) {
path.push([x, y])
// if (x === end[0] && y === end[1]) {
// container.children[y * 100 + x].style.backgroundColor = 'pink'
// }
[x, y] = map[y * 100 + x]
console.log('[x,y]', [x, y])
}
return path
}
await insert([x - 1, y], [x, y])
await insert([x + 1, y], [x, y])
await insert([x, y - 1], [x, y])
await insert([x, y + 1], [x, y])
await insert([x - 1, y - 1], [x, y])
await insert([x + 1, y - 1], [x, y])
await insert([x - 1, y + 1], [x, y])
await insert([x + 1, y + 1], [x, y])
}
return null
}
</script>
</html>