Skip to content

Instantly share code, notes, and snippets.

@ncdulo
Forked from RobertSudwarts/deg_to_cardinal.py
Last active March 13, 2020 22:05
Show Gist options
  • Save ncdulo/7861c85518c3f1091ea36d4eb62b8593 to your computer and use it in GitHub Desktop.
Save ncdulo/7861c85518c3f1091ea36d4eb62b8593 to your computer and use it in GitHub Desktop.
[Python] Degrees to Cardinal direction
def degrees_to_cardinal(degrees):
'''
Return the cardinal direction representing a given 'degrees'
'''
cardinal = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
cardinal_len = len(cardinal)
ix = round(degrees / (360.0 / cardinal_len))
return cardinal[ix % cardinal_len]
@ncdulo
Copy link
Author

ncdulo commented Mar 13, 2020

Updated the original to integrate the changes described in this comment, and to increase readability. I doubt the change from calling len() twice will make any meaningful difference, I just didn't like calling the same function twice like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment