Class CubeCoordinate


  • public class CubeCoordinate
    extends java.lang.Object
    CubeCoordinate/CubeVector: This class can be used to compute Coordinates or Vectors in a hexagon world. This Coordinate consists of 3 components: q, r, s. This components represent three vector in cartesian, rotated by 180° each. To get a unique Vector all components MUST full fill this condition: q+r+s = 0.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  CubeCoordinate.AreaIterator
      An AreaIterator iterates over all tiles in a filled hexagon with a center and radius.
      class  CubeCoordinate.RingIterator
      The RingIterator is a Iterator and Iterable over CubeCoordinate's.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double EDGE_LENGTH
      Edge length of a tile in cartesian coordinates = 1 / SQRT(3)
      int q
      q component of this coordinate/vector (note: q+r+s = 0)
      static Vec2 Q_VEC
      cartesian vector of the q component of a vector, with q=1
      int r
      r component of this coordinate/vector (note: q+r+s = 0)
      static Vec2 R_VEC
      cartesian vector of the r component of a vector, with r=1
      int s
      s component of this coordinate/vector (note: q+r+s = 0)
      static Vec2 S_VEC
      cartesian vector of the s component of a vector, with s=1
      static double SQRT_3
      square root of 3
      static double TILE_SIZE
      Distance between tow tiles centers, connecting on there edges.
    • Constructor Summary

      Constructors 
      Constructor Description
      CubeCoordinate()
      Creates a 0 CubeCoordinate or a 0 CubeVector.
      CubeCoordinate​(int q, int r, int s)
      Creates a CubeCoordinate/CubeVector with coordinates.
      CubeCoordinate​(CubeCoordinate current)
      Copy/clone a CubeCoordinate/CubeVector.
    • Field Detail

      • SQRT_3

        public static double SQRT_3
        square root of 3
      • EDGE_LENGTH

        public static double EDGE_LENGTH
        Edge length of a tile in cartesian coordinates = 1 / SQRT(3)
      • TILE_SIZE

        public static double TILE_SIZE
        Distance between tow tiles centers, connecting on there edges.
      • Q_VEC

        public static Vec2 Q_VEC
        cartesian vector of the q component of a vector, with q=1
      • R_VEC

        public static Vec2 R_VEC
        cartesian vector of the r component of a vector, with r=1
      • S_VEC

        public static Vec2 S_VEC
        cartesian vector of the s component of a vector, with s=1
      • q

        public int q
        q component of this coordinate/vector (note: q+r+s = 0)
      • r

        public int r
        r component of this coordinate/vector (note: q+r+s = 0)
      • s

        public int s
        s component of this coordinate/vector (note: q+r+s = 0)
    • Constructor Detail

      • CubeCoordinate

        public CubeCoordinate()
        Creates a 0 CubeCoordinate or a 0 CubeVector. (can be used as both)
      • CubeCoordinate

        public CubeCoordinate​(int q,
                              int r,
                              int s)
        Creates a CubeCoordinate/CubeVector with coordinates. The following condition MUST be true: q+r+s = 0. (will be checked in debug mode)
        Parameters:
        q - q component
        r - r component
        s - s component
      • CubeCoordinate

        public CubeCoordinate​(CubeCoordinate current)
        Copy/clone a CubeCoordinate/CubeVector.
        Parameters:
        current - coordinate to copy
    • Method Detail

      • hexLen

        public int hexLen()
        Returns the size that a hexagon area, centered around (0,0,0), would have to be, to contain the Coordinate.
        Returns:
        size
      • distance

        public int distance​(CubeCoordinate coord)
        Returns the hex len between two coordinates. See hexLen.
        Parameters:
        coord - coordinate to compare to
        Returns:
        distance between the coords
      • getRing

        public CubeCoordinate.RingIterator getRing​(int radius)
        Creates Iterator iterating all coordinates on the outline of an hexagon. The radius does define the size of a hexagon.
        Parameters:
        radius - size of the hexagon, min 0
        Returns:
        ring iterator
      • getArea

        public CubeCoordinate.AreaIterator getArea​(int radius)
        Creates Iterator iterating all coordinates in a given radius. The radius does define the size of a hexagon.
        Parameters:
        radius - size of the hexagon, min 1
        Returns:
        area iterator
      • toVec

        public Vec2 toVec()
        Converts this CubeCoordinate/CubeVector to cartesian coordinates.
        Returns:
        cartesian vector
      • unitFromDirection

        public static CubeCoordinate unitFromDirection​(int dir)
        Create a unit CubeCoordinate Vector from direction index.
        Parameters:
        dir - value between 0 and 5
        Returns:
        requested unit vector.
      • add

        public CubeCoordinate add​(CubeCoordinate other)
        Add CubeVector, to this CubeCoordinate/CubeVector. (this nor other will not be modified)
        Parameters:
        other - other CubeCoordinate/CubeVector
        Returns:
        added copy
      • sub

        public CubeCoordinate sub​(CubeCoordinate other)
        Subtract CubeVector, to this CubeCoordinate/CubeVector. (this nor other will not be modified)
        Parameters:
        other - other CubeCoordinate/CubeVector
        Returns:
        subtracted copy
      • mul

        public CubeCoordinate mul​(int scale)
        Multiply CubeVector by a scalar value. (this will not be modified)
        Parameters:
        scale - value to multiply ba
        Returns:
        multiplied copy
      • negate

        public CubeCoordinate negate()
        Create a negated CubeVector. (this will not be modified)
        Returns:
        negated copy
      • div

        public CubeCoordinate div​(int scale)
        Divide this CubeVector be scalar value. (this will not be modified)
        Parameters:
        scale - value to divide by.
        Returns:
        divided copy
      • clone

        public CubeCoordinate clone()
        Creates a clone of this Coordinate.
        Overrides:
        clone in class java.lang.Object
        Returns:
        the new CubeCoordinate
      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • deserialize

        public static CubeCoordinate deserialize​(org.json.JSONObject data)
        deserializes a CubeCoordinate from json
        Parameters:
        data - json data
        Returns:
        deserialized CubeCoordinate
      • serialize

        public org.json.JSONObject serialize()
        serializes a CubeCoordinate to json
        Returns:
        json object
      • toString

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