Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created September 29, 2013 00:51
Show Gist options
  • Save alecperkins/6748189 to your computer and use it in GitHub Desktop.
Save alecperkins/6748189 to your computer and use it in GitHub Desktop.
Convert a bearing in degrees to a cardinal or ordinal direction. eg: 90 becomes E, 195 becomes SSW
bearingToDirection = (bearing, abbrev=true) ->
if abbrev
directions = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
else
directions = ['North', 'North-Northeast', 'Northeast', 'East-Northeast', 'East', 'East-Southeast', 'Southeast', 'South-Southeast', 'South', 'South-Southwest', 'Southwest', 'West-Southwest', 'West', 'West-Northwest', 'Northwest', 'North-Northwest', 'North']
return directions[Math.floor((bearing + 11.25) / 22.5)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment