/// <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); }
/// <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 static CubicHexCoord RotateOnceRight(CubicHexCoord center, CubicHexCoord toRotate) { var local = toRotate - center; var localRotated = new CubicHexCoord(-local.y, -local.z, -local.x); var rotated = localRotated + center; return(rotated); }
/// <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)); }
/// <summary> /// Check if this CubicHexCoord is equal to an arbitrary object. /// </summary> /// <returns>Whether or not this CubicHexCoord and the given object are equal.</returns> public override bool 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)); }
/// <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); }
/// <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); }
FloatCubic(CubicHexCoord cubic) { this.x = (float)cubic.x; this.y = (float)cubic.y; this.z = (float)cubic.z; }
/// <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!"); }
/// <summary> /// Returns an array of CubicHexCoords of a area centering around this 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="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> /// <returns></returns> public CubicHexCoord[] SpiralAroundOutward(int range, DirectionEnum startDirection = DirectionEnum.E) { return(CubicHexCoord.SpiralOutward(this, range, startDirection)); }
/// <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)); }
/// <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)); }
/// <summary> /// Returns an array of CubicHexCoords that appear at the given range around this 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="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 CubicHexCoord[] RingAround(int range, DirectionEnum startDirection = DirectionEnum.E) { return(CubicHexCoord.Ring(this, range, startDirection)); }
/// <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)); }
/// <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)); }
/// <summary> /// Returns an array of CubicHexCoords within the given range from this hex (including /// this hex) in no particular order. /// </summary> /// <param name="range">The maximum number of grid steps away from this hex that /// CubicHexCoords will be returned for.</param> /// <returns>An array of CubicHexCoords within the given range from this hex (including /// this hex) in no particular order.</returns> public CubicHexCoord[] AreaAround(int range) { return(CubicHexCoord.Area(this, range)); }