Ring(CubicHexCoord center, int range, DirectionEnum startDirection = DirectionEnum.E) { if (range <= 0) { throw new ArgumentOutOfRangeException("range must be a positive integer value."); } CubicHexCoord[] result = new CubicHexCoord[6 * range]; CubicHexCoord cube = center + DIRECTIONS[(int)startDirection].Scale(range); int[] directions = new int[6]; for (int i = 0; i < 6; i++) { directions[i] = ((int)startDirection + i) % 6; } int index = 0; for (int i = 0; i < 6; i++) { int neighborDirection = (directions[i] + 2) % 6; for (int j = 0; j < range; j++) { result[index++] = cube; cube = cube.Neighbor((DirectionEnum)neighborDirection); } } return(result); }
public void AreaAround() { CubicHexCoord cubic = new CubicHexCoord( 1, 0, -1 ); CubicHexCoord[] area = CubicHexCoord.Area( cubic, 2 ); // Center Assert.Contains( new CubicHexCoord( 1, 0, -1 ), area ); // Distance 1 Assert.Contains( new CubicHexCoord( 0, 1, -1 ), area ); Assert.Contains( new CubicHexCoord( 1, 1, -2 ), area ); Assert.Contains( new CubicHexCoord( 2, 0, -2 ), area ); Assert.Contains( new CubicHexCoord( 2, -1, -1 ), area ); Assert.Contains( new CubicHexCoord( 1, -1, 0 ), area ); Assert.Contains( new CubicHexCoord( 0, 0, 0 ), area ); // Distance 2 Assert.Contains( new CubicHexCoord( -1, 2, -1 ), area ); Assert.Contains( new CubicHexCoord( 0, 2, -2 ), area ); Assert.Contains( new CubicHexCoord( 1, 2, -3 ), area ); Assert.Contains( new CubicHexCoord( 2, 1, -3 ), area ); Assert.Contains( new CubicHexCoord( 3, 0, -3 ), area ); Assert.Contains( new CubicHexCoord( 3, -1, -2 ), area ); Assert.Contains( new CubicHexCoord( 3, -2, -1 ), area ); Assert.Contains( new CubicHexCoord( 2, -2, 0 ), area ); Assert.Contains( new CubicHexCoord( 1, -2, 1 ), area ); Assert.Contains( new CubicHexCoord( 0, -1, 1 ), area ); Assert.Contains( new CubicHexCoord( -1, 0, 1 ), area ); Assert.Contains( new CubicHexCoord( -1, 1, 0 ), area ); }
SpiralOutward(CubicHexCoord center, int range, DirectionEnum startDirection = DirectionEnum.E) { if (range <= 0) { throw new ArgumentOutOfRangeException("range must be a positive integer value."); } else if (range == 0) { return(new CubicHexCoord[1] { center }); } int arraySize = 1; for (int i = range; i > 0; i--) { arraySize += 6 * i; } CubicHexCoord[] result = new CubicHexCoord[arraySize]; result[0] = center; int arrayIndex = 1; for (int i = 1; i <= range; i++) { CubicHexCoord[] ring = CubicHexCoord.Ring(center, i, startDirection); ring.CopyTo(result, arrayIndex); arrayIndex += ring.Length; } return(result); }
public void ConstructorXYZ() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ); Assert.That( cubic.x, Is.EqualTo( 1 ) ); Assert.That( cubic.y, Is.EqualTo( 2 ) ); Assert.That( cubic.z, Is.EqualTo( 3 ) ); }
public void Diagonal() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ).Diagonal( DiagonalEnum.ESE ); Assert.That( cubic.x, Is.EqualTo( 2 ) ); Assert.That( cubic.y, Is.EqualTo( 0 ) ); Assert.That( cubic.z, Is.EqualTo( 4 ) ); }
Distance(CubicHexCoord a, CubicHexCoord b) { int dx = Math.Abs(a.x - b.x); int dy = Math.Abs(a.y - b.y); int dz = Math.Abs(a.z - b.z); return(Math.Max(Math.Max(dx, dy), dz)); }
public void ConstructorParameterless() { CubicHexCoord cubic = new CubicHexCoord(); Assert.That( cubic.x, Is.EqualTo( 0 ) ); Assert.That( cubic.y, Is.EqualTo( 0 ) ); Assert.That( cubic.z, Is.EqualTo( 0 ) ); }
public void Diagonals() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ); CubicHexCoord[] diagonals = cubic.Diagonals(); Assert.That( diagonals, Is.EquivalentTo( new CubicHexCoord[ 6 ] { cubic.Diagonal( DiagonalEnum.ESE ), cubic.Diagonal( DiagonalEnum.S ), cubic.Diagonal( DiagonalEnum.WSW ), cubic.Diagonal( DiagonalEnum.WNW ), cubic.Diagonal( DiagonalEnum.N ), cubic.Diagonal( DiagonalEnum.ENE ) } ) ); }
Equals(object obj) { if (obj == null) { return(false); } if (GetType() != obj.GetType()) { return(false); } CubicHexCoord other = (CubicHexCoord)obj; return((this.x == other.x) && (this.y == other.y) && (this.z == other.z)); }
Line(CubicHexCoord start, CubicHexCoord end) { int distance = CubicHexCoord.Distance(start, end); CubicHexCoord[] result = new CubicHexCoord[distance + 1]; for (int i = 0; i <= distance; i++) { float xLerp = start.x + (end.x - start.x) * 1f / distance * i; float yLerp = start.y + (end.y - start.y) * 1f / distance * i; float zLerp = start.z + (end.z - start.z) * 1f / distance * i; result[i] = new FloatCubic(xLerp, yLerp, zLerp).Round(); } return(result); }
Area(CubicHexCoord center, int range) { if (range < 0) { throw new ArgumentOutOfRangeException("range must be a non-negative integer value."); } else if (range == 0) { return(new CubicHexCoord[1] { center }); } int arraySize = 1; for (int i = range; i > 0; i--) { arraySize += 6 * i; } CubicHexCoord[] result = new CubicHexCoord[arraySize]; for (int i = 0, dx = -range; dx <= range; dx++) { int dyMinBound = Math.Max(-range, -dx - range); int dyMaxBound = Math.Min(range, -dx + range); for (int dy = dyMinBound; dy <= dyMaxBound; dy++) { int dz = -dx - dy; result[i++] = center + new CubicHexCoord(dx, dy, dz); } } return(result); }
SpiralAroundOutward(int range, DirectionEnum startDirection = DirectionEnum.E) { return(CubicHexCoord.SpiralOutward(this, range, startDirection)); }
RotateOtherAround(CubicHexCoord toRotate, RotationEnum rotation) { return(CubicHexCoord.Rotate(this, toRotate, rotation)); }
RotateAroundOther(CubicHexCoord center, RotationEnum rotation) { return(CubicHexCoord.Rotate(center, this, rotation)); }
public void Neighbors() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ); CubicHexCoord[] neighbors = cubic.Neighbors(); Assert.That( neighbors, Is.EquivalentTo( new CubicHexCoord[ 6 ] { cubic.Neighbor( DirectionEnum.E ), cubic.Neighbor( DirectionEnum.SE ), cubic.Neighbor( DirectionEnum.SW ), cubic.Neighbor( DirectionEnum.W ), cubic.Neighbor( DirectionEnum.NW ), cubic.Neighbor( DirectionEnum.NE ) } ) ); }
public void ToAxial() { AxialHexCoord axial = new CubicHexCoord( 1, 2, 3 ).ToAxial(); Assert.That( axial.q, Is.EqualTo( 1 ) ); Assert.That( axial.r, Is.EqualTo( 3 ) ); }
Rotate(CubicHexCoord center, CubicHexCoord toRotate, RotationEnum rotation) { throw new NotImplementedException("Feature not suppored yet!"); }
/// <summary> /// Returns an array of CubicHexCoords that appear at the given range around the given /// center hex. The ring begins from the CubicHexCoord range grid steps away from the /// center, heading in the given direction, and encircling the center in clockwise order. /// </summary> /// <param name="center">The CubicHexCoord around which the ring is formed.</param> /// <param name="range">The number of grid steps distance away from the center that the /// ring will be.</param> /// <param name="startDirection">The direction in which the first CubicHexCoord of the /// ring will appear in.</param> /// <returns>An array of CubicHexCoords ordered as a ring.</returns> public static CubicHexCoord[] Ring( CubicHexCoord center, int range, DirectionEnum startDirection = DirectionEnum.E ) { if ( range <= 0 ) { throw new ArgumentOutOfRangeException( "range must be a positive integer value." ); } CubicHexCoord[] result = new CubicHexCoord[ 6 * range ]; CubicHexCoord cube = center + DIRECTIONS[ (int)startDirection ].Scale( range ); int[] directions = new int[ 6 ]; for ( int i = 0; i < 6; i++ ) { directions[ i ] = ( (int)startDirection + i ) % 6; } int index = 0; for ( int i = 0; i < 6; i++ ) { int neighborDirection = ( directions[ i ] + 2 ) % 6; for ( int j = 0; j < range; j++ ) { result[ index++ ] = cube; cube = cube.Neighbor( (DirectionEnum)neighborDirection ); } } return result; }
/// <remarks>THIS IS NOT YET IMPLEMENTED!</remarks> /// <see cref="http://www.redblobgames.com/grids/hexagons/"/> /// <summary> /// Returns an array of CubicHexCoords that represents the hexes belonging to both a and b. /// </summary> /// <param name="a">An array of CubicHexCoords representing an area or spiral of hexes. /// </param> /// <param name="b">An array of CubicHexCoords representing an area or spiral of hexes. /// </param> /// <returns>An array of CubicHexCoords that represents the hexes belonging to both a /// and b.</returns> public static CubicHexCoord[] IntersectRanges( CubicHexCoord[] a, CubicHexCoord[] b ) { throw new NotImplementedException( "Feature not suppored yet!" ); }
/// <summary> /// Returns an array of CubicHexCoords within the given range from the given center /// (including the center itself) in no particular order. /// </summary> /// <param name="center">The CubicHexCoord around which the area is formed.</param> /// <param name="range">The maximum number of grid steps away from the center that /// CubicHexCoords will be returned for.</param> /// <returns>An array of CubicHexCoords within the given range from the given center /// (including the center itself) in no particular order.</returns> public static CubicHexCoord[] Area( CubicHexCoord center, int range ) { if ( range < 0 ) { throw new ArgumentOutOfRangeException( "range must be a non-negative integer value." ); } else if ( range == 0 ) { return new CubicHexCoord[ 1 ] { center }; } int arraySize = 1; for ( int i = range; i > 0; i-- ) { arraySize += 6 * i; } CubicHexCoord[] result = new CubicHexCoord[ arraySize ]; for ( int i = 0, dx = -range; dx <= range; dx++ ) { int dyMinBound = Math.Max( -range, -dx - range ); int dyMaxBound = Math.Min( range, -dx + range ); for ( int dy = dyMinBound; dy <= dyMaxBound; dy++ ) { int dz = -dx - dy; result[ i++ ] = center + new CubicHexCoord( dx, dy, dz ); } } return result; }
public void OperatorOverloadPlus() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ) + new CubicHexCoord( 4, 5, 6 ); Assert.That( cubic.x, Is.EqualTo( 5 ) ); Assert.That( cubic.y, Is.EqualTo( 7 ) ); Assert.That( cubic.z, Is.EqualTo( 9 ) ); }
public void StaticLine() { CubicHexCoord startCubic = new CubicHexCoord( 1, 0, -1 ); CubicHexCoord endCubic = new CubicHexCoord( -2, 2, 0 ); CubicHexCoord[] line = CubicHexCoord.Line( startCubic, endCubic ); Assert.That( line, Is.EquivalentTo( new CubicHexCoord[ 4 ] { new CubicHexCoord( 1, 0, -1 ), new CubicHexCoord( 0, 1, -1 ), new CubicHexCoord( -1, 1, 0 ), new CubicHexCoord( -2, 2, 0 ) } ) ); }
public void StaticRing() { CubicHexCoord cubic = new CubicHexCoord( 1, 0, -1 ); CubicHexCoord[] ring = CubicHexCoord.Ring( cubic, 2, DirectionEnum.W ); Assert.That( ring, Is.EquivalentTo( new CubicHexCoord[ 12 ] { new CubicHexCoord( -1, 2, -1 ), new CubicHexCoord( 0, 2, -2 ), new CubicHexCoord( 1, 2, -3 ), new CubicHexCoord( 2, 1, -3 ), new CubicHexCoord( 3, 0, -3 ), new CubicHexCoord( 3, -1, -2 ), new CubicHexCoord( 3, -2, -1 ), new CubicHexCoord( 2, -2, 0 ), new CubicHexCoord( 1, -2, 1 ), new CubicHexCoord( 0, -1, 1 ), new CubicHexCoord( -1, 0, 1 ), new CubicHexCoord( -1, 1, 0 ) } ) ); }
public void ToOffset() { OffsetHexCoord offset = new CubicHexCoord( 0, -2, 2 ).ToOffset(); Assert.That( offset.q, Is.EqualTo( 1 ) ); Assert.That( offset.r, Is.EqualTo( 2 ) ); }
/// <summary> /// Returns an array of CubicHexCoords of a area centering around the given center hex and /// extending in every direction up to the given range. The hexes are ordered starting from /// the maximum range, in the given direction, spiraling inward in a clockwise direction /// until the center is reached (and is the last element in the array). /// </summary> /// <param name="center">The CubicHexCoord around which the spiral is formed.</param> /// <param name="range">The number of grid steps distance away from the center that the /// edge of the spiral will be.</param> /// <param name="startDirection">The direction in which the first CubicHexCoord of the /// spiral will appear in.</param> /// <returns>An array of CubicHexCoords ordered as a spiral, beginning from the outside /// and proceeding clockwise until it reaches the center of the spiral.</returns> public static CubicHexCoord[] SpiralOutward( CubicHexCoord center, int range, DirectionEnum startDirection = DirectionEnum.E ) { if ( range <= 0 ) { throw new ArgumentOutOfRangeException( "range must be a positive integer value." ); } else if ( range == 0 ) { return new CubicHexCoord[ 1 ] { center }; } int arraySize = 1; for ( int i = range; i > 0; i-- ) { arraySize += 6 * i; } CubicHexCoord[] result = new CubicHexCoord[ arraySize ]; result[ 0 ] = center; int arrayIndex = 1; for ( int i = 1; i <= range; i++ ) { CubicHexCoord[] ring = CubicHexCoord.Ring( center, i, startDirection ); ring.CopyTo( result, arrayIndex ); arrayIndex += ring.Length; } return result; }
public void OperatorOverloadNotEquals() { bool isTrue = new CubicHexCoord( 1, 2, 3 ) != new CubicHexCoord( 4, 5, 6 ); bool isFalse = new CubicHexCoord( 1, 2, 3 ) != new CubicHexCoord( 1, 2, 3 ); Assert.That( isTrue, Is.True ); Assert.That( isFalse, Is.False ); }
/// <summary> /// Returns an array of CubicHexCoords that form the straightest path from this hex to the /// given end. The hexes in the line are determined by forming a straight line from start /// to end and linearly interpolating and rounding each of the interpolated points to /// the nearest hex position. /// </summary> /// <param name="other">The CubicHexCoord representing the last hex in the line.</param> /// <returns>An array of CubicHexCoords ordered as a line from start to end.</returns> public CubicHexCoord[] LineTo( CubicHexCoord other ) { return CubicHexCoord.Line( this, other ); }
public void OperatorOverloadMinus() { CubicHexCoord cubic = new CubicHexCoord( 6, 5, 4 ) - new CubicHexCoord( 1, 2, 3 ); Assert.That( cubic.x, Is.EqualTo( 5 ) ); Assert.That( cubic.y, Is.EqualTo( 3 ) ); Assert.That( cubic.z, Is.EqualTo( 1 ) ); }
/// <remarks>THIS IS NOT YET IMPLEMENTED!</remarks> /// <summary> /// Rotate a CubicHexCoord around this CubicHexCoord by the given rotation (constrained to /// exact 60 degree rotation steps) and return the CubicHexCoord that represents the /// rotated position in the grid. /// </summary> /// <param name="toRotate">The CubicHexCoord to be rotated around this CubicHexCoord.</param> /// <param name="rotation">The direction and magnitude of rotation to be applied (in exact /// 60 degree rotation steps.</param> /// <returns>A CubicHexCoord representing the position of the rotated hex on the grid. /// </returns> public CubicHexCoord RotateOtherAround( CubicHexCoord toRotate, RotationEnum rotation ) { return CubicHexCoord.Rotate( this, toRotate, rotation ); }
public void DistanceTo() { CubicHexCoord cubic1 = new CubicHexCoord( 0, 0, 0 ); CubicHexCoord cubic2 = cubic1.Neighbor( DirectionEnum.E ).Neighbor( DirectionEnum.SE ); int distance = cubic1.DistanceTo( cubic2 ); Assert.That( distance, Is.EqualTo( 2 ) ); }
public void StaticSpiralOutward() { CubicHexCoord cubic = new CubicHexCoord( 1, 0, -1 ); CubicHexCoord[] spiral = CubicHexCoord.SpiralOutward( cubic, 2, DirectionEnum.W ); Assert.That( spiral, Is.EquivalentTo( new CubicHexCoord[ 19 ] { // Center new CubicHexCoord( 1, 0, -1 ), // Distance 1 new CubicHexCoord( 0, 1, -1 ), new CubicHexCoord( 1, 1, -2 ), new CubicHexCoord( 2, 0, -2 ), new CubicHexCoord( 2, -1, -1 ), new CubicHexCoord( 1, -1, 0 ), new CubicHexCoord( 0, 0, 0 ), // Distance 2 new CubicHexCoord( -1, 2, -1 ), new CubicHexCoord( 0, 2, -2 ), new CubicHexCoord( 1, 2, -3 ), new CubicHexCoord( 2, 1, -3 ), new CubicHexCoord( 3, 0, -3 ), new CubicHexCoord( 3, -1, -2 ), new CubicHexCoord( 3, -2, -1 ), new CubicHexCoord( 2, -2, 0 ), new CubicHexCoord( 1, -2, 1 ), new CubicHexCoord( 0, -1, 1 ), new CubicHexCoord( -1, 0, 1 ), new CubicHexCoord( -1, 1, 0 ) } ) ); }
/// <summary> /// Returns the minimum number of grid steps to get from a to b. /// </summary> /// <param name="a">Any CubicHexCoord.</param> /// <param name="b">Any CubicHexCoord.</param> /// <returns>An integer number of grid steps from a to b.</returns> public static int Distance( CubicHexCoord a, CubicHexCoord b ) { int dx = Math.Abs( a.x - b.x ); int dy = Math.Abs( a.y - b.y ); int dz = Math.Abs( a.z - b.z ); return Math.Max( Math.Max( dx, dy ), dz ); }
FloatCubic(CubicHexCoord cubic) { this.x = (float)cubic.x; this.y = (float)cubic.y; this.z = (float)cubic.z; }
/// <summary> /// Returns an array of CubicHexCoords that form the straightest path from the given start /// to the given end. The hexes in the line are determined by forming a straight line from /// start to end and linearly interpolating and rounding each of the interpolated points to /// the nearest hex position. /// </summary> /// <param name="start">The CubicHexCoord representing the first hex in the line.</param> /// <param name="end">The CubicHexCoord representing the last hex in the line.</param> /// <returns>An array of CubicHexCoords ordered as a line from start to end.</returns> public static CubicHexCoord[] Line( CubicHexCoord start, CubicHexCoord end ) { int distance = CubicHexCoord.Distance( start, end ); CubicHexCoord[] result = new CubicHexCoord[ distance + 1 ]; for ( int i = 0; i <= distance; i++ ) { float xLerp = start.x + ( end.x - start.x ) * 1f / distance * i; float yLerp = start.y + ( end.y - start.y ) * 1f / distance * i; float zLerp = start.z + ( end.z - start.z ) * 1f / distance * i; result[ i ] = new FloatCubic( xLerp, yLerp, zLerp ).Round(); } return result; }
AreaAround(int range) { return(CubicHexCoord.Area(this, range)); }
/// <remarks>THIS IS NOT YET IMPLEMENTED!</remarks> /// <see cref="http://www.redblobgames.com/grids/hexagons/"/> /// <summary> /// Rotate a CubicHexCoord around the given center by the given rotation (constrained to /// exact 60 degree rotation steps) and return the CubicHexCoord that represents the /// rotated position in the grid. /// </summary> /// <param name="center">The CubicHexCoord representing the rotation's pivot.</param> /// <param name="toRotate">The CubicHexCoord to be rotated around the pivot.</param> /// <param name="rotation">The direction and magnitude of rotation to be applied (in exact /// 60 degree rotation steps.</param> /// <returns>A CubicHexCoord representing the position of the rotated hex on the grid. /// </returns> public static CubicHexCoord Rotate( CubicHexCoord center, CubicHexCoord toRotate, RotationEnum rotation ) { throw new NotImplementedException( "Feature not suppored yet!" ); }
DistanceTo(CubicHexCoord other) { return(CubicHexCoord.Distance(this, other)); }
/// <summary> /// Returns the minimum number of grid steps to get from this hex to the given hex. /// </summary> /// <param name="other">Any CubicHexCoord.</param> /// <returns>An integer number of grid steps from this hex to the given hex.</returns> public int DistanceTo( CubicHexCoord other ) { return CubicHexCoord.Distance( this, other ); }
LineTo(CubicHexCoord other) { return(CubicHexCoord.Line(this, other)); }
/// <remarks>THIS IS NOT YET IMPLEMENTED!</remarks> /// <summary> /// Rotate this CubicHexCoord around the given center by the given rotation (constrained to /// exact 60 degree rotation steps) and return the CubicHexCoord that represents the /// rotated position in the grid. /// </summary> /// <param name="center">The CubicHexCoord to be rotated around this CubicHexCoord.</param> /// <param name="rotation">The direction and magnitude of rotation to be applied (in exact /// 60 degree rotation steps.</param> /// <returns>A CubicHexCoord representing the position of the rotated hex on the grid. /// </returns> public CubicHexCoord RotateAroundOther( CubicHexCoord center, RotationEnum rotation ) { return CubicHexCoord.Rotate( center, this, rotation ); }
RingAround(int range, DirectionEnum startDirection = DirectionEnum.E) { return(CubicHexCoord.Ring(this, range, startDirection)); }
/// <summary> /// Create a new FloatCubic given a CubicHexIndex. /// </summary> /// <param name="cubic">Any CubicHexCoord representing a hex.</param> public FloatCubic( CubicHexCoord cubic ) { this.x = (float)cubic.x; this.y = (float)cubic.y; this.z = (float)cubic.z; }
public void Neighbor() { CubicHexCoord cubic = new CubicHexCoord( 1, 2, 3 ).Neighbor( DirectionEnum.E ); Assert.That( cubic.x, Is.EqualTo( 2 ) ); Assert.That( cubic.y, Is.EqualTo( 1 ) ); Assert.That( cubic.z, Is.EqualTo( 3 ) ); }