-
Notifications
You must be signed in to change notification settings - Fork 9
/
finder(a).html
261 lines (261 loc) · 7.21 KB
/
finder(a).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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>寻址-最短路径(a*算法)</title>
<style type="text/css">
body{
font-size:12px;
font-family: "Microsoft Yahei",Arial,Helvetica;
}
button,input{
font-family: "Microsoft Yahei",Arial,Helvetica;
}
.container{
width:1200px;
margin:0 auto 15px;
border:1px solid #ccc;
}
.container .box{
border-bottom:1px solid #ccc;
}
.container canvas{
vertical-align: bottom;
}
.container .panel{
position:relative;
padding:10px 20px;
}
.container .panel .right{
position:absolute;
top:10px;
right:20px;
}
.container .panel input{
text-align:center;
}
.copy{
width:1200px;
margin:0 auto;
text-align:center;
color:#666;
}
.copy a{
color:#666;
text-decoration:none;
}
.copy a:hover{
text-decoration:underline;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<canvas id="canvas" width="1200" height="800"></canvas>
</div>
<div class="panel">
<div class="left">
<button id="start">起点</button>
<button id="end">终点</button>
<button id="pen">画笔</button>
<button id="eraser">橡皮</button>
</div>
<div class="right">当前操作:<input id="type" type="text" value="终点" readonly/><button id="find">寻找</button></div>
</div>
</div>
<div class="copy">© <a href="http://www.passer-by.com" target="_blank">passer-by.com</a></div>
<script type="text/javascript">
(function(){
var $canvas = document.getElementById("canvas");
var $pen = document.getElementById("pen");
var $eraser = document.getElementById("eraser");
var $find = document.getElementById("find");
var $start = document.getElementById("start");
var $end = document.getElementById("end");
var $type = document.getElementById("type");
var context = $canvas.getContext("2d");
var unit = 10;
var width = $canvas.width/unit;
var height = $canvas.height/unit;
var map = [];
var start = [0,0];
var end = [0,0];
var path = "";
var isMouseDown = false;
var type = "end";
var type_text = {"end":"终点","start":"起点","pen":"画笔","eraser":"橡皮"};
var scroll = getScroll();
for(var x=0;x<width;x++){
map[x] = new Array(height);
for(var y =0;y<height;y++){
map[x][y] = 0;
}
}
$canvas.onmousedown = function(e){
isMouseDown = true;
var x = Math.floor((e.clientX - this.offsetLeft+scroll.left)/unit);
var y = Math.floor((e.clientY - this.offsetTop+scroll.top)/unit);
if(type == "start"){
start = [x,y];
draw();
}else if(type == "end"){
end = [x,y];
draw();
}else if(type == "pen"&&!map[x][y]){
map[x][y] = 1;
draw();
}else if(type == "eraser"&map[x][y]){
map[x][y] = 0;
draw();
}
};
$canvas.onmousemove = function(e){
if(isMouseDown){
var x = Math.floor((e.clientX - this.offsetLeft+scroll.left)/unit);
var y = Math.floor((e.clientY - this.offsetTop+scroll.top)/unit);
if(type == "start"){
start = [x,y];
draw();
}else if(type == "end"){
end = [x,y];
draw();
}else if(type == "pen"&&!map[x][y]){
map[x][y] = 1;
draw();
}else if(type == "eraser"&map[x][y]){
map[x][y] = 0;
draw();
}
}
};
$canvas.onmouseup = function(e){
isMouseDown = false;
};
$pen.onclick = function(){
type = "pen";
$type.value = type_text[type];
};
$eraser.onclick = function(){
type = "eraser";
$type.value = type_text[type];
};
$start.onclick = function(){
type = "start";
$type.value = type_text[type];
};
$end.onclick = function(){
type = "end";
$type.value = type_text[type];
};
$find.onclick = function(){
console.time('t');
path = '|'+finder({'map':map,'start':start,'end':end}).join('|')+'|';
console.timeEnd('t');
draw();
}
window.onscroll = function(){
scroll = getScroll();
};
draw();
function draw(){
context.fillStyle = "#fff";
context.fillRect(0, 0, $canvas.width, $canvas.width);
for(var x=0; x<width; x++){
for(var y=0; y<height; y++){
if(x==start[0]&&y==start[1]||x==end[0]&&y==end[1]){
context.fillStyle=map[x][y]?"#ccc":"#00f";
context.fillRect(x*unit,y*unit,unit,unit);
}else if(map[x][y]){
context.fillStyle="#000";
context.fillRect(x*unit,y*unit,unit,unit);
}else if(path.indexOf('|'+x+','+y+'|')>-1){
context.fillStyle="#f00";
context.fillRect(x*unit,y*unit,unit,unit);
}
}
}
}
})();
function getScroll() {
var scrollPos={};
if (window.pageYOffset||window.pageXOffset) {
scrollPos['top'] = window.pageYOffset;
scrollPos['left'] = window.pageXOffset;
}else if (document.compatMode && document.compatMode != 'BackCompat'){
scrollPos['top'] = document.documentElement.scrollTop;
scrollPos['left'] = document.documentElement.scrollLeft;
}else if(document.body){
scrollPos['top'] = document.body.scrollTop;
scrollPos['left'] = document.body.scrollLeft;
}
return scrollPos;
}
function finder(param){ //获取最短路径
var defaults = {
map :[],
start:[0,0],
end:[0,0]
};
var options = (function(target, params) {
for (var prop in params) {
target[prop] = params[prop];
}
return target;
})(defaults,param);
var result = [];
if(options.map[options.start[0]][options.start[1]]||options.map[options.end[0]][options.end[1]]){ //当起点或终点设置在墙上
return [];
}
var finded = false;
var length_x = options.map.length;
var steps = new Array(length_x); //步骤的映射
for(var x =0;x<length_x;x++){
var length_y = options.map[x].length;
steps[x] = new Array(length_y);
for(var y=0;y<length_y;y++){
steps[x][y] = 0;
}
}
var list = [[options.start[0],options.start[1],1000000]];
(function(index){
index++;
var current = list.pop();
var x = current[0],y = current[1];
var next = function(x,y){
if(typeof steps[x]!= 'undefined'&&typeof steps[x][y] != 'undefined'&&!options.map[x][y]&&!finded){ //当前点是否可以走
if(x==options.end[0]&&y==options.end[1]){
steps[x][y] = current;
finded = true;
}else if(!steps[x][y]){
var g = index + (options.end[0]-x)*(options.end[0]-x)+(options.end[1]-y)*(options.end[1]-y);
steps[x][y] = current;
list.push([x,y,g]);
}
}
};
next(x+1,y);
next(x,y+1);
next(x-1,y);
next(x,y-1);
if(!finded){
list.sort(function(a,b){
return b[2]-a[2];
});
arguments.callee(index);
}
})(0);
if(finded){
var current=options.end;
while(current[0]!=options.start[0]||current[1]!=options.start[1]){
current.length=2;
result.unshift(current);
current=steps[current[0]][current[1]];
}
}
return result;
}
</script>
<script type="text/javascript" src="https://passer-by.com/public/script/stat.js"></script>
</body>
</html>