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

      • getQ

        public int getQ()
      • getR

        public int getR()
      • 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
      • 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
      • getPositionsInCircleArea

        public java.util.List<GridPosition> getPositionsInCircleArea​(int radius)
      • 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
      • clone

        protected GridPosition clone()
        Overrides:
        clone in class java.lang.Object
      • 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
      • 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
      • getDirectionTo

        public HexagonMap.Direction getDirectionTo​(GridPosition otherPosition)
        Parameters:
        otherPosition - reference grid position
        Returns:
        the direction
      • 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
      • 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)
      • getDistance

        public int getDistance​(GridPosition target)
      • toString

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