-
Notifications
You must be signed in to change notification settings - Fork 0
/
better.html
290 lines (275 loc) · 10 KB
/
better.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="Shortcut Icon" href="favicon.png" type="image/x-icon" />
<title>A sample of HTML5 Canvas</title>
<style type="text/css">
html,body { margin: 0; padding: 0; width:100%; height:100%; }
#fps{ width: 50px; }
</style>
</head>
<body>
<canvas id="main" width="600" height="400"></canvas>
<div id="config">
<span>FPS:</span><input id="fps" type="text" value="0" />
<input id="size5" type="button" value="5 x 5" />
<input id="size10" type="button" value="10x10" />
<input id="size20" type="button" value="20x20" />
<input id="size50" type="button" value="50x50" />
<input id="Manhattan" type="button" value="曼哈顿距离">
<input id="Euclidean" type="button" value="欧氏距离">
<input id="Chebyshev" type="button" value="切比雪夫距离">
</div>
<script type="text/javascript">
var RAF = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
var id = window.setTimeout( callback, 1000/60 );
return id;
}
})();
var canvas = document.querySelector('#main');
var ctx = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
var size = 10;
var Manhattan = true;
var Euclidean = false;
var Chebyshev = false;
var startI, startJ, distI, distJ;
var Map = [];
var OpenList = [];
var CloseList = [];
var Barrier = [];
var Father = [];
var Index = [];
var NextTo = [[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1]];
var Evt = {
click: function(e){
var x = Math.floor(e.offsetX/size);
var y = Math.floor(e.offsetY/size);
if( (y === startI && x === startJ) || (y === distI && x === distJ) ) return;
Barrier[y][x] = 3;
}
};
var init = (function(){
var i,j,l1,l2;
for(i=0,l1=height/size; i<l1; i++){
Barrier[i] = [];
for(j=0,l2=width/size; j<l2; j++){
Barrier[i][j] = 0;
}
}
startI = 2;
startJ = 1;
distI = height/size-3;
distJ = width/size-2;
return arguments.callee;
})();
function initialize(){
var i,j,l1,l2;
Map = [];
Father = [];
Index = [];
for(i=0,l1=height/size; i<l1; i++){
Map[i] = [];
Father[i] = [];
Index[i] = [];
for(j=0,l2=width/size; j<l2; j++){
Map[i][j] = 0;
Father[i][j]= [i,j];
Index[i][j] = 0;
}
}
Map[startI][startJ] = 1; //起点
Map[distI][distJ] = 2; //终点
OpenList = [];
OpenList[0] = {};
CloseList = [];
};
function PathFinding(){
var i, j, l, l1, l2, val, u, v, child, parent, node, temp;
var row, col;
initialize();
if( typeof startI === "undefined" || typeof startJ === "undefined" || typeof distI === "undefined" || typeof distJ === "undefined") return false;
//把起点加入OpenList
OpenList.push({
i: startI,
j: startJ,
G: 0,
H: Manhattan ? Math.abs(startI-distI)*10 + Math.abs(startJ-distJ)*10 : Euclidean ? Math.sqrt((startI-distI)*(startI-distI)+(startJ-distJ)*(startJ-distJ))*10 : Chebyshev ? Math.max(Math.abs(startI-distI), Math.abs(startJ-distJ))*10 : 0
});
Index[startI][startJ] = 1;
//重复以下操作
while( (l=OpenList.length) && OpenList[1] ){
//遍历OpenList,查找F值最小的节点,更新二叉堆
node = OpenList.slice(1,2)[0];
OpenList[1] = OpenList[l-1];
Index[OpenList[1].i][OpenList[1].j] = 1;
Index[OpenList[l-1].i][OpenList[l-1].j] = 0;
OpenList.pop();
v = 1;
while( true ) {
u = v;
if( 2*u+1 <= l-2 ){
if( OpenList[u].G+OpenList[u].H > OpenList[2*u].G+OpenList[2*u].H ) v = 2*u;
if( OpenList[v].G+OpenList[v].H > OpenList[2*u+1].G+OpenList[2*u+1].H ) v = 2*u+1;
}
else if( 2*u <= l-2 ) {
if( OpenList[u].G+OpenList[u].H > OpenList[2*u].G+OpenList[2*u].H ) v = 2*u;
}
if( u !== v ){
temp = OpenList[u];
OpenList[u] = OpenList[v];
OpenList[v] = temp;
Index[OpenList[u].i][OpenList[u].j] = u;
Index[OpenList[v].i][OpenList[v].j] = v;
}
else break;
}
//把节点移到CloseList
CloseList.push( node );
Map[node.i][node.j] = -2;
//对当前节点的相邻8个节点的每个节点
for(i=0; i<8; i++){
row = node.i + NextTo[i][0];
col = node.j + NextTo[i][1];
if( typeof Map[row] === "undefined" || typeof Map[row][col] === "undefined" ) continue;
//遇到障碍物,则忽略此节点,不能绕过墙角
if( Barrier[row][col] ) { continue; }
if( i === 1 && (Barrier[row][col-1] || Barrier[row+1][col]) ) continue;
if( i === 3 && (Barrier[row][col-1] || Barrier[row-1][col]) ) continue;
if( i === 5 && (Barrier[row][col+1] || Barrier[row-1][col]) ) continue;
if( i === 7 && (Barrier[row][col+1] || Barrier[row+1][col]) ) continue;
//Map0表示节点不在OpenList不在CloseList
if(Map[row][col] === 0){
//加入OpenList,使用二叉堆
Map[row][col] = -1;
OpenList.push({
i: row,
j: col,
G: node.G + (i%2?14:10),
H: Manhattan ? Math.abs(row-distI)*10 + Math.abs(col-distJ)*10 : Euclidean ? Math.sqrt((row-distI)*(row-distI)+(col-distJ)*(col-distJ))*10 : Chebyshev ? Math.max(Math.abs(row-distI), Math.abs(col-distJ))*10 : 0
});
child = Index[row][col] = OpenList.length - 1;
parent = Math.floor(child/2)
while( child !== 1 && OpenList[child].G+OpenList[child].H < OpenList[parent].G+OpenList[parent].H ) {
temp = OpenList[child];
OpenList[child] = OpenList[parent];
OpenList[parent] = temp;
Index[OpenList[child].i][OpenList[child].j] = child;
Index[OpenList[parent].i][OpenList[parent].j] = parent;
child = parent;
parent = Math.floor(child/2);
}
Father[row][col] = [node.i, node.j];
}
//如果在OpenList中,检查路径是否更好
else if(Map[row][col] === -1){
if( node.G+(i%2?14:10) < OpenList[Index[row][col]].G ){
OpenList[Index[row][col]].G = node.G+(i%2?14:10);
child = Index[row][col];
parent = Math.floor(child/2);
while( child !== 1 && OpenList[child].G+OpenList[child].H < OpenList[parent].G+OpenList[parent].H ) {
temp = OpenList[child];
OpenList[child] = OpenList[parent];
OpenList[parent] = temp;
Index[OpenList[child].i][OpenList[child].j] = child;
Index[OpenList[parent].i][OpenList[parent].j] = parent;
child = parent;
parent = Math.floor(child/2);
}
Father[row][col] = [node.i, node.j];
}
}
//Map2表示节点为终点,结束并保存路径
else if(Map[row][col] === 2) {
Father[row][col] = [node.i, node.j];
node = Father[distI][distJ];
while( !(node[0] === startI && node[1] === startJ) ){
Map[node[0]][node[1]] = 4;
node = Father[node[0]][node[1]];
}
Map[startI][startJ] = 1;
return true;
}
}
}
Map[startI][startJ] = 1;
return false;
}
function draw(){
var i,j,l1,l2,val;
for(i=1,l1=height/size; i<l1; i++){
ctx.save();
ctx.strokeStyle = '#222';
ctx.beginPath();
ctx.moveTo(0,i*size);
ctx.lineTo(width,i*size);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
for(j=1,l2=width/size; j<l2; j++){
ctx.save();
ctx.strokeStyle = '#222';
ctx.beginPath();
ctx.moveTo(j*size, 0);
ctx.lineTo(j*size, height);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
for(i=0,l1=Map.length; i<l1; i++){
l2 = Map[i].length;
for(j=0; j<l2; j++){
val = Map[i][j];
if( val === 1 || val === 2 || Barrier[i][j] === 3 || val === 4 ){
ctx.save();
switch (Map[i][j]){
case 1: ctx.fillStyle = "#3366FF"; break;
case 2: ctx.fillStyle = "#FF0033"; break;
case 4: ctx.fillStyle = "#B88A00"; break;
}
if(Barrier[i][j] === 3) ctx.fillStyle = "#B8B8B8";
ctx.fillRect(j*size,i*size,size,size);
ctx.restore();
}
}
}
}
var times = 50;
var total = 0;
(function(){
var start = +new Date();
RAF( arguments.callee );
ctx.fillStyle = 'rgb(0,0,0)';
ctx.fillRect(0,0,width,height);
PathFinding();
draw();
var end = +new Date();
total += end-start;
if( --times === 0 ) { document.getElementById('fps').value = Math.round(1000/(total/50)), total = 0, times = 50; };
})();
canvas.addEventListener('click',Evt.click);
document.getElementById('config').onclick = function(e){
e.preventDefault();
switch(e.target.id) {
case "size5" : size = 5; break;
case "size10": size = 10; break;
case "size20": size = 20; break;
case "size50": size = 50; break;
case "Manhattan": Manhattan = true; Euclidean = false; Chebyshev = false; break;
case "Euclidean": Euclidean = true; Manhattan = false; Chebyshev = false; break;
case "Chebyshev": Chebyshev = true; Manhattan = false; Euclidean = false; break;
}
init();
}
</script>
</body>
</html>