// Copyright (c) 2014 Robert Rouhani and other contributors (see CONTRIBUTORS file). // Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE using System; namespace SharpNav { /// /// A set of cardinal directions. /// public enum Direction { /// /// The west direction. /// West = 0, /// /// The north direction. /// North = 1, /// /// The east direction. /// East = 2, /// /// The south direction. /// South = 3 } /// /// A set of extension methods to make using the Direction enum a lot simpler. /// public static class DirectionExtensions { private static readonly int[] OffsetsX = { -1, 0, 1, 0 }; private static readonly int[] OffsetsY = { 0, 1, 0, -1 }; /// /// Gets an X offset. /// /// /// The directions cycle between the following, starting from 0: west, north, east, south. /// /// The direction. /// The offset for the X coordinate. public static int GetHorizontalOffset(this Direction dir) { return OffsetsX[(int)dir]; } /// /// Get a Y offset. /// /// /// The directions cycle between the following, starting from 0: west, north, east, south. /// /// The direction. /// The offset for the Y coordinate. public static int GetVerticalOffset(this Direction dir) { return OffsetsY[(int)dir]; } /// /// Gets the next cardinal direction in clockwise order. /// /// The current direction. /// The next direction. public static Direction NextClockwise(this Direction dir) { switch (dir) { case Direction.West: return Direction.North; case Direction.North: return Direction.East; case Direction.East: return Direction.South; case Direction.South: return Direction.West; default: throw new ArgumentException("dir isn't a valid Direction."); } } /// /// Gets the next cardinal direction in counter-clockwise order. /// /// The current direction. /// The next direction. public static Direction NextCounterClockwise(this Direction dir) { switch (dir) { case Direction.West: return Direction.South; case Direction.South: return Direction.East; case Direction.East: return Direction.North; case Direction.North: return Direction.West; default: throw new ArgumentException("dir isn't a valid Direction."); } } } }