ch.aplu.jgamegrid
Class Location

java.lang.Object
  extended by ch.aplu.jgamegrid.Location

public class Location
extends java.lang.Object

Class to represent the position of a cell (in cell coordinates). Directions are measured in degrees clockwise from the positive x-axis.


Nested Class Summary
static class Location.CompassDirection
          Class to represent the 8 compass directions.
 
Field Summary
static Location.CompassDirection EAST
          The compass direction for east.
static Location.CompassDirection NORTH
          The compass direction for north.
static Location.CompassDirection NORTHEAST
          The compass direction for northeast.
static Location.CompassDirection NORTHWEST
          The compass direction for northwest.
static Location.CompassDirection SOUTH
          The compass direction for south.
static Location.CompassDirection SOUTHEAST
          The compass direction for southeast.
static Location.CompassDirection SOUTHWEST
          The compass direction for southwest.
static Location.CompassDirection WEST
          The compass direction for west.
 int x
          The public horizontal coordinate (cell index) of the location.
 int y
          The public vertical coordinate (cell index) of the location.
 
Constructor Summary
Location()
          Constructs a location at (0, 0).
Location(int x, int y)
          Constructs a location with given horizontal and vertical cell coordinates.
Location(Location location)
          Constructs a location with the coordinates of the given location.
 
Method Summary
 Location clone()
          Returns a new location with duplicated coordinates.
 boolean equals(java.lang.Object obj)
          Checks whether the x-y-coordinates of the given location are equal to the x-y-coordinates of the current location (overrides Object.equals()).
 Location.CompassDirection get4CompassDirectionTo(Location location)
          Returns the compass direction restricted to 4 sectors from the current location the given location.
 Location getAdjacentLocation(double direction)
          Same as getAdjacentLocation(double direction, 5).
 Location getAdjacentLocation(double direction, int distance)
          Gets the adjacent location of a cell where a displacement arrow from the current center of the current cell with given direction and length = (distance + epsilon) * cellSize ends up.
 Location getAdjacentLocation(Location.CompassDirection compassDir)
          Same as getAdjacentLocation(double direction, 5) with given compass direction.
 Location getAdjacentLocation(Location.CompassDirection compassDir, int distance)
          Same as getAdjacentLocation(double direction, int distance) with given compass direction.
 Location.CompassDirection getCompassDirectionTo(Location location)
          Returns the compass direction restricted to 8 sectors from the current location the given location.
 double getDirectionTo(Location location)
          Returns the direction from the current location to the given location.
 int getDistanceTo(Location location)
          Returns the distance from the current location to the given location (in cellsize units, rounded to integer).
 Location getNeighbourLocation(double direction)
          Gets one of the 8 surrounding cells in given direction 45 degrees wide.
 Location getNeighbourLocation(Location.CompassDirection compassDir)
          Gets one of the 8 surrounding cells in the given compass directions.
 java.util.ArrayList<Location> getNeighbourLocations(double distance)
          Returns all locations in a specified distance.
 int getX()
          Gets the horizontal cell coordinate (index).
 int getY()
          Gets the vertical cell coordinate (index).
 int hashCode()
          Returns a hash code value for the object.
 java.lang.String toString()
          Returns a string that represents this location.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public int x
The public horizontal coordinate (cell index) of the location.


y

public int y
The public vertical coordinate (cell index) of the location.


EAST

public static final Location.CompassDirection EAST
The compass direction for east.


SOUTHEAST

public static final Location.CompassDirection SOUTHEAST
The compass direction for southeast.


SOUTH

public static final Location.CompassDirection SOUTH
The compass direction for south.


SOUTHWEST

public static final Location.CompassDirection SOUTHWEST
The compass direction for southwest.


WEST

public static final Location.CompassDirection WEST
The compass direction for west.


NORTHWEST

public static final Location.CompassDirection NORTHWEST
The compass direction for northwest.


NORTH

public static final Location.CompassDirection NORTH
The compass direction for north.


NORTHEAST

public static final Location.CompassDirection NORTHEAST
The compass direction for northeast.

Constructor Detail

Location

public Location()
Constructs a location at (0, 0).


Location

public Location(int x,
                int y)
Constructs a location with given horizontal and vertical cell coordinates.

Parameters:
x - the horizontal cell coordinate
y - the vertical cell coordinate

Location

public Location(Location location)
Constructs a location with the coordinates of the given location.

Parameters:
location - the location where to take the horizontal and vertical cell coordinates
Method Detail

getX

public int getX()
Gets the horizontal cell coordinate (index).

