forked from M3OpenSource/ShortestPath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar_thibaud.html
220 lines (194 loc) · 5.88 KB
/
astar_thibaud.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
<!-- http://will.thimbleby.net/a-shortest-path-in-javascript/ -->
<html>
<script language="JavaScript" type="text/JavaScript" src="astar_thibaud.js"></script>
<script language="JavaScript" type="text/JavaScript" src="warehouse1.js"></script>
<script language="JavaScript" type="text/JavaScript" src="warehouse2.js"></script>
<script>
var cellSize = 10;
var canvasWidth;
var canvasHeight;
var map, canvas, ctx, allowDiagonals, nodesSearched;
var mDown = false;
var dragMode;
var start = {x:2,y:2};
var end = {x:18,y:10};
var startMap;
function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
while(1)
{
curleft += obj.offsetLeft;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.x)
curleft += obj.x;
return curleft;
}
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}
function mouseDown(event) {
var pos = {x:Math.floor((event.clientX-findPosX(canvas))/cellSize), y:Math.floor((event.clientY-findPosY(canvas))/cellSize)};
mDown = true;
dragMode = 0;
if(pos.x==start.x && pos.y==start.y)
dragMode = 2;
else if(pos.x==end.x && pos.y==end.y)
dragMode = 3;
else if(map[pos.x][pos.y].solid)
dragMode = 1;
mouseMoved(event);
}
function mouseMoved(event) {
if(mDown) {
var pos = {x:Math.floor((event.clientX-findPosX(canvas))/cellSize), y:Math.floor((event.clientY-findPosY(canvas))/cellSize)};
switch(dragMode) {
case 0:
map[pos.x][pos.y].solid = true;
break;
case 1:
map[pos.x][pos.y].solid = false;
break;
case 2:
start.x = pos.x;
start.y = pos.y;
break;
case 3:
end.x = pos.x;
end.y = pos.y;
break;
}
draw();
}
}
function mouseUp(e) {
mDown = false;
}
function clearMap() {
for(var i=0;i<map.length;i++) {
for(var j=0;j<map[i].length;j++) {
map[i][j].solid = false;
}
}
draw();
}
function randomMap() {
for(var i=0;i<map.length;i++) {
for(var j=0;j<map[i].length;j++) {
map[i][j].solid = Math.random() > 0.8;
}
}
draw();
}
function init() {
map = new Array;
for(var i=0;i<startMap.length;i++) {
map[i] = new Array;
for(var j=0;j<startMap[i].length;j++) {
map[i][j] = new Node(i, j, startMap[i][j]);
}
}
//canvas = document.getElementById("canvas");
canvas.width = canvasWidth = map.length * cellSize;
canvas.height = canvasHeight = map[0].length * cellSize;
ctx = canvas.getContext("2d");
canvas.onmousedown = mouseDown;
canvas.onmousemove = mouseMoved;
canvas.onmouseup = mouseUp;
draw();
}
function draw() {
nodesSearched = 0;
allowDiagonals = document.getElementById("diagonals").checked;
diagonalCost = document.getElementById("diagonalcost").checked? 1.01 : 1.0;
dotTiebreaker = document.getElementById("dotTiebreaker").checked;
badSorting = !(document.getElementById("badSorting").checked);
ctx.fillStyle = '#EEE';
ctx.strokeStyle = '#888';
ctx.clearRect(0,0,canvasWidth,canvasHeight);
var r = pathFind(start.x, start.y, end.x, end.y);
ctx.strokeRect(0.5, 0.5, canvasWidth, canvasHeight);
ctx.fillStyle = '#333';
for(i=0;i<map.length;i++) {
for(j=0;j<map[i].length;j++) {
if(map[i][j].solid) {
// map
//ctx.fillRect(i*cellSize, j*cellSize, cellSize, cellSize);
}
else {
// gridlines
//ctx.strokeRect(i*cellSize-0.5, j*cellSize-0.5, cellSize, cellSize);
}
}
}
ctx.fillStyle = '#F77';
ctx.fillRect(start.x*cellSize, start.y*cellSize, cellSize, cellSize);
ctx.fillStyle = '#77F';
ctx.fillRect(end.x*cellSize, end.y*cellSize, cellSize, cellSize);
ctx.strokeStyle = '#F00';
ctx.lineWidth = 5;
ctx.beginPath();
for(i=0;i<r.length;i++) {
if(i==0) ctx.moveTo(r[i].x*cellSize+cellSize/2+0.5, r[i].y*cellSize+cellSize/2+0.5);
else ctx.lineTo(r[i].x*cellSize+cellSize/2+0.5, r[i].y*cellSize+cellSize/2+0.5);
}
ctx.stroke();
ctx.lineWidth = 1;
document.getElementById("pathlength").innerHTML = r.length;
document.getElementById("nodes").innerHTML = nodesSearched;
}
function warehouse1() {
start = {x:2,y:2};
end = {x:18,y:10};
startMap = mapWarehouse1;
canvas1 = document.getElementById("canvas1");
canvas2 = document.getElementById("canvas2");
canvas1.style.display = "";
canvas2.style.display = "none";
canvas = canvas1;
}
function warehouse2() {
start = {x:2,y:2};
end = {x:18,y:10};
startMap = mapWarehouse2;
canvas1 = document.getElementById("canvas1");
canvas2 = document.getElementById("canvas2");
canvas1.style.display = "none";
canvas2.style.display = "";
canvas = canvas2;
}
</script>
<body onload="warehouse1();init()" style="font-family:'Lucida Grande', Verdana, Arial, Sans-Serif;font-size:12px">
<canvas id="canvas1" style="float:left;margin-right:10px; background-image: url('warehouse1.png'); display:none"></canvas>
<canvas id="canvas2" style="float:left;margin-right:10px; background-image: url('warehouse2.png'); display:none"></canvas>
<div>
<input type="button" onclick="warehouse1();init()" value="Warehouse1">
<input type="button" onclick="warehouse2();init()" value="Warehouse2">
<br/>
<input id="diagonals" type="checkbox" onclick="draw()" checked="true">Allow diagonals<br>
<input id="diagonalcost" type="checkbox" onclick="draw()" checked="true">Tweak diagonal cost<br>
<input id="badSorting" type="checkbox" onclick="draw()" checked="yes">Prefer new nodes<br>
<input id="dotTiebreaker" type="checkbox" onclick="draw()">Dot product tie breaker<br><br>
<input type="button" onclick="clearMap()" value="Clear"><input type="button" onclick="randomMap()" value="Random"><br><br>
<table style="font-family:'Lucida Grande', Verdana, Arial, Sans-Serif;font-size:12px"><tr><td>
Path length:</td><td><span id="pathlength" style="font-size:4em"></span></td></tr>
<tr><td>Nodes searched:</td><td><span id="nodes"></span></td></tr></table>
</div>
</body>
</html>