Skip to content

Instantly share code, notes, and snippets.

@McFunkypants
Created January 16, 2013 04:53
Show Gist options
  • Save McFunkypants/4544738 to your computer and use it in GitHub Desktop.
Save McFunkypants/4544738 to your computer and use it in GitHub Desktop.
// distanceFunction functions
// these return how far away a point is to another
function ManhattanDistance(Point, Goal)
{ // linear movement - no diagonals - just cardinal directions (NSEW)
return abs(Point.x - Goal.x) + abs(Point.y - Goal.y);
}
function DiagonalDistance(Point, Goal)
{ // diagonal movement - assumes diag dist is 1, same as cardinals
return max(abs(Point.x - Goal.x), abs(Point.y - Goal.y));
}
function EuclideanDistance(Point, Goal)
{ // diagonals are considered a little farther than cardinal directions
// diagonal movement using Euclide (AC = sqrt(AB^2 + BC^2))
// where AB = x2 - x1 and BC = y2 - y1 and AC will be [x3, y3]
return sqrt(pow(Point.x - Goal.x, 2) + pow(Point.y - Goal.y, 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment