Box the compass: Difference between revisions

no edit summary
(→‎{{header|Wren}}: Library name change.)
No edit summary
Line 5,829:
32 354.37° North by west
1 354.38° North</pre>
=={{header|Red}}==
<lang Rebol>Red []
 
d: charset [#"N" #"E" #"S" #"W"] ;; main directions
hm: #() ;; hm = hashmap - key= heading, value = [box , compass point]
 
compass-points: [N NbE NNE NEbN NE NEbE ENE EbN E EbS ESE SEbE SE SEbS SSE
SbE S SbW SSW SWbS SW SWbW WSW WbS W WbN WNW NWbW NW NWbN NNW NbW N ]
 
expand: func [cp repl][ ;; expand compass point to words
parse cp [ copy a thru d ahead 2 d insert "-" ] ;; insert "-" after first direction, if followed by 2 more
foreach [src dst ] repl [ replace/all cp to-string src to-string dst ] ;; N -> north ...
uppercase/part cp 1 ;; convert first letter to uppercase
]
 
print-line: does [ print [pad/left hm/:heading/1 3 pad hm/:heading/2 20 heading ] ]
 
forall compass-points [
i: (index? compass-points) - 1 ;; so i = 0..33
heading: i * 11.25 + either 1 = rem: i % 3 [ 5.62] ;; rem = remainder
[ either rem = 2 [-5.62] [0.0] ]
hm/:heading: reduce [ (i % 32 + 1 ) expand to-string compass-points/1 [N north b " by " S south E east W west] ]
print-line heading
]</lang>
'''output'''
<pre>
1 North 0.0
2 North by east 16.87
3 North-northeast 16.88
4 Northeast by north 33.75
5 Northeast 50.62
6 Northeast by east 50.63
7 East-northeast 67.5
8 East by north 84.37
9 East 84.38
10 East by south 101.25
11 East-southeast 118.12
12 Southeast by east 118.13
13 Southeast 135.0
14 Southeast by south 151.87
15 South-southeast 151.88
16 South by east 168.75
17 South 185.62
18 South by west 185.63
19 South-southwest 202.5
20 Southwest by south 219.37
21 Southwest 219.38
22 Southwest by west 236.25
23 West-southwest 253.12
24 West by south 253.13
25 West 270.0
26 West by north 286.87
27 West-northwest 286.88
28 Northwest by west 303.75
29 Northwest 320.62
30 Northwest by north 320.63
31 North-northwest 337.5
32 North by west 354.37
1 North 354.38
>>
</pre>
=={{header|REXX}}==
This version does normalization of the (degree) heading and can also handle negative headings.