Class GridPosition

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class GridPosition
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable
    Stores coordinates and has functions for grid calculations, e.g. getLine, ring and distance. These calculations do not depend on how you have placed the Hexagons on the HexagonMap. The axial coordinate system is used.
    See Also:
    Serialized Form
    • Field Detail

      • q

        protected final int q
        The Axial Q coordinate
      • r

        protected final int r
        The Axial R coordinate
    • Constructor Detail

      • GridPosition

        public GridPosition​(int q,
                            int r)
        Parameters:
        q - the axial Q coordinate
        r - the axial R coordinate
    • Method Detail

      • clone

        public GridPosition clone()
                           throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • equals

        public boolean equals​(java.lang.Object obj)
        Two positions are equal if they have the same q and r
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - object to compare to
        Returns:
        positions are equal if they have the same q and r
      • getCoordinates

        public java.lang.String getCoordinates()
      • getDirectionTo

        public HexagonMap.Direction getDirectionTo​(GridPosition otherPosition)
        Parameters:
        otherPosition - reference grid position
        Returns:
        the direction
      • getDistance

        public int getDistance​(GridPosition target)
      • getNeighborPosition

        public GridPosition getNeighborPosition​(HexagonMap.Direction direction)
        Finds the adjacent position in the specified direction from this position
        Parameters:
        direction - in which to search
        Returns:
        the adjacent position
      • getPositionsInCircleArea

        public java.util.List<GridPosition> getPositionsInCircleArea​(int radius)
      • getPositionsOnCircleEdge

        public java.util.List<GridPosition> getPositionsOnCircleEdge​(int radius)
        Finds all positions that are on the edge of a circle in which this position is the centre. If radius is 0, an array with only this GridPosition will be returned.
        Parameters:
        radius - circle radius
        Returns:
        positions that are on the edge of a circle
      • getQ

        public int getQ()
      • getR

        public int getR()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • isAdjacent

        public boolean isAdjacent​(GridPosition otherPosition)
        Parameters:
        otherPosition - other hex grid position
        Returns:
        true if the positions are adjacent
      • line

        public java.util.List<GridPosition> line​(GridPosition destination)
        Finds all GridPositions that are on a getLine between this and the given position (the array includes this and the destination positions)
        Parameters:
        destination - destination grid position
        Returns:
        an array positions
      • toString

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

        public static int getDistance​(GridPosition a,
                                      GridPosition b)
        Calculates the grid distance between two positions
        Parameters:
        a - the start position
        b - the destination position
        Returns:
        the distance (number of hexagons)
      • hexRound

        public static GridPosition hexRound​(double q,
                                            double r)
        Finds the position that best matches given non-integer coordinates
        Parameters:
        q - coordinate
        r - coordinate
        Returns:
        position that best matches given non-integer coordinates