megamek.common
Class Coords

java.lang.Object
  extended by megamek.common.Coords
All Implemented Interfaces:
java.io.Serializable

public class Coords
extends java.lang.Object
implements java.io.Serializable

Coords stores x and y values. Since these are hexes, coordinates with odd x values are a half-hex down. Directions work clockwise around the hex, starting with zero at the top. -y 0 _____ 5 / \ 1 -x / \ +x \ / 4 \_____/ 2 3 +y

See Also:
Serialized Form

Field Summary
 double fitness
           
static double HEXSIDE
           
static int MAX_BOARD_HEIGHT
          The maximum height of a board in number of hexes.
static int MAX_BOARD_WIDTH
          The maximum width of a board in number of hexes.
 int x
           
 int y
           
 
Constructor Summary
Coords()
          Constructs a new coordinate pair at (0, 0).
Coords(Coords c)
          Constructs a new coordinate pair that is a duplicate of the parameter.
Coords(int x, int y)
          Constructs a new coordinate pair at (x, y).
 
Method Summary
 int degree(Coords d)
          Returns the degree direction of another Coords.
 int direction(Coords d)
          Returns the direction in which another coordinate lies; 0 if the coordinates are equal.
 int distance(Coords c)
          Returns the distance to another coordinate.
 int distance(int distx, int disty)
           
 boolean equals(java.lang.Object object)
          Coords are equal if their x and y components are equal
 java.lang.String getBoardNum()
          Returns a string representing a coordinate in "board number" format.
static Coords getFromHashCode(int hash)
          Get the coordinates object for a given hash code.
 int hashCode()
          Get the hash code for these coords.
static java.util.ArrayList<Coords> intervening(Coords src, Coords dest)
          Returns an array of the Coords of hexes that are crossed by a straight line from the center of src to the center of dest, including src & dest.
static java.util.ArrayList<Coords> intervening(Coords src, Coords dest, boolean split)
           
 boolean isXOdd()
          Tests whether the x coordinate of this coordinate is odd.
static Coords nextHex(Coords current, Coords destination)
          Pass-thru version of the above that assumes current = iSrc.
static Coords nextHex(Coords current, IdealHex iSrc, IdealHex iDest, int[] directions)
          Returns the first further hex found along the line from the centers of src to dest.
 double radian(Coords d)
          Returns the radian direction of another Coords.
 java.lang.String toFriendlyString()
          this makes the coordinates 1 based instead of 0 based to match the tiles diaplayed on the grid.
 java.lang.String toString()
           
 Coords translated(int dir)
          Returns a new coordinate that represents the coordinate 1 unit in the specified direction.
 Coords translated(int dir, int distance)
           
 Coords translated(java.lang.String dir)
           
static int xInDir(int x, int y, int dir)
          Returns the x parameter of the coordinates in the direction
static int xInDir(int x, int y, int dir, int distance)
           
static int yInDir(int x, int y, int dir)
          Returns the y parameter of the coordinates in the direction
static int yInDir(int x, int y, int dir, int distance)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

HEXSIDE

public static final double HEXSIDE
See Also:
Constant Field Values

x

public int x

y

public int y

fitness

public double fitness

MAX_BOARD_HEIGHT

public static final int MAX_BOARD_HEIGHT
The maximum height of a board in number of hexes.

See Also:
Constant Field Values

MAX_BOARD_WIDTH

public static final int MAX_BOARD_WIDTH
The maximum width of a board in number of hexes. Also need to make room for the sign bits.

See Also:
Constant Field Values
Constructor Detail

Coords

public Coords()
Constructs a new coordinate pair at (0, 0).


Coords

public Coords(int x,
              int y)
Constructs a new coordinate pair at (x, y).


Coords

public Coords(Coords c)
Constructs a new coordinate pair that is a duplicate of the parameter.

Method Detail

translated

public final Coords translated(int dir)
Returns a new coordinate that represents the coordinate 1 unit in the specified direction.

Parameters:
dir - the direction.
Returns:
the new coordinate, if the direction is valid; otherwise, a new copy of this coordinate.

translated

public final Coords translated(int dir,
                               int distance)

translated

public final Coords translated(java.lang.String dir)

xInDir

public static final int xInDir(int x,
                               int y,
                               int dir)
Returns the x parameter of the coordinates in the direction


xInDir

public static final int xInDir(int x,
                               int y,
                               int dir,
                               int distance)

yInDir

public static final int yInDir(int x,
                               int y,
                               int dir)
Returns the y parameter of the coordinates in the direction


yInDir

public static final int yInDir(int x,
                               int y,
                               int dir,
                               int distance)

isXOdd

public boolean isXOdd()
Tests whether the x coordinate of this coordinate is odd. This is significant in determining where this coordinate lies in relation to other coordinates.


direction

public int direction(Coords d)
Returns the direction in which another coordinate lies; 0 if the coordinates are equal.

Parameters:
d - the destination coordinate.

radian

public double radian(Coords d)
Returns the radian direction of another Coords.

Parameters:
d - the destination coordinate.

degree

public int degree(Coords d)
Returns the degree direction of another Coords.

Parameters:
d - the destination coordinate.

distance

public int distance(Coords c)
Returns the distance to another coordinate.


distance

public int distance(int distx,
                    int disty)

getBoardNum

public final java.lang.String getBoardNum()
Returns a string representing a coordinate in "board number" format.


equals

public boolean equals(java.lang.Object object)
Coords are equal if their x and y components are equal

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Get the hash code for these coords.

Overrides:
hashCode in class java.lang.Object
Returns:
The int hash code for these coords.

getFromHashCode

public static Coords getFromHashCode(int hash)
Get the coordinates object for a given hash code.

Parameters:
hash - - the hash code for the desired object.
Returns:
the Coords that match the hash code.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

intervening

public static java.util.ArrayList<Coords> intervening(Coords src,
                                                      Coords dest)
Returns an array of the Coords of hexes that are crossed by a straight line from the center of src to the center of dest, including src & dest. The returned coordinates are in line order, and if the line passes directly between two hexes, it returns them both. Based on the degree of the angle, the next hex is going to be one of three hexes. We check those three hexes, sides first, add the first one that intersects and continue from there. Based off of some of the formulas at Amit's game programming site. (http://www-cs-students.stanford.edu/~amitp/gameprog.html)


intervening

public static java.util.ArrayList<Coords> intervening(Coords src,
                                                      Coords dest,
                                                      boolean split)

nextHex

public static Coords nextHex(Coords current,
                             IdealHex iSrc,
                             IdealHex iDest,
                             int[] directions)
Returns the first further hex found along the line from the centers of src to dest. Checks the three directions given and returns the closest. This relies on the side directions being given first. If it checked the center first, it would end up missing the side hexes sometimes. Not the most elegant solution, but it works.


nextHex

public static Coords nextHex(Coords current,
                             Coords destination)
Pass-thru version of the above that assumes current = iSrc.


toFriendlyString

public java.lang.String toFriendlyString()
this makes the coordinates 1 based instead of 0 based to match the tiles diaplayed on the grid.

Returns: