@@ -10,43 +10,38 @@ window.log = function(){
10
10
}
11
11
} ;
12
12
13
- var WALL = 0 ;
14
- var OPEN = 1 ;
13
+ var WALL = 0 ,
14
+ OPEN = 1 ;
15
15
16
16
var generateRandom = function ( width , height , wallFrequency ) {
17
-
18
17
var nodes = [ ] ;
18
+ for ( var x = 0 ; x < width ; x ++ ) {
19
+ var nodeRow = [ ] ,
20
+ gridRow = [ ] ;
19
21
20
- for ( var x = 0 ; x < width ; x ++ ) {
21
- var nodeRow = [ ] ;
22
- var gridRow = [ ] ;
23
-
24
- for ( var y = 0 ; y < height ; y ++ ) {
25
-
22
+ for ( var y = 0 ; y < height ; y ++ ) {
26
23
var isWall = Math . floor ( Math . random ( ) * ( 1 / wallFrequency ) ) ;
27
- if ( isWall == 0 ) {
24
+ if ( isWall == 0 ) {
28
25
nodeRow . push ( WALL ) ;
29
26
}
30
- else {
27
+ else {
31
28
nodeRow . push ( OPEN ) ;
32
29
}
33
30
}
34
31
nodes . push ( nodeRow ) ;
35
32
}
36
33
37
-
38
34
return new Graph ( nodes ) ;
39
35
} ;
40
36
41
37
$ ( function ( ) {
42
38
43
- var $grid = $ ( "#search_grid" ) ;
44
- var $selectWallFrequency = $ ( "#selectWallFrequency" ) ;
45
- var $selectGridSize = $ ( "#selectGridSize" ) ;
46
- var $checkDebug = $ ( "#checkDebug" ) ;
47
- var $searchDiagonal = $ ( "#searchDiagonal" ) ;
48
- var $checkClosest = $ ( "#checkClosest" ) ;
49
-
39
+ var $grid = $ ( "#search_grid" ) ,
40
+ $selectWallFrequency = $ ( "#selectWallFrequency" ) ,
41
+ $selectGridSize = $ ( "#selectGridSize" ) ,
42
+ $checkDebug = $ ( "#checkDebug" ) ,
43
+ $searchDiagonal = $ ( "#searchDiagonal" ) ,
44
+ $checkClosest = $ ( "#checkClosest" ) ;
50
45
51
46
var opts = {
52
47
wallFrequency : $selectWallFrequency . val ( ) ,
@@ -111,28 +106,26 @@ GraphSearch.prototype.setOption = function(opt) {
111
106
}
112
107
} ;
113
108
GraphSearch . prototype . initialize = function ( ) {
114
-
115
- var self = this ;
116
109
this . grid = [ ] ;
117
- var nodes = [ ] ;
118
- var $graph = this . $graph ;
110
+ var self = this ,
111
+ nodes = [ ] ,
112
+ $graph = this . $graph ;
119
113
120
114
$graph . empty ( ) ;
121
115
122
- var cellWidth = ( $graph . width ( ) / this . opts . gridSize ) - 2 ; // -2 for border
123
- var cellHeight = ( $graph . height ( ) / this . opts . gridSize ) - 2 ;
124
- var $cellTemplate = $ ( "<span />" ) . addClass ( "grid_item" ) . width ( cellWidth ) . height ( cellHeight ) ;
125
- var startSet = false ;
116
+ var cellWidth = ( $graph . width ( ) / this . opts . gridSize ) - 2 , // -2 for border
117
+ cellHeight = ( $graph . height ( ) / this . opts . gridSize ) - 2 ,
118
+ $cellTemplate = $ ( "<span />" ) . addClass ( "grid_item" ) . width ( cellWidth ) . height ( cellHeight ) ,
119
+ startSet = false ;
126
120
127
- for ( var x = 0 ; x < this . opts . gridSize ; x ++ ) {
128
- var $row = $ ( "<div class='clear' />" ) ;
121
+ for ( var x = 0 ; x < this . opts . gridSize ; x ++ ) {
122
+ var $row = $ ( "<div class='clear' />" ) ,
123
+ nodeRow = [ ] ,
124
+ gridRow = [ ] ;
129
125
130
- var nodeRow = [ ] ;
131
- var gridRow = [ ] ;
132
-
133
- for ( var y = 0 ; y < this . opts . gridSize ; y ++ ) {
134
- var id = "cell_" + x + "_" + y ;
135
- var $cell = $cellTemplate . clone ( ) ;
126
+ for ( var y = 0 ; y < this . opts . gridSize ; y ++ ) {
127
+ var id = "cell_" + x + "_" + y ,
128
+ $cell = $cellTemplate . clone ( ) ;
136
129
$cell . attr ( "id" , id ) . attr ( "x" , x ) . attr ( "y" , y ) ;
137
130
$row . append ( $cell ) ;
138
131
gridRow . push ( $cell ) ;
@@ -146,7 +139,9 @@ GraphSearch.prototype.initialize = function() {
146
139
var cell_weight = ( $ ( "#generateWeights" ) . prop ( "checked" ) ? ( Math . floor ( Math . random ( ) * 3 ) ) * 2 + 1 : 1 ) ;
147
140
nodeRow . push ( cell_weight ) ;
148
141
$cell . addClass ( 'weight' + cell_weight ) ;
149
- if ( $ ( "#displayWeights" ) . prop ( "checked" ) ) { $cell . html ( cell_weight ) } ;
142
+ if ( $ ( "#displayWeights" ) . prop ( "checked" ) ) {
143
+ $cell . html ( cell_weight ) ;
144
+ }
150
145
if ( ! startSet ) {
151
146
$cell . addClass ( css . start ) ;
152
147
startSet = true ;
@@ -163,7 +158,9 @@ GraphSearch.prototype.initialize = function() {
163
158
164
159
// bind cell event, set start/wall positions
165
160
this . $cells = $graph . find ( ".grid_item" ) ;
166
- this . $cells . click ( function ( ) { self . cellClicked ( $ ( this ) ) } ) ;
161
+ this . $cells . click ( function ( ) {
162
+ self . cellClicked ( $ ( this ) ) ;
163
+ } ) ;
167
164
} ;
168
165
GraphSearch . prototype . cellClicked = function ( $end ) {
169
166
@@ -176,17 +173,18 @@ GraphSearch.prototype.cellClicked = function($end) {
176
173
177
174
this . $cells . removeClass ( css . finish ) ;
178
175
$end . addClass ( "finish" ) ;
179
- var $start = this . $cells . filter ( "." + css . start ) ;
180
- var start = this . nodeFromElement ( $start ) ;
176
+ var $start = this . $cells . filter ( "." + css . start ) ,
177
+ start = this . nodeFromElement ( $start ) ;
181
178
182
179
var sTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
180
+
183
181
var path = this . search ( this . graph , start , end , {
184
182
closest : this . opts . closest
185
183
} ) ;
186
- var fTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
187
- var duration = ( fTime - sTime ) . toFixed ( 2 ) ;
184
+ var fTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ,
185
+ duration = ( fTime - sTime ) . toFixed ( 2 ) ;
188
186
189
- if ( ! path || path . length == 0 ) {
187
+ if ( ! path || path . length == 0 ) {
190
188
$ ( "#message" ) . text ( "couldn't find a path (" + duration + "ms)" ) ;
191
189
this . animateNoPath ( ) ;
192
190
}
@@ -203,8 +201,8 @@ GraphSearch.prototype.drawDebugInfo = function(show) {
203
201
var that = this ;
204
202
if ( show ) {
205
203
that . $cells . each ( function ( i ) {
206
- var node = that . nodeFromElement ( $ ( this ) ) ;
207
- var debug = false ;
204
+ var node = that . nodeFromElement ( $ ( this ) ) ,
205
+ debug = false ;
208
206
if ( node . visited ) {
209
207
debug = "F: " + node . f + "<br />G: " + node . g + "<br />H: " + node . h ;
210
208
}
@@ -213,7 +211,6 @@ GraphSearch.prototype.drawDebugInfo = function(show) {
213
211
$ ( this ) . html ( debug ) ;
214
212
}
215
213
} ) ;
216
-
217
214
}
218
215
} ;
219
216
GraphSearch . prototype . nodeFromElement = function ( $cell ) {
@@ -222,18 +219,20 @@ GraphSearch.prototype.nodeFromElement = function($cell) {
222
219
GraphSearch . prototype . animateNoPath = function ( ) {
223
220
var $graph = this . $graph ;
224
221
var jiggle = function ( lim , i ) {
225
- if ( i >= lim ) { $graph . css ( "top" , 0 ) . css ( "left" , 0 ) ; return ; }
222
+ if ( i >= lim ) { $graph . css ( "top" , 0 ) . css ( "left" , 0 ) ; return ; }
226
223
if ( ! i ) i = 0 ;
227
224
i ++ ;
228
225
$graph . css ( "top" , Math . random ( ) * 6 ) . css ( "left" , Math . random ( ) * 6 ) ;
229
- setTimeout ( function ( ) { jiggle ( lim , i ) } , 5 ) ;
226
+ setTimeout ( function ( ) {
227
+ jiggle ( lim , i ) ;
228
+ } , 5 ) ;
230
229
} ;
231
230
jiggle ( 15 ) ;
232
231
} ;
233
232
GraphSearch . prototype . animatePath = function ( path ) {
234
- var grid = this . grid ;
235
- var timeout = 1000 / grid . length ;
236
- var elementFromNode = function ( node ) {
233
+ var grid = this . grid ,
234
+ timeout = 1000 / grid . length ,
235
+ elementFromNode = function ( node ) {
237
236
return grid [ node . x ] [ node . y ] ;
238
237
} ;
239
238
@@ -244,25 +243,27 @@ GraphSearch.prototype.animatePath = function(path) {
244
243
return setStartClass ( path , i ) ;
245
244
}
246
245
elementFromNode ( path [ i ] ) . removeClass ( css . active ) ;
247
- setTimeout ( function ( ) { removeClass ( path , i + 1 ) } , timeout * path [ i ] . cost ) ;
248
- }
249
- var setStartClass = function ( path , i ) {
250
- if ( i === path . length ) {
246
+ setTimeout ( function ( ) {
247
+ removeClass ( path , i + 1 ) ;
248
+ } , timeout * path [ i ] . cost ) ;
249
+ } ;
250
+ var setStartClass = function ( path , i ) {
251
+ if ( i === path . length ) {
251
252
self . $graph . find ( "." + css . start ) . removeClass ( css . start ) ;
252
253
elementFromNode ( path [ i - 1 ] ) . addClass ( css . start ) ;
253
254
}
254
- }
255
- var addClass = function ( path , i ) {
255
+ } ;
256
+ var addClass = function ( path , i ) {
256
257
if ( i >= path . length ) { // Finished showing path, now remove
257
258
return removeClass ( path , 0 ) ;
258
259
}
259
260
elementFromNode ( path [ i ] ) . addClass ( css . active ) ;
260
- setTimeout ( function ( ) { addClass ( path , i + 1 ) } , timeout * path [ i ] . cost ) ;
261
+ setTimeout ( function ( ) {
262
+ addClass ( path , i + 1 ) ;
263
+ } , timeout * path [ i ] . cost ) ;
261
264
} ;
262
265
263
- addClass ( path , 0 )
266
+ addClass ( path , 0 ) ;
264
267
this . $graph . find ( "." + css . start ) . removeClass ( css . start ) ;
265
268
this . $graph . find ( "." + css . finish ) . removeClass ( css . finish ) . addClass ( css . start ) ;
266
269
} ;
267
-
268
-
0 commit comments