Skip to content

Commit

Permalink
Use different heuristics based on 4 vs 8 directions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce Neal committed May 16, 2016
1 parent e36a256 commit 5f9ca7a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/easystar.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,20 @@ EasyStar.js = function() {
};

var getDistance = function(x1,y1,x2,y2) {
return Math.sqrt( (x2-=x1)*x2 + (y2-=y1)*y2 );
if (diagonalsEnabled) {
// Octile distance
var dx = Math.abs(x1 - x2);
var dy = Math.abs(y1 - y2);
if (dx < dy) {
return DIAGONAL_COST * dx + dy;
} else {
return DIAGONAL_COST * dy + dx;
}
} else {
// Manhattan distance
var dx = Math.abs(x1 - x2);
var dy = Math.abs(y1 - y2);
return (dx + dy)
}
};
}

0 comments on commit 5f9ca7a

Please sign in to comment.