@@ -52,16 +52,14 @@ var astar = {
52
52
options = options || { } ;
53
53
var heuristic = options . heuristic || astar . manhattan ;
54
54
var diagonal = ! ! options . diagonal ;
55
- var costDiagonal = options . costDiagonal || 1 ;
56
- var costStraight = options . costStraight || 1 ;
57
55
var closest = options . closest || false ;
58
56
59
57
var openHeap = astar . heap ( ) ;
60
58
61
59
// set the start node to be the closest if required
62
60
var closestNode = start ;
63
61
64
- start . h = heuristic ( start . pos , end . pos , costStraight , costDiagonal ) ;
62
+ start . h = heuristic ( start . pos , end . pos ) ;
65
63
66
64
function pathTo ( node ) {
67
65
var curr = node ;
@@ -110,7 +108,7 @@ var astar = {
110
108
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
111
109
neighbor . visited = true ;
112
110
neighbor . parent = currentNode ;
113
- neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos , costStraight , costDiagonal ) ;
111
+ neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos ) ;
114
112
neighbor . g = gScore ;
115
113
neighbor . f = neighbor . g + neighbor . h ;
116
114
@@ -150,10 +148,12 @@ var astar = {
150
148
var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
151
149
return d1 + d2 ;
152
150
} ,
153
- diagonal : function ( pos0 , pos1 , D , D2 ) {
154
- var d1 = Math . abs ( pos1 . x - pos0 . x ) ;
155
- var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
156
- return ( D * ( d1 + d2 ) ) + ( ( D2 - ( 2 * D ) ) * Math . min ( d1 , d2 ) ) ;
151
+ diagonal : function ( pos0 , pos1 ) {
152
+ var D = 1 ;
153
+ var D2 = Math . sqrt ( 2 ) ;
154
+ var d1 = Math . abs ( pos1 . x - pos0 . x ) ;
155
+ var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
156
+ return ( D * ( d1 + d2 ) ) + ( ( D2 - ( 2 * D ) ) * Math . min ( d1 , d2 ) ) ;
157
157
} ,
158
158
neighbors : function ( grid , node , diagonals ) {
159
159
var ret = [ ] ;
0 commit comments