2
0

TurnDirectionMapper.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. namespace Routes
  3. {
  4. static class TurnDirectionMapper
  5. {
  6. private static readonly Dictionary<RoadDirection, TurnDirection> fromSouth =
  7. new Dictionary<RoadDirection, TurnDirection>
  8. {
  9. {RoadDirection.East, TurnDirection.Right},
  10. {RoadDirection.West, TurnDirection.Left}
  11. };
  12. private static readonly Dictionary<RoadDirection, TurnDirection> fromNorth =
  13. new Dictionary<RoadDirection, TurnDirection>
  14. {
  15. {RoadDirection.East, TurnDirection.Left},
  16. {RoadDirection.West, TurnDirection.Right}
  17. };
  18. private static readonly Dictionary<RoadDirection, TurnDirection> fromWest =
  19. new Dictionary<RoadDirection, TurnDirection>
  20. {
  21. {RoadDirection.North, TurnDirection.Left},
  22. {RoadDirection.South, TurnDirection.Right}
  23. };
  24. private static readonly Dictionary<RoadDirection, TurnDirection> fromEast =
  25. new Dictionary<RoadDirection, TurnDirection>
  26. {
  27. {RoadDirection.North, TurnDirection.Right},
  28. {RoadDirection.South, TurnDirection.Left}
  29. };
  30. public static TurnDirection GetTurnDirection(RoadDirection from, RoadDirection to)
  31. {
  32. TurnDirection direction;
  33. switch (from)
  34. {
  35. case RoadDirection.West:
  36. return fromWest.TryGetValue(to, out direction) ? direction : TurnDirection.Straight;
  37. case RoadDirection.North:
  38. return fromNorth.TryGetValue(to, out direction) ? direction : TurnDirection.Straight;
  39. case RoadDirection.East:
  40. return fromEast.TryGetValue(to, out direction) ? direction : TurnDirection.Straight;
  41. case RoadDirection.South:
  42. return fromSouth.TryGetValue(to, out direction) ? direction : TurnDirection.Straight;
  43. default:
  44. return TurnDirection.Straight;
  45. }
  46. }
  47. }
  48. }