@@ -132,6 +132,19 @@ test( "GPS Pathfinding", function() {
132
132
CityNode . prototype . isWall = function ( ) {
133
133
return this . weight === 0 ;
134
134
} ;
135
+ // Heuristic function
136
+ CityNode . prototype . GPS_distance = function ( city ) {
137
+ var x = ( city . longRad - this . longRad ) * Math . cos ( ( this . latRad + city . latRad ) / 2 ) ,
138
+ y = city . latRad - this . latRad ,
139
+ res = Math . sqrt ( Math . pow ( x , 2 ) + Math . pow ( y , 2 ) ) * 6371 ;
140
+ return res ;
141
+ } ;
142
+ // Real cost function
143
+ CityNode . prototype . Real_distance = function ( city ) {
144
+ // Re-use heuristic function for now
145
+ // TODO: Determine the real distance between cities (from another data set)
146
+ return this . GPS_distance ( city ) ;
147
+ } ;
135
148
136
149
//---
137
150
@@ -147,23 +160,26 @@ test( "GPS Pathfinding", function() {
147
160
graph . cities = cities ;
148
161
graph . links = links ;
149
162
150
- var GPSheuristic = astar . heuristics . gps ;
151
-
152
- graph . neighbors = function ( node ) { // Override neighbors function for this specific graph
163
+ // Override neighbors function for this specific graph
164
+ graph . neighbors = function ( node ) {
153
165
var neighbors = [ ] ,
154
166
ids = this . links [ node . name ] ;
155
167
for ( var i = 0 , len = ids . length ; i < len ; ++ i ) {
156
168
var name = ids [ i ] ,
157
169
neighbor = this . cities [ name ] ;
158
- neighbor . cost = GPSheuristic ( node , neighbor ) ; // Compute real cost!
170
+ neighbor . cost = node . Real_distance ( neighbor ) ; // Compute real cost!
159
171
neighbors . push ( neighbor ) ;
160
172
}
161
173
return neighbors ;
162
174
} ;
163
175
164
176
var start = cities [ "Paris" ] ,
165
177
end = cities [ "Cannes" ] ;
166
-
178
+
179
+ var GPSheuristic = function ( node0 , node1 ) {
180
+ return node0 . GPS_distance ( node1 ) ;
181
+ } ;
182
+
167
183
var result = astar . search ( graph , start , end , { heuristic : GPSheuristic } ) ;
168
184
equal ( result . length , 3 , "Cannes is 3 cities away from Paris" ) ;
169
185
equal ( result [ 0 ] . name , "Lyon" , "City #1 is Lyon" ) ;
0 commit comments