Dominic Charley-Roy
https://github.com/dominiccharleyroy
dominic.charley-roy @ mail.mcgill


This is a formula for determining the distance between two points. You can think of this as if you drew a straight line between two points and measured it with a ruler.

Given the points (x1, y1) and (x2, y2), the distance is calculated by:
double distance = Math.sqrt(
Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));This can now help us answer questions such as Is Bomberman within the distance d from an enemy.
As the distance is initially a double, it is common to round it to work with it as an integer. Then we can check if distance <= 3. If we left it as a double, we have to worry about accuracy.
This is a different metric for determining the distance between two points. Since we are working on a 2D grid, we can think of distance like we would in a regular city.
If a taxicab needs to drive up 3 blocks and then right 4 blocks to get you to your house, the distance is 7!

Given two points (x1, y1) and (x2, y2) the taxicab distance is calculated:
int distance = Math.abs(x1 - x2) + Math.abs(y1 - y2);Note: Taxicab distance gives you the distance as an int!
Also note that Taxicab distance only really makes sense if your points are integers (eg. no 10.5)
A*
Points are evaluated based on the sum of a "past" score and a "future" score.
score = pastDistance + futureDistance;List<Point> getShortestPath(startX, startY, endX, endY) {
// Create the queue of candidates.
PriorityQueue<Point> candidates = new PriorityQueue<Point>(
new PointComparator(endX, endY));
// Create a boolean 2D array representing if a given point
// is in the closed list.
boolean[][] closed = new boolean[sizeX][sizeY];
// Add the starting point to the list of candidates.
candidates.add(new Point(startX, startY, null, 0));
// ...
}// Keep going until there are no candidates left.
while (!candidates.isEmpty()) {
// Get the best candidate.
Point candidate = candidates.poll();
// If the candidate is the end point, we've found the
// shortest path!
if (candidate.x == endX && candidate.y == endY)
return buildPath(candidate);
// If the candidate is already closed, ignore it.
if (closed[candidate.x][candidate.y])
continue;
// Look at each neighbor of the candidate.
for (Point neighbor : candidate.getNeighbors()) {
// If the neighbor is closed or cannot be walked to,
// it is ignored.
if (blocked[neighbor.x][neighbor.y] ||
closed[neighbor.x][neighbor.y])
continue;
// Add the candidate.
candidates.add(neighbor);
}
// Close the candidate.
closed[candidate.x][candidate.y];
}
// No path found!
return null;We have to create a comparator specifically tailored to our end point to calculate the distance!
public class TestComparator implements Comparator<Point> {
public TestComparator(int endX, int endY) {
// Create a comparator based on a given end point.
this.endX = endX;
this.endY = endY;
}
public int compare(Point p1, Point p2) {
// Calculate past + future score for both points.
int score1 = p1.pastScore + distance(p1.x, p1.y, endX, endY);
int score2 = p2.pastScore + distance(p2.x, p2.y, endX, endY);
// Point 1 should be before Point 2 if score1 has a lower value.
return score1 - score2;
}
public int distance(x1, y1, x2, y2) {
// Insert distance function here!
}
}
public class Point {
// ...
public List<Point> getNeighbors() {
List<Point> points = new ArrayList<>();
// Up direction
// Check if this point is within the boundaries of the map.
if (this.y > 0) {
// Create the up neighbor with the proper coordinates.
// The neighbor will have this point as its parent and
// takes the pastScore and adds 1 to represent the move.
Point upNeighbor = new Point(
this.x, this.y - 1,
this, this.pastScore + 1);
points.add(upNeighbor);
}
// Other directions...
return points
}
}