@@ -37,7 +37,9 @@ var astar = {
37
37
return node . f ;
38
38
} ) ;
39
39
} ,
40
- search : function ( grid , start , end , diagonal , heuristic ) {
40
+ search : function ( grid , start , end , diagonal , heuristic , costStraight , costDiagonal ) {
41
+ costDiagonal = costDiagonal || 1 ;
42
+ costStraight = costStraight || 1 ;
41
43
astar . init ( grid ) ;
42
44
heuristic = heuristic || astar . manhattan ;
43
45
diagonal = ! ! diagonal ;
@@ -86,7 +88,7 @@ var astar = {
86
88
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
87
89
neighbor . visited = true ;
88
90
neighbor . parent = currentNode ;
89
- neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos ) ;
91
+ neighbor . h = neighbor . h || heuristic ( neighbor . pos , end . pos , costStraight , costDiagonal ) ;
90
92
neighbor . g = gScore ;
91
93
neighbor . f = neighbor . g + neighbor . h ;
92
94
@@ -112,6 +114,11 @@ var astar = {
112
114
var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
113
115
return d1 + d2 ;
114
116
} ,
117
+ diagonal : function ( pos0 , pos1 , D , D2 ) {
118
+ var d1 = Math . abs ( pos1 . x - pos0 . x ) ;
119
+ var d2 = Math . abs ( pos1 . y - pos0 . y ) ;
120
+ return ( D * ( d1 + d2 ) ) + ( ( D2 - ( 2 * D ) ) * Math . min ( d1 , d2 ) ) ;
121
+ } ,
115
122
neighbors : function ( grid , node , diagonals ) {
116
123
var ret = [ ] ;
117
124
var x = node . x ;
0 commit comments