Skip to content

Commit c5f2f20

Browse files
committed
use 1 and Math.sqrt(2) for costStraight and costDiagonal, rather than allowing options to be passed in. Issue bgrins#12
1 parent b5fd746 commit c5f2f20

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

astar.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ var astar = {
5252
options = options || {};
5353
var heuristic = options.heuristic || astar.manhattan;
5454
var diagonal = !!options.diagonal;
55-
var costDiagonal = options.costDiagonal || 1;
56-
var costStraight = options.costStraight || 1;
5755
var closest = options.closest || false;
5856

5957
var openHeap = astar.heap();
6058

6159
// set the start node to be the closest if required
6260
var closestNode = start;
6361

64-
start.h = heuristic(start.pos, end.pos, costStraight, costDiagonal);
62+
start.h = heuristic(start.pos, end.pos);
6563

6664
function pathTo(node){
6765
var curr = node;
@@ -110,7 +108,7 @@ var astar = {
110108
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
111109
neighbor.visited = true;
112110
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);
114112
neighbor.g = gScore;
115113
neighbor.f = neighbor.g + neighbor.h;
116114

@@ -150,10 +148,12 @@ var astar = {
150148
var d2 = Math.abs (pos1.y - pos0.y);
151149
return d1 + d2;
152150
},
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));
157157
},
158158
neighbors: function(grid, node, diagonals) {
159159
var ret = [];

0 commit comments

Comments
 (0)