RuneHive-Game
Loading...
Searching...
No Matches
Distance.java
Go to the documentation of this file.
1package com.runehive.game.world.pathfinding.distance;
2
3import com.runehive.game.world.position.Position;
4
5/**
6 * An interface to calculate the distance between two nodes in a {@link
7 * Position}
8 *
9 * @author Artem Batutin <artembatutin@gmail.com>
10 */
11public interface Distance {
12
13 /**
14 * Calculates the heuristic value of the defined two positions.
15 *
16 * @param from The first position from which we calculate.
17 * @param to The second position to where we calculate.
18 * @return Distance value of the two definitions.
19 */
21
22 /**
23 * The Chebyshev heuristic, ideal for a system that allows for 8-directional
24 * movement.
25 *
26 * @author Artem Batutin <artembatutin@gmail.com>
27 */
28 final class Chebyshev implements Distance {
29
30 @Override
31 public int calculate(Position to, Position from) {
32 int dx = Math.abs(from.getX() - to.getX());
33 int dy = Math.abs(from.getX() - to.getY());
34 return dx >= dy ? dx : dy;
35 }
36 }
37
38 /**
39 * Since Euclidean distance is shorter than Manhattan or diagonal distance,
40 * you will still get shortest paths, but the pathfinder will take longer to
41 * run.
42 *
43 * @author Artem Batutin <artembatutin@gmail.com>
44 */
45 class Euclidean implements Distance {
46
47 @Override
48 public int calculate(Position to, Position from) {
49 int deltaX = from.getX() - to.getX();
50 int deltaY = from.getY() - to.getY();
51 return (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
52 }
53
54 }
55
56 /**
57 * The Manhattan Distance is the distance between two points measured along
58 * axes at right angles. The name alludes to the grid layout of the streets
59 * of Manhattan, which causes the shortest path a car could take between two
60 * points in the city. Perfect for 4 dimensional movements.
61 *
62 * @author Artem Batutin <artembatutin@gmail.com>
63 */
64 class Manhattan implements Distance {
65
66 @Override
67 public int calculate(Position to, Position from) {
68 int deltaX = Math.abs(from.getX() - to.getX());
69 int deltaY = Math.abs(from.getY() - to.getY());
70 return deltaX + deltaY;
71 }
72
73 }
74}
The Chebyshev heuristic, ideal for a system that allows for 8-directional movement.
Definition Distance.java:28
int calculate(Position to, Position from)
Calculates the heuristic value of the defined two positions.
Definition Distance.java:31
Since Euclidean distance is shorter than Manhattan or diagonal distance, you will still get shortest ...
Definition Distance.java:45
int calculate(Position to, Position from)
Calculates the heuristic value of the defined two positions.
Definition Distance.java:48
The Manhattan Distance is the distance between two points measured along axes at right angles.
Definition Distance.java:64
int calculate(Position to, Position from)
Calculates the heuristic value of the defined two positions.
Definition Distance.java:67
Represents a single tile on the game world.
Definition Position.java:14
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
An interface to calculate the distance between two nodes in a Position.
Definition Distance.java:11
int calculate(Position from, Position to)
Calculates the heuristic value of the defined two positions.