Last active
February 18, 2017 16:31
-
-
Save CallumWatkins/e494aa278bf1c84fdad29139640b8ce1 to your computer and use it in GitHub Desktop.
A class to convert between bearings and compass directions. License: MIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Compass | |
{ | |
public enum CompassDirection { N, NbE, NNE, NEbN, NE, NEbE, ENE, EbN, E, EbS, ESE, SEbE, SE, SEbS, SSE, SbE, S, SbW, SSW, SWbS, SW, SWbW, WSW, WbS, W, WbN, WNW, NWbW, NW, NWbN, NNW, NbW } | |
public enum CompassPoints { Four = 4, Eight = 8, Sixteen = 16, ThirtyTwo = 32 } | |
public static Dictionary<CompassDirection, string> CompassDirections | |
{ | |
get | |
{ | |
return new Dictionary<CompassDirection, string>() | |
{ | |
{ CompassDirection.N, "North" }, | |
{ CompassDirection.NbE, "North by east" }, | |
{ CompassDirection.NNE, "North-northeast" }, | |
{ CompassDirection.NEbN, "Northeast by north" }, | |
{ CompassDirection.NE, "Northeast" }, | |
{ CompassDirection.NEbE, "Northeast by east" }, | |
{ CompassDirection.ENE, "East-northeast" }, | |
{ CompassDirection.EbN, "East by north" }, | |
{ CompassDirection.E, "East" }, | |
{ CompassDirection.EbS, "East by south" }, | |
{ CompassDirection.ESE, "East-southeast" }, | |
{ CompassDirection.SEbE, "Southeast by east" }, | |
{ CompassDirection.SE, "Southeast" }, | |
{ CompassDirection.SEbS, "Southeast by south" }, | |
{ CompassDirection.SSE, "South-southeast" }, | |
{ CompassDirection.SbE, "South by east" }, | |
{ CompassDirection.S, "South" }, | |
{ CompassDirection.SbW, "South by west" }, | |
{ CompassDirection.SSW, "South-southwest" }, | |
{ CompassDirection.SWbS, "Southwest by south" }, | |
{ CompassDirection.SW, "Southwest" }, | |
{ CompassDirection.SWbW, "Southwest by west" }, | |
{ CompassDirection.WSW, "West-southwest" }, | |
{ CompassDirection.WbS, "West by south" }, | |
{ CompassDirection.W, "West" }, | |
{ CompassDirection.WbN, "West by north" }, | |
{ CompassDirection.WNW, "West-northwest" }, | |
{ CompassDirection.NWbW, "Northwest by west" }, | |
{ CompassDirection.NW, "Northwest" }, | |
{ CompassDirection.NWbN, "Northwest by north" }, | |
{ CompassDirection.NNW, "North-northwest" }, | |
{ CompassDirection.NbW, "North by west" } | |
}; | |
} | |
} | |
public static CompassDirection BearingToCompassDirection(Bearing bearing, CompassPoints points = CompassPoints.Eight) | |
{ | |
int counter = 0; | |
decimal interval = 360 / (decimal)points; | |
for (decimal d = interval / 2; d <= 360 + (interval / 2); d += interval) | |
{ | |
if (bearing.Value <= d) | |
{ | |
while (counter > 31) { counter -= 32; } | |
return (CompassDirection)counter; | |
} | |
counter += 32 / (int)points; | |
} | |
throw new Exception(string.Format("An unknown exception occurred in BearingToCompassDirection; bearing={0}, points={1}.", bearing.Value, points)); | |
} | |
public static Bearing CompassDirectionToBearing(CompassDirection compassDirection) | |
{ | |
return new Bearing() { Value = 11.25m * (int)compassDirection }; | |
} | |
public struct Bearing | |
{ | |
private decimal _value; | |
public decimal Value | |
{ | |
get { return _value; } | |
set | |
{ | |
while (value < 0) { value += 360; } | |
while (value > 360) { value -= 360; } | |
_value = value; | |
} | |
} | |
public string ToThreeFigureBearing() | |
{ | |
return ((int)Value).ToString("D3"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment