Skip to content

Instantly share code, notes, and snippets.

@mmar
Last active August 7, 2022 03:33
Show Gist options
  • Save mmar/284964 to your computer and use it in GitHub Desktop.
Save mmar/284964 to your computer and use it in GitHub Desktop.
Converting degrees to compass heading
class Fixnum
def degrees_to_compass
%w( N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW )[((self+11.25).modulo(360)/22.5).floor]
end
end
func heading(degrees: Double) -> String {
["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"][Int((degrees + 11.25).truncatingRemainder(dividingBy: 360) / 22.5)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment