-
-
Save ncdulo/7861c85518c3f1091ea36d4eb62b8593 to your computer and use it in GitHub Desktop.
[Python] Degrees to Cardinal direction
This file contains hidden or 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
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.