Package net.sf.freecol.common.model
Class Map.Position
- java.lang.Object
-
- net.sf.freecol.common.model.Map.Position
-
- Enclosing class:
- Map
public static final class Map.Position extends java.lang.Object
A position on the Map.
-
-
Constructor Summary
Constructors Constructor Description Position(int posX, int posY)
Creates a newPosition
object with the given coordinates.Position(Map.Position start, Direction direction)
Creates a newPosition
from an existing one with an optional step in a given direction.Position(Tile tile)
Creates a newPosition
object with the coordinates of a supplied tile.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o)
Direction
getDirection(Map.Position other)
Get the direction from this position to an adjacent position.int
getDistance(Map.Position position)
Gets the distance in tiles between two map positions.int
getX()
Gets the x-coordinate of this Position.static int
getXYDistance(int ax, int ay, int bx, int by)
Gets the distance in tiles between two map positions.int
getY()
Gets the y-coordinate of this Position.int
hashCode()
boolean
isValid(int width, int height)
Checks whether a position is valid within a given map size.java.lang.String
toString()
-
-
-
Constructor Detail
-
Position
public Position(int posX, int posY)
Creates a newPosition
object with the given coordinates.- Parameters:
posX
- The x-coordinate for this position.posY
- The y-coordinate for this position.
-
Position
public Position(Tile tile)
Creates a newPosition
object with the coordinates of a supplied tile.- Parameters:
tile
- TheTile
to extract coordinates from.
-
Position
public Position(Map.Position start, Direction direction)
Creates a newPosition
from an existing one with an optional step in a given direction.- Parameters:
start
- The startingPosition
.direction
- An optionalDirection
to step.
-
-
Method Detail
-
getX
public int getX()
Gets the x-coordinate of this Position.- Returns:
- The x-coordinate of this Position.
-
getY
public int getY()
Gets the y-coordinate of this Position.- Returns:
- The y-coordinate of this Position.
-
isValid
public boolean isValid(int width, int height)
Checks whether a position is valid within a given map size.- Parameters:
width
- The width of the map.height
- The height of the map.- Returns:
- True if the given position is within the bounds of the map.
-
getXYDistance
public static int getXYDistance(int ax, int ay, int bx, int by)
Gets the distance in tiles between two map positions. With an isometric map this is a non-trivial task. The formula below has been developed largely through trial and error. It should cover all cases, but I wouldn't bet my life on it.- Parameters:
ax
- The x-coordinate of the first position.ay
- The y-coordinate of the first position.bx
- The x-coordinate of the second position.by
- The y-coordinate of the second position.- Returns:
- The distance in tiles between the positions.
-
getDistance
public int getDistance(Map.Position position)
Gets the distance in tiles between two map positions. With an isometric map this is a non-trivial task. The formula below has been developed largely through trial and error. It should cover all cases, but I wouldn't bet my life on it.- Parameters:
position
- The otherPosition
to compare.- Returns:
- The distance in tiles to the other position.
-
getDirection
public Direction getDirection(Map.Position other)
Get the direction from this position to an adjacent position.- Parameters:
other
- The adjacentPosition
.- Returns:
- The
Direction
, or null if not adjacent.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-