Returns:
the horizontal coordinate

getY

public int getY()
Gets the vertical cell coordinate (index).

Returns:
the vertical coordinate

getAdjacentLocation

public Location getAdjacentLocation(double direction,
                                    int distance)
Gets the adjacent location of a cell where a displacement arrow from the current center of the current cell with given direction and length = (distance + epsilon) * cellSize ends up. For distance == 1, epsilon is 0.3. This will give the neighbour cells in the 8 compass directions 45 degrees wide. For distance > 1, epsilon is -0.2.

Parameters:
direction - the direction in which to find a adjacent location
distance - the distance to the requested cell location in cell units.
Returns:
the adjacent location in the direction where the displacement arrow ends up

getAdjacentLocation

public Location getAdjacentLocation(Location.CompassDirection compassDir,
                                    int distance)
Same as getAdjacentLocation(double direction, int distance) with given compass direction.

Parameters:
compassDir - the compass direction in which to find a adjacent location
distance - the distance to the requested cell location
Returns:
the adjacent location in the direction where the displacement arrow ends up

getAdjacentLocation

public Location getAdjacentLocation(double direction)
Same as getAdjacentLocation(double direction, 5).

Parameters:
direction - the direction in which to find a adjacent location
Returns:
the adjacent location in the direction where the displacement arrow ends up

getAdjacentLocation

public Location getAdjacentLocation(Location.CompassDirection compassDir)
Same as getAdjacentLocation(double direction, 5) with given compass direction.

Parameters:
compassDir - the compass direction in which to find a adjacent location
Returns:
the adjacent location in the direction where the displacement arrow ends up

getNeighbourLocation

public Location getNeighbourLocation(double direction)
Gets one of the 8 surrounding cells in given direction 45 degrees wide.

Parameters:
direction - the direction in which to find a neighbour location
Returns:
one of the 8 neighbour cell locations

getNeighbourLocation

public Location getNeighbourLocation(Location.CompassDirection compassDir)
Gets one of the 8 surrounding cells in the given compass directions.

Parameters:
compassDir - the compass direction in which to find a neighbour location
Returns:
one of the 8 neighbour cell locations

getDirectionTo

public double getDirectionTo(Location location)
Returns the direction from the current location to the given location. If the location is the same as the current location, returns zero.

Parameters:
location - the target location
Returns:
the direction that points from the current location to the given location (0..360 degrees, clockwise, zero to east)

getDistanceTo

public int getDistanceTo(Location location)
Returns the distance from the current location to the given location (in cellsize units, rounded to integer).

Parameters:
location - the remote location
Returns:
the distance to the remote location (in cellsize units)

get4CompassDirectionTo

public Location.CompassDirection get4CompassDirectionTo(Location location)
Returns the compass direction restricted to 4 sectors from the current location the given location. If the location is the same as the current location, returns CompassDirection.WEST.

Parameters:
location - the target location
Returns:
the direction that points from the current location to the given location (0..360 degrees, clockwise, zero to east)

getCompassDirectionTo

public Location.CompassDirection getCompassDirectionTo(Location location)
Returns the compass direction restricted to 8 sectors from the current location the given location. If the location is the same as the current location, returns CompassDirection.WEST.

Parameters:
location - the target location
Returns:
the direction that points from the current location to the given location (0..360 degrees, clockwise, zero to east)

hashCode

public int hashCode()
Returns a hash code value for the object. In accordance with the general contract for hashCode(). (Code from JDK's Point class)

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Checks whether the x-y-coordinates of the given location are equal to the x-y-coordinates of the current location (overrides Object.equals()).

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object whose location is checked
Returns:
true if the given object is of class Location and has the same (x, y) as the current location; otherwise false

getNeighbourLocations

public java.util.ArrayList<Location> getNeighbourLocations(double distance)
Returns all locations in a specified distance. The distance defines a circle around the current cell center. All cells that intersects with this circle are returned. Also cells outside the visible grid are considered. To restrict this list to cells inside the grid, use GameGrid.isInGrid(). To get the 6 nearest neighbours, use distance = 1, to exclude diagonal locations, use distance = 0.5; The current location is not included.

Parameters:
distance - the distance in (fractional) cell units

toString

public java.lang.String toString()
Returns a string that represents this location.

Overrides:
toString in class java.lang.Object
Returns:
a string with horizontal and vertical coordinates of this location, in the format (x, y)

clone

public Location clone()
Returns a new location with duplicated coordinates.

Overrides:
clone in class java.lang.Object
Returns:
a clone of the current location