Boxing The Compass
October 12, 2018
Modern navigators use the 360° angles of a circle to point to compass directions, but traditional mariners use compass points with names like SW and ENE.
Your task is to write a program that converts from degrees to compass points. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
directions(
[n, nne, ne, ene,
e, ese, se, sse,
s, ssw, sw, wsw,
w, wnw, nw, nnw]).
compass(D, Result) :-
directions(Directions),
length(Directions, Divisions),
Separation is 360 / Divisions,
Offset is Separation / 2,
I is mod(floor((D + Offset) / Separation), Divisions) + 1,
nth(I, Directions, Result).
A Haskell version. It was difficult, but I resisted the temptation to do the 128 compass points…
Here’s a 32-point compass solution in Python.
Output: