Skip to content

Commit 11cb0e1

Browse files
committed
2 parents 301f343 + baf62d4 commit 11cb0e1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

astar.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ var astar = {
3737
return node.f;
3838
});
3939
},
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;
4143
astar.init(grid);
4244
heuristic = heuristic || astar.manhattan;
4345
diagonal = !!diagonal;
@@ -86,7 +88,7 @@ var astar = {
8688
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
8789
neighbor.visited = true;
8890
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);
9092
neighbor.g = gScore;
9193
neighbor.f = neighbor.g + neighbor.h;
9294

@@ -112,6 +114,11 @@ var astar = {
112114
var d2 = Math.abs (pos1.y - pos0.y);
113115
return d1 + d2;
114116
},
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+
},
115122
neighbors: function(grid, node, diagonals) {
116123
var ret = [];
117124
var x = node.x;

0 commit comments

Comments
 (0)