Skip to content

Instantly share code, notes, and snippets.

@allebb
Forked from adrianstevens/compass-cardinal
Created August 16, 2016 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allebb/165faffa61f145fb7b81c2f8d49ced9d to your computer and use it in GitHub Desktop.
Save allebb/165faffa61f145fb7b81c2f8d49ced9d to your computer and use it in GitHub Desktop.
Compass heading to cardinal direction in c#
public static string DegreesToCardinal(double degrees)
{
string[] caridnals = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" };
return caridnals[ (int)Math.Round(((double)degrees % 360) / 45) ];
}
public static string DegreesToCardinalDetailed(double degrees)
{
string[] caridnals = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };
return caridnals[ (int)Math.Round(((double)degrees*10 % 3600) / 225) ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment