private static IHexCoordinate Round(double x, double y, double z) { var rX = (int)Math.Round(x); var rY = (int)Math.Round(y); var rZ = (int)Math.Round(z); var xDiff = Math.Abs(rX - x); var yDiff = Math.Abs(rY - y); var zDiff = Math.Abs(rZ - z); if (xDiff > yDiff && xDiff > yDiff) { rX = -rY - rZ; } else if (yDiff > zDiff) { rY = -rX - rZ; } else { rZ = -rX - rY; } return(HexCoordinates.Cube(rX, rY, rZ)); }
/// <summary> /// <para> /// Returns all <see cref="IHexCoordinate" /> within <paramref name="radius" /> and <paramref name="coordinate" /> /// as center. /// </para> /// </summary> /// <param name="coordinate">Current <see cref="IHexCoordinate" />.</param> /// <param name="radius">Radius from <paramref name="coordinate" />. 0 returns <paramref name="coordinate" />.</param> /// <returns>All <see cref="IHexCoordinate" /> within <paramref name="radius" />.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="radius" /> < 0. </exception> /// <exception cref="ArgumentNullException"><paramref name="coordinate" /> is null.</exception> public static IEnumerable <IHexCoordinate> Range(this IHexCoordinate coordinate, int radius) { if (coordinate == null) { throw new ArgumentNullException("coordinate"); } if (radius == 0) { yield return(coordinate); yield break; } if (radius < 0) { throw new ArgumentOutOfRangeException("radius"); } for (var dx = -radius; dx <= radius; dx++) { for (var dy = Math.Max(-radius, -dx - radius); dy <= Math.Min(radius, -dx + radius); dy++) { yield return(HexCoordinates.Cube(dx, dy, -dx - dy).Add(coordinate)); } } }
/// <summary> /// Initializes a new unit <see cref="IHexCoordinate" /> with given <paramref name="direction" /> and /// <paramref name="distance" />. /// </summary> /// <param name="direction">Direction from (0, 0).</param> /// <param name="distance">Distance from (0, 0).</param> /// <returns>Unit <see cref="IHexCoordinate" />.</returns> /// <exception cref="ArgumentOutOfRangeException"> /// <para><paramref name="distance" /> < 0.</para> /// <para><paramref name="direction" /> out of range.</para> /// </exception> public static IHexCoordinate Distance(this HexDirection direction, int distance) { return(HexCoordinates.Axial(0, 0).Neighbor(direction, distance)); }