Box the compass: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) (→{{header|langur}}: modified output) |
Langurmonkey (talk | contribs) |
||
(42 intermediate revisions by 23 users not shown) | |||
Line 22:
* The column of indices can be thought of as an enumeration of the thirty two cardinal points (see [[Talk:Box the compass#Direction, index, and angle|talk page]])..
<br><br>
=={{header|11l}}==
{{trans|Python}}
<
majors *= 2
V quarter1 = ‘N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N’.split(‘,’)
Line 52 ⟶ 51:
d -= 5.62
V n = i % 32 + 1
print(‘#2.0 #<18 #4.2°’.format(n, degrees2compasspoint(d), d))</
{{out}}
<pre>
Line 63 ⟶ 62:
1 North 354.38°
</pre>
=={{header|Action!}}==
Atari 8-bit computer is able to show up to 40 characters per row. Therefore abbreviations of direction have been used instead of the full names.
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
DEFINE PTR="CARD"
PROC InitNames(PTR ARRAY names)
names(0)="N" names(1)="NbE"
names(2)="NNE" names(3)="NEbN"
names(4)="NE" names(5)="NEbE"
names(6)="ENE" names(7)="EbN"
names(8)="E" names(9)="EbS"
names(10)="ESE" names(11)="SEbE"
names(12)="SE" names(13)="SEbS"
names(14)="SSE" names(15)="SbE"
names(16)="S" names(17)="SbW"
names(18)="SSW" names(19)="SWbS"
names(20)="SW" names(21)="SWbW"
names(22)="WSW" names(23)="WbS"
names(24)="W" names(25)="WbN"
names(26)="WNW" names(27)="NWbW"
names(28)="NW" names(29)="NWbN"
names(30)="NNW" names(31)="NbW"
names(32)="N"
RETURN
PROC PrintIndex(BYTE i)
CHAR ARRAY s(5)
StrB(i,s)
PrintF("%2S",s)
RETURN
BYTE FUNC FindDot(CHAR ARRAY s)
BYTE i
FOR i=1 TO s(0)
DO
IF s(i)='. THEN
RETURN (i)
FI
OD
RETURN (0)
PROC PrintAngle(REAL POINTER a)
CHAR ARRAY s(10)
BYTE pos
StrR(a,s)
pos=FindDot(s)
IF pos=0 THEN
s(0)==+3
s(s(0)-2)='.
s(s(0)-1)='0
s(s(0)-0)='0
ELSEIF pos=s(0)-1 THEN
s(0)==+1
s(s(0)-0)='0
FI
PrintF("%6S",s)
RETURN
INT FUNC GetIndex(REAL POINTER a)
REAL r32,r360,r1,r2
IntToReal(32,r32)
IntToReal(360,r360)
RealMult(a,r32,r1)
RealDiv(r1,r360,r2)
RETURN (RealToInt(r2))
PROC Main()
DEFINE COUNT="33"
PTR ARRAY names(COUNT)
INT i,m,ind,x,y
REAL ri,r11_25,r5_62,rh,r1
Put(125) PutE() ;clear the screen
InitNames(names)
ValR("11.25",r11_25)
ValR("5.62",r5_62)
FOR i=0 TO 32
DO
IntToReal(i,ri)
RealMult(ri,r11_25,rh)
m=i MOD 3
IF m=1 THEN
RealAdd(rh,r5_62,r1)
RealAssign(r1,rh)
ELSEIF m=2 THEN
RealSub(rh,r5_62,r1)
RealAssign(r1,rh)
FI
ind=GetIndex(rh)
IF i<16 THEN
x=2 y=i+1
ELSE
x=20 y=i-15
FI
Position(x,y) PrintIndex(ind MOD (COUNT-1)+1)
x==+3
Position(x,y) Print(names(ind))
x==+5
Position(x,y) PrintAngle(rh)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Box_the_compass.png Screenshot from Atari 8-bit computer]
<pre>
1 N 0.00 17 S 185.62
2 NbE 16.87 18 SbW 185.63
3 NNE 16.88 19 SSW 202.50
4 NEbN 33.75 20 SWbS 219.37
5 NE 50.62 21 SW 219.38
6 NEbE 50.63 22 SWbW 236.25
7 ENE 67.50 23 WSW 253.12
8 EbN 84.37 24 WbS 253.13
9 E 84.38 25 W 270.00
10 EbS 101.25 26 WbN 286.87
11 ESE 118.12 27 WNW 286.88
12 SEbE 118.13 28 NWbW 303.75
13 SE 135.00 29 NW 320.62
14 SEbS 151.87 30 NWbN 320.63
15 SSE 151.88 31 NNW 337.50
16 SbE 168.75 32 NbW 354.37
1 N 354.38
</pre>
=={{header|Ada}}==
Inspired by [[#C++|the C++ program]], but without the need for a specific library.
<
procedure Box_The_Compass is
Line 133 ⟶ 261:
Put_Line(Degrees(Degrees'Base(I) * 11.25 + Difference(I mod 3)));
end loop;
end Box_The_Compass;</
Output:
Line 172 ⟶ 300:
354.37 | 32 | North by west
354.38 | 1 | North</pre>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<
[]STRING
Line 272 ⟶ 399:
))
OD
)</
Output:
<pre>
Line 345 ⟶ 472:
32|North by West |NbW | |343°07.50′|348°45.00′|354°22.49′
</pre>
=={{header|AppleScript}}==
===Functional===
OS X Yosemite onwards (uses Foundation classes for record handling etc)
Line 355 ⟶ 481:
Functional composition, allowing for additional languages, and different numbers of compass points – see the test section)
<
use scripting additions
Line 747 ⟶ 873:
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpper</
{{Out}}
<pre> 1 0.00° North 北
Line 782 ⟶ 908:
32 354.37° North by west 北微西
1 354.38° North 北 </pre>
----
===Simpler alternative===
<syntaxhighlight lang="applescript">on degreesToCompassPoint(degrees)
set cardinals to {"north", "east", "south", "west"}
set idx to ((degrees / 11.25) as integer) mod 32
set qIdx to idx mod 8
if (qIdx = 0) then return cardinals's item (idx div 8 + 1)
set ns to cardinals's item (3 - (idx + 24) div 16 mod 2 * 2)
set ew to cardinals's item ((idx div 16 + 1) * 2)
if (idx mod 16 > 7) then set qIdx to 8 - qIdx
if (qIdx = 1) then return ns & " by " & ew
if (qIdx = 2) then return ns & "-" & ns & ew
if (qIdx = 3) then return ns & ew & " by " & ns
if (qIdx = 4) then return ns & ew
if (qIdx = 5) then return ns & ew & " by " & ew
if (qIdx = 6) then return ew & "-" & ns & ew
return ew & " by " & ns -- qIdx = 7.
end degreesToCompassPoint
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
on task()
set output to {"", "Index Compass point Degrees"}
set inputs to {0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, ¬
118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, ¬
253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38}
repeat with i from 1 to (count inputs)
set degrees to inputs's item i
set cpid to (degreesToCompassPoint(degrees))'s id
set cpid's first item to (cpid's beginning) mod 32 + 64
set compassPoint to string id cpid
set degrees to degrees as text
set entry to {text -2 thru -1 of (space & ((i - 1) mod 32 + 1)), ¬
text 1 thru 18 of (compassPoint & " "), ¬
text (offset of "." in degrees) thru end of (" " & degrees)}
set end of output to join(entry, " ")
end repeat
join(output, linefeed)
end task
task()</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">"
Index Compass point Degrees
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"</syntaxhighlight>
=={{header|AutoHotkey}}==
{{trans|C++}}
{{works with|AutoHotkey_L}}
<
return Mod(floor(angle / 11.25 +0.5), 32) + 1
}
Line 824 ⟶ 1,035:
. " ", 1, 24) . SubStr(" " a, -5) . "`r`n" ;
}
clipboard := out</
;Output
<pre> 1 North 0.00
Line 859 ⟶ 1,070:
32 North by west 354.37
1 North 354.38</pre>
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
Local $avArray[33] = [0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, _
151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, _
Line 881 ⟶ 1,091:
StringFormat("%-20s", $namearray[Mod(Floor($Degree / 11.25 + 0.5), 32)]) & " : " & $Degree & @CRLF)
EndFunc ;==>Boxing_the_compass
</syntaxhighlight>
Output : <pre>0 : North : 0
Line 916 ⟶ 1,126:
31 : North by west : 354.37
0 : North : 354.38</pre>
=={{header|AWK}}==
<
BEGIN {
split("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",A," ");
Line 936 ⟶ 1,145:
printf "%6.2f : %2d\t%s\n",$1,box,A[box];
}
</syntaxhighlight>
Output:
<pre> 0.00 : 1 N
Line 972 ⟶ 1,181:
354.38 : 1 N</pre>
=={{header|
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">arraybase 1
dim names$ = {"North", "North by east", "North-northeast", "Northeast by north", "Northeast", "Northeast by east", "East-northeast", "East by north", "East", "East by south", "East-southeast", "Southeast by east", "Southeast", "Southeast by south", "South-southeast", "South by east", "South", "South by west", "South-southwest", "Southwest by south", "Southwest", "Southwest by west", "West-southwest", "West by south", "West", "West by north", "West-northwest", "Northwest by west", "Northwest", "Northwest by north", "North-northwest", "North by west", "North"}
dim grados = {0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38}
for i = grados[?,] to grados[?]
j = int((grados[i] + 5.625) / 11.25)
if j > 31 then j -= 32
print rjust(string(j),2); " "; ljust(string(names$[j+1]),20); grados[i]
next i</syntaxhighlight>
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
bearing() = 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, \
\ 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, \
Line 1,001 ⟶ 1,224:
pt$() = "north", "east", "south", "west"
= pt$(pt% DIV 8)
</syntaxhighlight>
Output:
<pre>0 1 North
Line 1,036 ⟶ 1,259:
354.37 32 North by west
354.38 1 North</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 dim names$(31)
110 restore 300
120 for c = 0 to ubound(names$)
130 read a$
140 names$(c) = a$
150 next c
160 dim degrees(32)
170 restore 380
180 for c = 0 to ubound(degrees)
190 read b
200 degrees(c) = b
210 next c
220 for i = 0 to ubound(degrees)
230 j = int((degrees(i)+5.625)/11.25)
240 if j > 31 then j = j-32
250 print using "####.##";degrees(i);
260 print using " ## ";j;
270 print names$(j)
280 next i
290 end
300 data "North","North by east","North-northeast","Northeast by north"
310 data "Northeast","Northeast by east","East-northeast","East by north"
320 data "East","East by south","East-southeast","Southeast by east"
330 data "Southeast","Southeast by south","South-southeast","South by east"
340 data "South","South by west","South-southwest","Southwest by south"
350 data "Southwest","Southwest by west","West-southwest","West by south"
360 data "West","West by north","West-northwest","Northwest by west","Northwest"
370 data "Northwest by north","North-northwest","North by west","North"
380 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
390 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
400 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
410 data 337.5,354.37,354.388</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 dim n$(31)
110 restore 300
120 for c = 0 to 31
130 read a$
140 n$(c) = a$
150 next c
160 dim d(32)
170 restore 380
180 for c = 0 to 32
190 read b
200 d(c) = b
210 next c
220 for i = 0 to 32
230 j = int((d(i)+5.625)/11.25)
240 if j > 31 then j = j-32
250 print using "####.##";d(i);
260 print using " ## ";j;
270 print n$(j)
280 next i
290 end
300 data "North","North by east","North-northeast","Northeast by north"
310 data "Northeast","Northeast by east","East-northeast","East by north"
320 data "East","East by south","East-southeast","Southeast by east"
330 data "Southeast","Southeast by south","South-southeast","South by east"
340 data "South","South by west","South-southwest","Southwest by south"
350 data "Southwest","Southwest by west","West-southwest","West by south"
360 data "West","West by north","West-northwest","Northwest by west","Northwest"
370 data "Northwest by north","North-northwest","North by west","North"
380 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
390 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
400 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
410 data 337.5,354.37,354.38</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|Quite BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 array n$
110 for c = 0 to 31
120 read a$
130 let n$(c) = a$
140 next c
150 array d
160 for c = 0 to 33
170 read b
180 let d(c) = b
190 next c
200 for i = 1 to 33
210 let j = int((d(i)+5.625)/11.25)
220 if j > 31 then let j = j-32
230 print d(i);" ";j;" ";n$(j)
240 next i
250 end
260 data "North","North by east","North-northeast","Northeast by north"
270 data "Northeast","Northeast by east","East-northeast","East by north"
280 data "East","East by south","East-southeast","Southeast by east"
290 data "Southeast","Southeast by south","South-southeast","South by east"
300 data "South","South by west","South-southwest","Southwest by south"
310 data "Southwest","Southwest by west","West-southwest","West by south"
320 data "West","West by north","West-northwest","Northwest by west","Northwest"
330 data "Northwest by north","North-northwest","North by west","North"
340 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
350 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
360 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
370 data 337.5,354.37,354.38</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
=={{header|Befunge}}==
<
^_@#!`*84:+1<v*9"}"*+55,,,".",,,$$_^#!:-1g60/+55\<
>_06g:v>55+,^>/5+55+/48*::::,,,,%:1+.9,:06p48*\-0v
Line 1,052 ⟶ 1,391:
W>0"tsaE"0"htron yb tsaE"0"tsaehtron-tsaE"0"tsae"v
#v"rtheast by north"0"Northeast"0"Northeast by "<<
^>"oN"0"tsaehtron-htroN"0"tsae yb htroN"0"htroN"01</
{{output}}
Line 1,088 ⟶ 1,427:
354.37 32 North by west
354.38 1 North</pre>
=={{header|C}}==
Like [[wp:Box the compass|Wikipedia's article]], this program uses indexes to count the headings. There are now 33 headings, from 1 to 33, because 0.0 and 354.38 are different angles. (This differs from the task pseudocode, which mapped the 32 compass points to indexes.)
<
int main()
Line 1,144 ⟶ 1,482:
return 0;
}</
<pre>
1 North 0.00
Line 1,180 ⟶ 1,518:
1 North 354.38
</pre>
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
namespace BoxTheCompass
{
class Compass
{
string[] cp = new string[] {"North", "North by east", "North-northeast", "Northeast by north", "Northeast","Northeast by east",
"East-northeast", "East by north", "East", "East by south", "East-southeast", "Southeast by east", "Southeast",
"Southeast by south", "South-southeast", "South by east", "South", "South by west", "South-southwest", "Southwest by south",
"Southwest", "Southwest by west", "West-southwest", "West by south", "West", "West by north", "West-northwest",
"Northwest by west", "Northwest", "Northwest by north", "North-northwest", "North by west", "North"};
public void compassHeading(float a)
{
int h = Convert.ToInt32(Math.Floor(a / 11.25f + .5f)) % 32;
Console.WriteLine( "{0,2}: {1,-22} : {2,6:N}",h + 1, cp[h], a );
}
};
class Program
{
static void Main(string[] args)
{
Compass c = new Compass();
float[] degs = new float[] {0.0f, 16.87f, 16.88f, 33.75f, 50.62f, 50.63f, 67.5f, 84.37f, 84.38f, 101.25f,
118.12f, 118.13f, 135.0f, 151.87f, 151.88f, 168.75f, 185.62f, 185.63f, 202.5f, 219.37f, 219.38f, 236.25f,
253.12f, 253.13f, 270.0f, 286.87f, 286.88f, 303.75f, 320.62f, 320.63f, 337.5f, 354.37f, 354.38f};
foreach (float d in degs)
c.compassHeading(d);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
</syntaxhighlight>
{{out}}
<pre>
1: North : 0.00
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.50
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.00
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.50
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.00
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.50
32: North by west : 354.37
1: North : 354.38
</pre>
=={{header|C++}}==
Using the Boost libraries
{{libheader|Boost}}
<
#include <boost/array.hpp>
#include <boost/assign/list_of.hpp>
Line 1,242 ⟶ 1,654:
}
return 0;
}</
Output:
<pre>
Line 1,279 ⟶ 1,691:
1 North 354.38
</pre>
=={{header|Clojure}}==
{{trans|Tcl}}
<
(:use [clojure.string :only [capitalize]]))
Line 1,383 ⟶ 1,718:
(apply str (map-indexed #(format "%2s %-18s %7.2f\n"
(inc (mod %1 32)) (angle2compass %2) %2)
headings)))</
Output:
<pre> 1 North 0.00
Line 1,418 ⟶ 1,753:
32 North by west 354.37
1 North 354.38</pre>
=={{header|COBOL}}==
Works with GnuCOBOL
<
program-id. box-compass.
data division.
Line 1,487 ⟶ 1,821:
display compass-point space show-degrees
end-perform
goback.</
{{out}}
Line 1,526 ⟶ 1,860:
1 North 354.38
</pre>
=={{header|D}}==
{{trans|Java}}
<
struct boxTheCompass {
Line 1,562 ⟶ 1,895:
heading.boxTheCompass, heading);
}
}</
{{out}}
<pre>1 North 0
Line 1,599 ⟶ 1,932:
===Alternative version===
<
import std.stdio;
Line 1,622 ⟶ 1,955:
writefln("%2d %18s %6.2f", i + 1, box[i], phi);
}
}</
{{out}}
<pre> 1 North 0.00
Line 1,657 ⟶ 1,990:
32 North by west 354.37
1 North 354.38</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Box_the_compass#Pascal Pascal].
=={{header|EasyLang}}==
{{trans|Kotlin}}
<syntaxhighlight>
func$ expand cp$ .
for c$ in strchars cp$
if c$ = "N"
r$ &= "north"
elif c$ = "E"
r$ &= "east"
elif c$ = "S"
r$ &= "south"
elif c$ = "W"
r$ &= "west"
elif c$ = "b"
r$ &= "by"
else
r$ &= "-"
.
.
h$ = strchar (strcode substr r$ 1 1 - 32)
return h$ & substr r$ 2 999
.
proc main . .
cp$[] = [ "N" "NbE" "N-NE" "NEbN" "NE" "NEbE" "E-NE" "EbN" "E" "EbS" "E-SE" "SEbE" "SE" "SEbS" "S-SE" "SbE" "S" "SbW" "S-SW" "SWbS" "SW" "SWbW" "W-SW" "WbS" "W" "WbN" "W-NW" "NWbW" "NW" "NWbN" "N-NW" "NbW" ]
print "Index Degrees Compass point"
print "----- ------- -------------"
for i = 0 to 32
ind = (i + 1) mod1 32
heading = i * 11.25
if i mod 3 = 1
heading += 5.62
elif i mod 3 = 2
heading -= 5.62
.
print ind & "\t" & heading & "\t" & expand cp$[ind]
.
.
main
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
defp head do
Enum.chunk(~w(north east south west north), 2, 1)
Line 1,683 ⟶ 2,057:
end
Box.compass</
{{out}}
Line 1,723 ⟶ 2,097:
=={{header|Euphoria}}==
<
"Northeast","Northeast by east","East-northeast","East by north","East",
"East by south","East-southeast","Southeast by east","Southeast","Southeast by south",
Line 1,745 ⟶ 2,119:
j = deg2ind(degrees[i])
printf(1, "%6.2f %2d %-22s\n", {degrees[i], j, names[j]})
end for</
Output:
Line 1,781 ⟶ 2,155:
354.37 32 North by west
354.38 1 North</pre>
=={{header|F_Sharp|F#}}==
{{trans|Perl}}
<
"North"; "North by east"; "North-northeast"; "Northeast by north";
"Northeast"; "Northeast by east"; "East-northeast"; "East by north";
Line 1,800 ⟶ 2,173:
|> List.iter (fun phi ->
let i = (int (phi * 32. / 360. + 0.5)) % 32
printf "%3d %18s %6.2f°\n" (i + 1) box.[i] phi)</
{{out}}
<pre> 1 North 0.00°
Line 1,835 ⟶ 2,208:
32 North by west 354.37°
1 North 354.38°</pre>
=={{header|Factor}}==
<
CONSTANT: box
Line 1,863 ⟶ 2,235:
dup 32 * 360 /f 0.5 + >integer 32 mod [ 1 + ] [ box nth ] bi
"%6.2f° %2d %s\n" printf
] each</
{{out}}
<pre>
Line 1,900 ⟶ 2,272:
354.38° 1 North
</pre>
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,940 ⟶ 2,311:
compasspoint = points(int(x))
end function compasspoint
end program Compass</
Output:
<pre>
Line 1,977 ⟶ 2,348:
1 North 354.38</pre>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 2,008 ⟶ 2,379:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> 0.00 0 North
Line 2,043 ⟶ 2,414:
354.37 31 North by west
354.38 0 North</pre>
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=823a56ac094b8963cf11f792b381fbcc Click this link to run this code]'''
<
Dim fDeg As Float[] = [0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38]
Dim cHeading As Collection = ["N": "North", "S": "South", "W": "West", "E": "East", "b": "by"]
Line 2,064 ⟶ 2,434:
Next
End</
Output:
<pre>
Line 2,101 ⟶ 2,471:
Index= 1 354.38 degrees = North
</pre>
=={{header|Go}}==
<
import "fmt"
Line 2,169 ⟶ 2,538:
fmt.Printf("%4d %-19s %7.2f°\n", index, degrees2compasspoint(h), h)
}
}</
<pre>
Index Compass point Degree
Line 2,206 ⟶ 2,575:
1 North 354.38°
</pre>
=={{header|Groovy}}==
<
def cardinalDirections = ["north", "east", "south", "west"]
Line 2,229 ⟶ 2,597:
354.37, 354.38].eachWithIndex { angle, index ->
println "${(index % 32) + 1}".padRight(3) + "${angle.asCompassPoint().padLeft(20)} $angle\u00b0"
}</
Output:
<pre>1 North 0.0°
Line 2,264 ⟶ 2,632:
32 North by west 354.37°
1 North 354.38°</pre>
=={{header|Haskell}}==
<
import Data.Maybe (fromMaybe)
Line 2,335 ⟶ 2,702:
main :: IO ()
main = mapM_ (printPointName . show) [0 .. 31]</
Output:
<pre> 1 North 0.00°
Line 2,370 ⟶ 2,737:
32 North by west 354.37°
1 North 354.38°</pre>
=={{header|Huginn}}==
<
import Text as text;
Line 2,417 ⟶ 2,783:
print( "{:2d} | {:6.2f}° | {}\n".format( n, d, c.degrees_to_compasspoint( d ) ) );
}
}</
Output:<pre> # | Angle | Compass point
---+---------|-------------------
Line 2,453 ⟶ 2,819:
32 | 354.37° | North by west
1 | 354.38° | North</pre>
=={{header|Icon}} and {{header|Unicon}}==
{{incomplete|Icon|354.38?}}
<
procedure main()
Line 2,481 ⟶ 2,846:
return dirs[round(((((d%360)+360)%360)/11.25)%32 + 1)]
end</
{{libheader|Icon Programming Library}}
Line 2,519 ⟶ 2,884:
31 north-northwest 337.50
32 north by west 354.37</pre>
=={{header|IS-BASIC}}==
<
110 STRING DR$(1 TO 33)*18
120 FOR I=1 TO 33
Line 2,534 ⟶ 2,898:
210 DATA North,North by east,North-northeast,Northeast by north,Northeast,Northeast by east,East-northeast,East by north,East,East by south,East-southeast,Southeast by east,Southeast,Southeast by south,South-southeast,South by east
220 DATA South,South by west,South-southwest,Southwest by south,Southwest,Southwest by west,West-southwest,West by south,West,West by north,West-northwest,Northwest by west,Northwest,Northwest by north,North-northwest,North by west,North
230 DATA 0.0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25,118.12,118.13,135.0,151.87,151.88,168.75,185.62,185.63,202.5,219.37,219.38,236.25,253.12,253.13,270.0,286.87,286.88,303.75,320.62,320.63,337.5,354.37,354.38</
Output:<pre> 0 1 North
Line 2,569 ⟶ 2,933:
354.37 32 North by west
354.38 1 North</pre>
=={{header|J}}==
<
subs=: 'N,north,S,south,E,east,W,west,b, by ,'
dirs=: subs (toupper@{., }.)@rplc~L:1 0&(<;._2) 0 :0 -. ' ',LF
Line 2,579 ⟶ 2,942:
)
indice=: 32 | 0.5 <.@+ %&11.25
deg2pnt=: dirs {~ indice</
Example use:
<
0 1 2 3 4 5 6 7 8 9
deg2pnt i.10
┌─────┬─────┬─────┬─────┬─────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐
│North│North│North│North│North│North│North by east│North by east│North by east│North by east│
└─────┴─────┴─────┴─────┴─────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘</
Required example:
<
1 North 0
2 North by east 16.87
Line 2,625 ⟶ 2,988:
31 North-northwest 337.5
32 North by west 354.37
1 North 354.38</
=={{header|Java}}==
<p>
For this task, I used an <kbd>enumeration</kbd> to contain all 32-points of the compass.<br />
The bounds and mid-points can then be derived using the <code>enum</code> ordinal value.
</p>
<p>
The <code>toString</code> method breaks down the <kbd>enum</kbd> name into characters, and
replaces each with there corresponding written value.
</p>
<syntaxhighlight lang="java">
enum Compass {
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;
float midpoint() {
float midpoint = (360 / 32f) * ordinal();
return midpoint == 0 ? 360 : midpoint;
}
float[] bounds() {
float midpoint = midpoint();
float boundB = midpoint + bound;
return new float[] {
}
static Compass parse(float degrees) {
float[]
float[] boundsN =
for (Compass value : Compass.values()) {
bounds =
if (degrees >= boundsN[0]
if (degrees >= bounds[0] && degrees < bounds[1])
return value;
}
return null;
}
@Override
String[]
int index = 0;
switch (letter) {
case 'W' -> strings[index] = "west";
}
index++;
}
String string
= strings[0].substring(0, 1).toUpperCase() +
strings[0].substring(1);
switch (strings.length) {
case 2 -> string += strings[1];
case 3 -> {
if (strings[1].equals("by")) {
string += " %s %s".formatted(strings[1], strings[2]);
} else {
string += "-%s%s".formatted(strings[1], strings[2]);
}
}
case 4 -> {
string += String.join(" ", strings[1], strings[2], strings[3]);
}
}
return string;
}
}
</syntaxhighlight>
<pre>
0 North 0.00
1 North by east 16.87
2 North-northeast 16.88
3 Northeast by north 33.75
4 Northeast 50.62
5 Northeast by east 50.63
6 East-northeast 67.50
7 East by north 84.37
9 East by south 101.25
10 East-southeast 118.12
11 Southeast by east 118.13
13 Southeast by south 151.87
14 South-southeast 151.88
15 South by east 168.75
17 South by west 185.63
18 South-southwest 202.50
19 Southwest by south 219.37
21 Southwest by west 236.25
22 West-southwest 253.12
23 West by south 253.13
25 West by north 286.87
26 West-northwest 286.88
27 Northwest by west 303.75
29 Northwest by north 320.63
30 North-northwest 337.50
31 North by west 354.37
</pre>
=={{header|JavaScript}}==
Line 2,717 ⟶ 3,106:
An iterative, web-based approach:
<
var tr = document.createElement('tr'),
td;
Line 2,762 ⟶ 3,151:
table.appendChild(tbody);
document.body.appendChild(table);
</syntaxhighlight>
Output:
<pre>1 North 0°
Line 2,801 ⟶ 3,190:
Functional composition, allowing for additional languages (and different numbers of compass points)
<
'use strict';
Line 3,039 ⟶ 3,428:
return compassTable(5, xs); // 2^5 -> 32 boxes
})();</
{{Out}}
Line 3,075 ⟶ 3,464:
32 354.37° North by west 北微西
1 354.38° North 北 </pre>
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
# Input: the input heading given as a number in degrees.
# Output: the corresponding integer index (from 0 to 31 inclusive)
# into the table, compassPoint, of compass point names.
def cpx:
(((. / 11.25) + 0.5)|floor % 32)
| if . < 0 then . + 32
else .
end;
# Compass point names
def compassPoint: [
"North",
"North by east",
"North-northeast",
"Northeast by north",
"Northeast",
"Northeast by east",
"East-northeast",
"East by north",
"East",
"East by south",
"East-southeast",
"Southeast by east",
"Southeast",
"Southeast by south",
"South-southeast",
"South by east",
"South",
"South by west",
"South-southwest",
"Southwest by south",
"Southwest",
"Southwest by west",
"West-southwest",
"West by south",
"West",
"West by north",
"West-northwest",
"Northwest by west",
"Northwest",
"Northwest by north",
"North-northwest",
"North by west"
];
### The task
# Input: heading in degrees
def degreesToCompassPoint:
compassPoint[cpx];
def r: [
0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5,
84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75,
185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0,
286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38
];
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
"Index Compass point Heading",
(r as $r
| range(0; $r|length)
| $r[.] as $h
| ((.%32) + 1) as $index # index as per requirements
| ($h | degreesToCompassPoint) as $d
| "\($index|lpad(3)) \($d|lpad(20)) \($h)°"
)
</syntaxhighlight>
{{output}}
<pre>
Index Compass point Heading
1 North 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°
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°
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|Julia}}==
Line 3,080 ⟶ 3,581:
{{trans|Python}}
<
function degree2compasspoint(d::Float64)
Line 3,102 ⟶ 3,603:
i % 3 == 2 && (d -= 5.62)
@printf("%2i %-17s %10.2f°\n", i % 32 + 1, degree2compasspoint(d), d)
end</
{{out}}
<pre> 1 North 0.00°
Line 3,139 ⟶ 3,639:
1 North 354.38°</pre>
=={{header|K}}==
The representation of the names was inspired by Tcl (etc.).
<syntaxhighlight lang="k"> d:("N;Nbe;N-ne;Nebn;Ne;Nebe;E-ne;Ebn;")
d,:("E;Ebs;E-se;Sebe;Se;Sebs;S-se;Sbe;")
d,:("S;Sbw;S-sw;Swbs;Sw;Swbw;W-sw;Wbs;")
d,:("W;Wbn;W-nw;Nwbw;Nw;Nwbn;N-nw;Nbw;N")
split:{1_'(&x=y)_ x:y,x}
dd:split[d;";"]
/ lookup table
s1:"NEWSnewsb-"
s2:("North";"East";"West";"South";"north";"east";"west";"south";" by ";"-")
c:.({`$x}'s1),'{`$x}'s2 / create the dictionary
cc:{,/{$c[`$$x]}'x} / lookup function
/ calculate the degrees
f:{m:x!3;(11.25*x)+:[1=m;+5.62;2=m;-5.62;0]}</syntaxhighlight>
The table:
<syntaxhighlight lang="k"> `0:{((2$(1+x!32))," ",(-19$cc@dd[x]),(6.2$f@x))}'!#dd
1 North 0.00
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.50
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.00
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.50
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.00
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.50
32 North by west 354.37
1 North 354.38</syntaxhighlight>
=={{header|Kotlin}}==
<
fun expand(cp: String): String {
Line 3,176 ⟶ 3,731:
println(f.format(index + 1, heading, expand(cp[index])))
}
}</
{{out}}
Line 3,216 ⟶ 3,771:
1 354.38 North
</pre>
=={{header|langur}}==
{{trans|D}}
<
"Northeast", "Northeast by east", "East-northeast", "East by north",
"East", "East by south", "East-southeast", "Southeast by east",
Line 3,238 ⟶ 3,792:
for .phi in .angles {
val .i =
writeln
}</
{{out}}
Line 3,279 ⟶ 3,833:
1 354.38 North
</pre>
=={{header|Lasso}}==
<
local(points = array)
loop(-from=0,-to=32) => {
Line 3,459 ⟶ 3,886:
#pformatted + ' | ' + (#counter < 10 ? ' ') + #counter + ' | ' + compassLong(compassShort->get(#counter)) + '\r'
^}</
{{out}}
Line 3,497 ⟶ 3,924:
354.37 | 32 | North by west
354.38 | 1 | North</pre>
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">dim point$( 32)
for i =1 to 32
read d$: point$( i) =d$
next i
for i = 0 to 32
heading = i *11.25
if ( i mod 3) =1 then
heading = heading +5.62
else
if ( i mod 3) =2 then heading = heading -5.62
end if
ind = i mod 32 +1
print ind, compasspoint$( heading), heading
next i
end
function compasspoint$( h)
x = h /11.25 +1.5
if (x >=33.0) then x =x -32.0
compasspoint$ = point$( int( x))
end function
data "North ", "North by east ", "North-northeast "
data "Northeast by north", "Northeast ", "Northeast by east ", "East-northeast "
data "East by north ", "East ", "East by south ", "East-southeast "
data "Southeast by east ", "Southeast ", "Southeast by south", "South-southeast "
data "South by east ", "South ", "South by west ", "South-southwest "
data "Southwest by south", "Southwest ", "Southwest by west ", "West-southwest "
data "West by south ", "West ", "West by north ", "West-northwest "
data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest "
data "North by west</syntaxhighlight>
Output:
<pre>1 North 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
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
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|LLVM}}==
{{trans|C}}
<
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 3,568 ⟶ 4,064:
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { argmemonly nounwind }</
{{out}}
<pre> 1 North 0.00
Line 3,603 ⟶ 4,099:
32 North by west 354.37
1 North 354.38</pre>
=={{header|Logo}}==
<
make "compass_points [ N NbE N-NE NEbN NE NEbE E-NE EbN
E EbS E-SE SEbE SE SEbS S-SE SbE
Line 3,669 ⟶ 4,164:
; and exit
bye
</syntaxhighlight>
Output:
Line 3,706 ⟶ 4,201:
354.37 | North by west | 32
354.38 | North | 1</pre>
=={{header|Lua}}==
{{trans|Logo}}
<
compass_points = { "N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN",
"E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE",
Line 3,757 ⟶ 4,251:
label = expand_point(abbr)
print(row_format:format(angle, label, index))
end</
Output:
Line 3,794 ⟶ 4,288:
354.37 | North by west | 32
354.38 | North | 1</pre>
=={{header|M2000 Interpreter}}==
In a For Next loop we can change loop variable inside block, but the actual loop hidden variable can't change so for next iteration we get the proper value.
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
Locale 1033 'change decimal point char to dot.
Line 3,826 ⟶ 4,319:
}
CheckIt
</syntaxhighlight>
{{out}}
Line 3,866 ⟶ 4,359:
354.38°| 1 | North
</pre >
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Map[{Floor[Mod[ #+5.625 , 360]/11.25]+1,#}&,input] ]//TableForm</
<pre>1 North 0.°
2 North by east 16.87°
Line 3,903 ⟶ 4,395:
32 North by west 354.37°
1 North 354.38°</pre>
=={{header|MATLAB}} / {{header|Octave}}==
<
b = ceil(mod(d+360/64,360)*32/360);
end; </
Output:
<pre>>> x=[0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38]';
Line 3,945 ⟶ 4,436:
354.37 : 32
354.38 : 1</pre>
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM RealStr IMPORT RealToStr;
Line 4,028 ⟶ 4,518:
ReadChar
END BoxTheCompass.</
=={{header|MUMPS}}==
The TCL implementation was the starting point, but this isn't an exact translation.
<
;This takes in a degree heading, nominally from 0 to 360, and returns the compass point name.
QUIT:((DEGREE<0)||(DEGREE>360)) "land lubber can't read a compass"
Line 4,065 ⟶ 4,554:
. WRITE $PIECE(X,"^"),?5,DIR,?40,$JUSTIFY($PIECE(POINTS,",",P),10,2),!
KILL POINTS,UP,LO,DIR,P,X
QUIT</
<p>Output:<pre>
Debugger executing 'BOXWRITE^COMPASS'
Line 4,101 ⟶ 4,590:
32 North by west 354.37
1 North 354.38</pre></p>
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary utf8
Line 4,304 ⟶ 4,792:
return table
</syntaxhighlight>
;Output
Line 4,377 ⟶ 4,865:
1 N North 354.380
</pre>
=={{header|Nim}}==
<
const
Line 4,408 ⟶ 4,895:
index = heading.toCompassIndex
compassHeading = heading.toCompassHeading.alignLeft(maxNameLength)
echo fmt"{index:>2} {compassHeading} {heading:6.2f}"</
Output:
<pre> 1 North 0.00
Line 4,443 ⟶ 4,930:
32 North by west 354.37
1 North 354.38</pre>
=={{header|Objeck}}==
<
class BoxCompass {
function : Main(args : String[]) ~ Nil {
Line 4,480 ⟶ 4,966:
}
}
</syntaxhighlight>
Output
Line 4,518 ⟶ 5,004:
1 North 354.38
</pre>
=={{header|OCaml}}==
<
let test_cases = [0.0; 16.87; 16.88; 33.75; 50.62; 50.63; 67.5;
84.37; 84.38; 101.25; 118.12; 118.13; 135.0;
Line 4,553 ⟶ 5,038:
List.iter (print_direction) test_cases;;
</syntaxhighlight>
Sample output:
<pre>
Line 4,590 ⟶ 5,075:
1 North 354.38
</pre>
=={{header|OoRexx}}==
<
Do
Line 4,851 ⟶ 5,335:
End
Exit
</syntaxhighlight>
;Output
Line 4,923 ⟶ 5,407:
1 N North 354.380° (354°22′48″)
</pre>
=={{header|PARI/GP}}==
<
"Northeast by east","East-northeast","East by north","East","East by south","East-southeast",
"Southeast by east","Southeast","Southeast by south","South-southeast","South by east","South",
Line 4,931 ⟶ 5,414:
"West by south","West","West by north","West-northwest","Northwest by west","Northwest",
"Northwest by north","North-northwest","North by west"][round(x*4/45)%32+1]};
for(i=0,32,print(i%32+1" "box(x=i*11.25+if(i%3==1,5.62,if(i%3==2,-5.62)))" "x))</
Output:
Line 4,967 ⟶ 5,450:
32 North by west 354.370000
1 North 354.380000</pre>
=={{header|Pascal}}==
{{trans|Fortran}}
<
function compasspoint(angle: real): string;
Line 5,006 ⟶ 5,488:
writeln((i mod 32) + 1:2, ' ', compasspoint(heading), ' ', heading:8:4);
end;
end.</
Output:
<pre>
Line 5,044 ⟶ 5,526:
1 North 354.3800
</pre>
=={{header|Perl}}==
Don't waste brain cells calculating names, not worth the effort. Code is probably shorter, faster, and easier to read this way.
<
my @names = (
Line 5,091 ⟶ 5,572:
my $i = int(($_ * 32 / 360) + .5) % 32;
printf "%3d %18s %6.2f°\n", $i + 1, $names[$i], $_;
}</
2 North by east 16.87°
3 North-northeast 16.88°
Line 5,123 ⟶ 5,604:
31 North-northwest 337.50°
32 North by west 354.37°
1 North 354.38°</
=={{header|Phix}}==
Unlike the Perl guy, for me the maths is the boring bit, building those strings is the fun!<br>
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get225</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p4</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">&</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" by "</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">p1</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" by "</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p4</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- eg {North by east,North-northeast,Northeast by north}</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- eg {Northeast by east,East-northeast,East by north}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get45</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">p3</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- North/East/South/West</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Northeast/Southwest</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Southeast/Northwest</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">get225</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- eg get225(1,North,Northeast,East)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Northeast/Southeast/Southwest/Northwest</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">get225</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- eg get225(0,East,Northeast,North)
-- -> {Northeast by east,East-northeast,East by north}</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get90</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get45</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> <span style="color: #000080;font-style:italic;">-- ie get45(1,North,East)
-- get45(
-- get45(
-- get45(0,West,North)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">compass_points</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get90</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"North"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"East"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"South"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"West"</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">33</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">test_point</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">11.25</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">5.62</span><span style="color: #0000FF;">*(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">compass_point</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_point</span><span style="color: #0000FF;">*</span><span style="color: #000000;">32</span><span style="color: #0000FF;">/</span><span style="color: #000000;">360</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">),</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%2d %-22s %6.2f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">compass_point</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">compass_points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">compass_point</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">test_point</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
If you like, you can regard d=1 (both of them) as "clockwise-name-inherit" and d=0 as "anti-clockwise-name-inherit".<br>
{{out}}
Line 5,272 ⟶ 5,692:
1 North 354.38
</pre>
of course the following (more sensible but less fun, full standalone program) way works just as well, and produces the same output
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">compass_points</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #008000;">"North"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North by east"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North-northeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Northeast by north"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Northeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Northeast by east"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"East-northeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"East by north"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"East"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"East by south"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"East-southeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Southeast by east"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Southeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Southeast by south"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South-southeast"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South by east"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"South"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South by west"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South-southwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Southwest by south"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Southwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Southwest by west"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"West-southwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"West by south"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"West"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"West by north"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"West-northwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Northwest by west"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Northwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Northwest by north"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North-northwest"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North by west"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">test_points</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16.87</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16.88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">33.75</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">50.62</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">50.63</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">84.37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">84.38</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">101.25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">118.12</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">118.13</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">135.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">151.87</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">151.88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">168.75</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">185.62</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">185.63</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">202.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">219.37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">219.38</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">236.25</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">253.12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">253.13</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">270.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">286.87</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">286.88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">303.75</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">320.62</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">320.63</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">337.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">354.37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">354.38</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_points</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">compass_point</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">32</span><span style="color: #0000FF;">/</span><span style="color: #000000;">360</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">),</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%2d %-22s %6.2f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">compass_point</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">compass_points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">compass_point</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">test_points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
=={{header|Picat}}==
{{trans|Nim}}
<syntaxhighlight lang="picat">go =>
Names = ["North", "North by east", "North-northeast", "Northeast by north",
"Northeast", "Northeast by east", "East-northeast", "East by north",
"East", "East by south", "East-southeast", "Southeast by east",
"Southeast", "Southeast by south","South-southeast", "South by east",
"South", "South by west", "South-southwest", "Southwest by south",
"Southwest", "Southwest by west", "West-southwest", "West by south",
"West", "West by north", "West-northwest", "Northwest by west",
"Northwest", "Northwest by north", "North-northwest", "North by west", "North"
],
foreach(I in 0..32)
J = I mod 32,
D = I * 11.25,
if I mod 3 == 1 then D := D + 5.62 end,
if I mod 3 == 2 then D := D - 5.62 end,
printf("%2d %-20s %6.2f\n", J+1, Names[J+1], D)
end,
nl.</syntaxhighlight>
{{out}}
<pre> 1 North 0.00
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.50
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.00
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.50
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.00
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.50
32 North by west 354.37
1 North 354.38</pre>
=={{header|PicoLisp}}==
<
(setq *Compass # Build lookup table
Line 5,323 ⟶ 5,800:
NIL
(cdr (heading H))
(round H 2) ) ) )</
Output:
<pre> 1 north 0.00
Line 5,358 ⟶ 5,835:
32 north by west 354.37
1 north 354.38</pre>
=={{header|PowerShell}}==
<
{
Line 5,375 ⟶ 5,851:
$x = 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38
$x | % { Convert-DegreeToDirection -Degree $_ }</
{{out}}
<pre>north
Line 5,411 ⟶ 5,887:
north</pre>
A more general solution allowing you to choose whether to use 4, 8, 16, or 32 compass points.
<
{
Line 5,433 ⟶ 5,909:
8 = ( Convert-DegreeToDirection -Degree $Degree -Points 8 )
4 = ( Convert-DegreeToDirection -Degree $Degree -Points 4 ) } }
$Values | Format-Table</
{{out}}
<pre>Degree 32 16 8 4
Line 5,470 ⟶ 5,946:
354.37 north by west north north north
354.38 north north north north</pre>
=={{header|Prolog}}==
Part 1 : The following knowledge base takes a heading in degrees and returns the correct 32-point compass heading. It can also go in the other direction.
<syntaxhighlight lang="prolog">
compassangle(1, 'North',n, 0.00).
compassangle(2, 'North by east', nbe, 11.25).
compassangle(3, 'North-northeast', nne,22.50).
compassangle(4, 'Northeast by north', nebn,33.75).
compassangle(5, 'Northeast', ne,45.00).
compassangle(6, 'Norteast by east', nebe,56.25).
compassangle(7, 'East-northeast', ene,67.50).
compassangle(8, 'East by North', ebn,78.75).
compassangle(9, 'East', e,90.00).
compassangle(10, 'East by south', ebs, 101.25).
compassangle(11, 'East-southeast', ese,112.50).
compassangle(12, 'Southeast by east', sebe, 123.75).
compassangle(13, 'Southeast', se, 135.00).
compassangle(14, 'Southeast by south', sebs, 146.25).
compassangle(15, 'South-southeast',sse, 157.50).
compassangle(16, 'South by east', sbe, 168.75).
compassangle(17, 'South', s, 180.00).
compassangle(18, 'South by west', sbw, 191.25).
compassangle(19, 'South-southwest', ssw, 202.50).
compassangle(20, 'Southwest by south', swbs, 213.75).
compassangle(21, 'Southwest', sw, 225.00).
compassangle(22, 'Southwest by west', swbw, 236.25).
compassangle(23, 'West-southwest', wsw, 247.50).
compassangle(24, 'West by south', wbs, 258.75).
compassangle(25, 'West', w, 270.00).
compassangle(26, 'West by north', wbn, 281.25).
compassangle(27, 'West-northwest', wnw, 292.50).
compassangle(28, 'Northwest by west', nwbw, 303.75).
compassangle(29, 'Northwest', nw, 315.00).
compassangle(30, 'Northwest by north', nwbn, 326.25).
compassangle(31, 'North-northwest', nnw, 337.50).
compassangle(32, 'North by west', nbw, 348.75).
compassangle(1, 'North', n, 360.00).
compassangle(Index , Name, Heading, Angle) :- nonvar(Angle), resolveindex(Angle, Index),
compassangle(Index,Name, Heading, _).
resolveindex(Angle, Index) :- N is Angle / 11.25 + 0.5, I is floor(N),Index is (I mod 32) + 1.
</syntaxhighlight>
Part 2 : The following rules print a table of indexes.
<syntaxhighlight lang="prolog">
printTableRow(Angle) :- compassangle(Index, Name, _, Angle),
write(Index), write(' '),
write(Name), write(' '),
write(Angle).
printTable([X|Xs]) :- printTableRow(X), nl, printTable(Xs),!.
printTable([]).
</syntaxhighlight>
The following query prints the required table.
<syntaxhighlight lang="prolog">
?- printTable([0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62,
185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38]).
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 Norteast 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
true.
</syntaxhighlight>
=={{header|PureBasic}}==
<
Data.s "N", "north", "E", "east", "W", "west", "S", "south", "b", " by " ;abbreviations, expansions
Data.s "N NbE N-NE NEbN NE NEbE E-NE EbN E EbS E-SE SEbE SE SEbS S-SE SbE" ;dirs
Line 5,544 ⟶ 6,109:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
Sample output:
<pre> 1 North 0.00
Line 5,579 ⟶ 6,144:
32 North by west 354.37
1 354.38</pre>
=={{header|Python}}==
<
majors *= 2 # no need for modulo later
quarter1 = 'N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N'.split(',')
Line 5,696 ⟶ 6,169:
elif m == 2: d -= 5.62
n = i % 32 + 1
print( '%2i %-18s %7.2f°' % (n, degrees2compasspoint(d), d) )</
;Output
Line 5,733 ⟶ 6,206:
1 North 354.38°</pre>
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|Liberty BASIC}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION compasspoint$ (h!)
DIM SHARED point$(32)
FOR i = 1 TO 32
READ d$: point$(i) = d$
NEXT i
FOR i = 0 TO 32
heading = i * 11.25
IF (i MOD 3) = 1 THEN
heading = heading + 5.62
ELSE
IF (i MOD 3) = 2 THEN heading = heading - 5.62
END IF
ind = i MOD 32 + 1
PRINT ind, compasspoint$(heading), heading
NEXT i
END
DATA "North ", "North by east ", "North-northeast ", "Northeast by north"
DATA "Northeast ", "Northeast by east ", "East-northeast ", "East by north "
DATA "East ", "East by south ", "East-southeast ", "Southeast by east "
DATA "Southeast ", "Southeast by south", "South-southeast ", "South by east "
DATA "South ", "South by west ", "South-southwest ", "Southwest by south"
DATA "Southwest ", "Southwest by west ", "West-southwest ", "West by south "
DATA "West ", "West by north ", "West-northwest ", "Northwest by west "
DATA "Northwest ", "Northwest by north", "North-northwest ", "North by west "
FUNCTION compasspoint$ (h)
x = h / 11.25 + 1.5
IF (x >= 33!) THEN x = x - 32!
compasspoint$ = point$(INT(x))
END FUNCTION</syntaxhighlight>
=={{header|Quackery}}==
<code>switch</code>, <code>case</code> and <code>otherwise</code> are define at [[Metaprogramming#Quackery]].
<syntaxhighlight lang="Quackery"> [ tuck space swap of
join
swap split drop echo$ ] is lecho$ ( $ n --> )
[ table ] is heading
$ "N NbE N-NE NEbN NE NEbE E-NE EbN
E EbS E-SE SEbE SE SEbS S-SE SbE
S SbW S-SW SWbS SW SWbW W-SW WbS
W WbN W-NW NWbW NW NWbN N-NW NbW"
nest$
witheach
[ $ "" swap
witheach
[ [ switch
char N case $ "north"
char E case $ "east"
char S case $ "south"
char W case $ "west"
char b case $ " by "
otherwise $ "-" ]
join ]
' heading put ]
[ 4 45 v* 1 2 v+ / 32 mod heading ] is degrees->compass$ ( n/d --> $ )
$ " 0.0 16.87 16.88 33.75 50.62 50.63
67.5 84.37 84.38 101.25 118.12 118.13
135.0 151.87 151.88 168.75 185.62 185.63
202.5 219.37 219.38 236.25 253.12 253.13
270.0 286.87 286.88 303.75 320.62 320.63
337.5 354.37 354.38"
nest$
witheach
[ i^ 32 mod 1+
dup 10 < if sp
echo sp
dup $->v drop
degrees->compass$ 19 lecho$
echo$ cr ]</syntaxhighlight>
{{out}}
<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|R}}==
R (also known as Arrr me hearties!) is ideally suited to this task. Here's an easy to understand but inefficient way to solve it:
<syntaxhighlight lang="rsplus">
# Build a table of directions
pts <- data.frame(
des = c("N", "NxE", "NNE", "NExN", "NE", "NExE", "ENE", "ExN",
"E", "ExS", "ESE", "SExE", "SE", "SExS", "SSE", "SxE",
"S", "SxW", "SSW", "SWxS", "SW", "SWxW", "WSW", "WxS",
"W", "WxN", "WNW", "NWxW", "NW", "NWxN", "NNW", "NxW",
"N")
)
pts$deg <- seq(0, 360, 360/32)
heading <- Vectorize(function(deg) {
res <- pts
# Calculate the absolute difference between each
# point on the table and the function input
res$diff <- abs(res$deg - deg)
# Sort the table by abs difference, return the first
res <- res[order(res$diff), ]
res[1,]$des[1]
})
</syntaxhighlight>
Here are the test inputs:
<syntaxhighlight lang="rsplus">
test <- data.frame(
deg = c( 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37,
84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75,
185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13,
270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37,
354.38)
)
test$heading <- heading(test$deg)
test
</syntaxhighlight>
Check that the output headings cover the full range of headings:
<syntaxhighlight lang="r">
all.equal(test$heading, pts$des)
</syntaxhighlight>
{{out}}
<pre>
> test
deg heading
1 0.00 N
2 16.87 NxE
3 16.88 NNE
4 33.75 NExN
5 50.62 NE
6 50.63 NExE
7 67.50 ENE
8 84.37 ExN
9 84.38 E
10 101.25 ExS
11 118.12 ESE
12 118.13 SExE
13 135.00 SE
14 151.87 SExS
15 151.88 SSE
16 168.75 SxE
17 185.62 S
18 185.63 SxW
19 202.50 SSW
20 219.37 SWxS
21 219.38 SW
22 236.25 SWxW
23 253.12 WSW
24 253.13 WxS
25 270.00 W
26 286.87 WxN
27 286.88 WNW
28 303.75 NWxW
29 320.62 NW
30 320.63 NWxN
31 337.50 NNW
32 354.37 NxW
33 354.38 N
> all.equal(test$heading, pts$des)
[1] TRUE
</pre>
=={{header|Racket}}==
<
;;; Generate the headings and boxes
Line 5,788 ⟶ 6,471:
(check-= (heading->box 5.62) 1 0)
(check-= (heading->box 5.63) 2 0)
(check-= (heading->box 16.87) 2 0))</
Output:
Line 5,826 ⟶ 6,509:
32 | 354.37 | North by west
1 | 354.38 | North</pre>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang="raku" line>sub point (Int $index) {
my $ix = $index % 32;
if $ix +& 1
{ "&point(($ix + 1) +& 28) by &point(((2 - ($ix +& 2)) * 4) + $ix +& 24)" }
elsif $ix +& 2
{ "&point(($ix + 2) +& 24)-&point(($ix +| 4) +& 28)" }
elsif $ix +& 4
{ "&point(($ix + 8) +& 16)&point(($ix +| 8) +& 24)" }
else
{ <north east south west>[$ix div 8]; }
}
sub test-angle ($ix) { $ix * 11.25 + (0, 5.62, -5.62)[ $ix % 3 ] }
sub angle-to-point(\𝜽) { floor 𝜽 / 360 * 32 + 0.5 }
for 0 .. 32 -> $ix {
my \𝜽 = test-angle($ix);
printf " %2d %6.2f° %s\n",
$ix % 32 + 1,
𝜽,
tc point angle-to-point 𝜽;
}</syntaxhighlight>
Output:
<pre> 1 0.00° North
2 16.87° North by east
3 16.88° North-northeast
4 33.75° Northeast by north
5 50.62° Northeast
6 50.63° Northeast by east
7 67.50° East-northeast
8 84.37° East by north
9 84.38° East
10 101.25° East by south
11 118.12° East-southeast
12 118.13° Southeast by east
13 135.00° Southeast
14 151.87° Southeast by south
15 151.88° South-southeast
16 168.75° South by east
17 185.62° South
18 185.63° South by west
19 202.50° South-southwest
20 219.37° Southwest by south
21 219.38° Southwest
22 236.25° Southwest by west
23 253.12° West-southwest
24 253.13° West by south
25 270.00° West
26 286.87° West by north
27 286.88° West-northwest
28 303.75° Northwest by west
29 320.62° Northwest
30 320.63° Northwest by north
31 337.50° North-northwest
32 354.37° North by west
1 354.38° North</pre>
=={{header|Red}}==
<syntaxhighlight 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
]</syntaxhighlight>
'''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.
<
Parse Arg degrees
If degrees='' Then
degrees=0
118.12 118.13 135 151.87 151.88 168.75 185.62 185.63 202.5,
219.37 219.38 236.25 253.12 253.13 270 286.87 286.88 303.75,
320.62 320.63 337.5 354.37 354.38
names='n nbe n-ne nebn ne nebe e-ne ebn e ebs e-se sebe se sebs s-se sbe',
's sbw s-sw swbs sw swbw w-sw wbs w wbn w-nw nwbw nw nwbn n-nw nbw'
dirs=
degsym='a1'x
Else
degsym='f8'x /* or degsym='a7'x */
Say right('--------',30) copies('-',20)
pad=' '
Do i=1 To words(degrees)
x=word(degrees,i)
Say right(format(x,,2)degsym,30-1) pad boxheading(x)
End
Exit /* stick a fork in it, we're all done*/
/*---------------------------------------------------------------------*/
boxheading:
y=arg(1)//360
If y<0 Then
y=360-y
z=word(names,trunc(max(1,(y/11.25+1.5)//nn))) /* degrees->heading */
/* n e s w are now replaced by north east south west, respectively */
Do k=1 To words(dirs)
d=word(dirs,k)
z=changestr(left(d,1),z,d)
End
Return changestr('b',z,' by ') /* expand 'b' ---? ' by '. */</syntaxhighlight>
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
{{out}}
<pre>
ºheading compass heading
Line 5,900 ⟶ 6,712:
=={{header|Ring}}==
<
# Project : Box the compass
Line 5,930 ⟶ 6,742:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 5,966 ⟶ 6,778:
354.37 31 North by west
354.38 0 North
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.9}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ { "north" "east" "south" "west" }
SWAP 8 / IP 1 + GET
LAST 1 + DUP 5 ≠ SWAP 1 IFTE GET
≫ '<span style="color:blue">I→QDT</span>' STO
≪ → pattern a b
≪ "" 1 pattern SIZE '''FOR''' j
pattern j DUP SUB
DUP "X" == a ROT DUP "Y" == b ROT IFTE IFTE
+ '''NEXT'''
≫ ≫ '<span style="color:blue">FILLXY</span>' STO
≪
11.25 / 0.5 + FLOOR 32 MOD
{ "X" "X by Y" "X-XY" "XY by X"
"XY" "XY by Y" "Y-XY" "Y by X" "Y" }
OVER 8 MOD 1 + GET SWAP <span style="color:blue">I→QDT</span> <span style="color:blue">FILLXY</span>
≫ '<span style="color:blue">→HDG</span>' STO
|
<span style="color:blue">I→QDT</span> ''( index → "wind1" "wind2" )'' // index ∈ [0,31]
Get wind1
Get wind2
<span style="color:blue">FILLXY</span> ''( "pattern" "a" "b" → "string" ) ''
Initialize output string and loop
get jth character of pattern
replace "X" by "a" and "Y" by b
add to output string
<span style="color:blue">→HDG</span> ''( angle → "heading" )'' // angle in degreees
Convert angle into compass index
Naming patterns
Get pattern, get winds and convert into heading
|}
The demonstration has been limited to a few significant cases, due to the poor display capabilities of typical RPL devices (e.g. 4 x 22 characters on a HP-28S) :
≪ <span style="color:red">{ 0 16.87 16.88 33.75 50.62 354.37 354.38 } { } 1 3</span> PICK SIZE '''FOR''' j OVER j GET <span style="color:blue">→HDG</span> + '''NEXT''' SWAP DROP ≫ EVAL
{{out}}
<pre>
1: { "north" "north by east" "north-northeast" "northeast by north" "northeast" "north by west" "north" }
</pre>
=={{header|Ruby}}==
<
[a,
"#{a} by #{b}",
Line 5,993 ⟶ 6,859:
index, name = heading degrees
printf "%2d %20s %6.2f\n", index, name.center(20), degrees
end</
{{output}}
Line 6,031 ⟶ 6,897:
=={{header|Run BASIC}}==
<
dim direct$(22)
direct$(1) = "y" 'by
Line 6,067 ⟶ 6,933:
data "N","N b e","N-ne","Ne b n","Ne","Ne b e","E-ne","E b n","E","E b s","E-se"
data "Se b e","Se","Se b s","S-se","S b e","S","S b w","S-sw","Sw b s","Sw","Sw b w"
data "W-sw","W b s","W","W b n","W-nw","Nw b w","Nw","Nw b n","N-nw","N b w"</
<code><table border=1>
<TR><TD align=right>1</td><td>North</td><td>0.0</td></tr>
Line 6,104 ⟶ 6,970:
</table>
</code>
=={{header|Rust}}==
{{trans|Kotlin}}
<
let mut out = String::new();
for c in cp.chars() {
Line 6,146 ⟶ 7,011:
);
}
}</
{{out}}
<pre>Index Degrees Compass point
Line 6,183 ⟶ 7,048:
32 354.37 north by west
1 354.38 north</pre>
=={{header|Scala}}==
Inspired by Java version
<
val cardinal = List("north", "east", "south", "west")
val pointDesc = List("1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1")
Line 6,207 ⟶ 7,071:
(0 to 32).map(i=>Triple(pointDeg(i),deg2ind(pointDeg(i)),pointName(deg2ind(pointDeg(i)))))
.map{t=>(printf("%s\t%18s\t%s°\n",t._2,t._3,t._1))}
}</
Output:
Line 6,245 ⟶ 7,109:
1 North 354.38°
</pre>
=={{header|Seed7}}==
<
include "float.s7i";
Line 6,270 ⟶ 7,133:
degrees[index] digits 2 lpad 6);
end for;
end func;</
Output:
Line 6,308 ⟶ 7,171:
1 North 354.38
</pre>
=={{header|Sidef}}==
{{trans|
<
var ix = (index % 32);
if (ix & 1) { "#{point((ix + 1) & 28)} by #{point(((2 - (ix & 2)) * 4) + ix & 24)}" }
Line 6,325 ⟶ 7,187:
var 𝜽 = test_angle(ix);
printf(" %2d %6.2f° %s\n", ix % 32 + 1, 𝜽, point(angle_to_point(𝜽)).tc);
}</
{{out}}
<pre>
Line 6,362 ⟶ 7,224:
1 354.38° North
</pre>
=={{header|smart BASIC}}==
<
GET SCREEN SIZE sw,sh
OPTION BASE 1
Line 6,400 ⟶ 7,261:
DATA "Northwest", "NW", "309.38", "315.00", "320.62", "Northwest by north", "NWbN", "320.63", "326.25", "331.87"
DATA "North-northwest", "NNW", "331.88", "337.50", "343.12", "North by west", "NbW", "343.13", "348.75", "354.37"
</syntaxhighlight>
Output:
<pre>
Line 6,438 ⟶ 7,299:
31 North-northwest NNW 331.88 337.50 343.12
32 North by west NbW 343.13 348.75 354.37
</pre>
=={{header|Swift}}==
Implementation:
<syntaxhighlight lang="swift">
import Foundation
extension Double {
var nearestQr: Double {(self * 4.0).rounded(.toNearestOrAwayFromZero) / 4.0}
}
extension Measurement<UnitAngle>/*: ExpressibleByFloatLiteral*/ {
var cos: Double { Darwin.cos(self.converted(to: .radians).value) }
var sin: Double { Darwin.sin(self.converted(to: .radians).value) }
}
struct Compass {
var bearing: Measurement<UnitAngle>
var style: Style = .RN
init(_ deg: Double, _ min: Double = 0, _ sec: Double = 0, style: Style = .RN
) {
self.bearing = .init(value: deg + min/60.0 + sec/360.0, unit: .degrees)
self.style = style
}
static func degreesToPoints(_ deg: Double) -> Double {
(deg/360 * 32).nearestQr
}
var point: Double {
Self.degreesToPoints(self.bearing.value)
}
var quad: (String,String) {
(bearing.cos < 0 ? "S" : "N", bearing.sin < 0 ? "W" : "E")
}
var step: (Int,Double) {
let temp = 8 - abs(abs(self.point - 16) - 8)
return (Int(temp),temp.truncatingRemainder(dividingBy: 1))
}
enum Style {case RN, USN, noBy}
var formats = ["N", "NxE", "NNE", "NExN", "NE", "NExE", "ENE", "ExN", "E"]
let fractions = ["¼","½","¾"]
var invertedPoints: [Int] {
switch self.style {
case .RN: [3,6,7]
case .USN: [3,7]
case .noBy: [1,5,7]
}
}
func named() -> String {
var (pt,frac) = self.step
var fracStr: String = ""
if frac != 0.0 {
if invertedPoints.contains(pt) {
pt += 1
fracStr = fractions.reversed()[Int(frac * 4) - 1] + "N"
} else {
fracStr = fractions[Int(frac * 4) - 1] + "E"
}
}
return (self.formats[pt] + fracStr)
.replacing(/(N|E)/) { $0.output.0 == "N" ? self.quad.0 : self.quad.1 }
.replacing(/x/) {_ in "by"}
}
}
</syntaxhighlight>
Execution:
<syntaxhighlight lang="swift">
let arr = [
000.00, 016.87, 016.88, 033.75, 050.62, 050.63, 067.50, 084.37, 084.38,
090.00, 101.25, 118.12, 118.13, 135.00, 151.87, 151.88, 168.75, 185.62,
185.63, 202.50, 219.37, 219.38, 236.25, 253.12, 253.13, 270.00, 286.87,
286.88, 303.75, 320.62, 320.63, 337.50, 354.37, 354.38
]
let arr2 = stride(from: 0, through: 360, by: 22.5/8.0)
let pointFormatter = NumberFormatter()
pointFormatter.minimumIntegerDigits = 2
pointFormatter.minimumFractionDigits = 2
for d in arr {
let c = Compass(d, style: .RN)
print(pointFormatter.string(from: c.point as NSNumber)!, c.dms , c.named())
}
</syntaxhighlight>
Result:
<pre>
00.00 000°00'00" N
01.50 016°52'12" NbyE½E
01.50 016°52'47" NbyE½E
03.00 033°45'00" NEbyN
04.50 050°37'11" NE½E
04.50 050°37'48" NE½E
06.00 067°30'00" ENE
07.50 084°22'12" E½N
07.50 084°22'47" E½N
08.00 090°00'00" E
09.00 101°15'00" EbyS
10.50 118°07'12" SEbyE½E
10.50 118°07'47" SEbyE½E
12.00 135°00'00" SE
13.50 151°52'12" SSE½E
13.50 151°52'47" SSE½E
15.00 168°45'00" SbyE
16.50 185°37'12" S½W
16.50 185°37'47" S½W
18.00 202°30'00" SSW
19.50 219°22'12" SW½S
19.50 219°22'47" SW½S
21.00 236°15'00" SWbyW
22.50 253°07'12" WbyS½S
22.50 253°07'47" WbyS½S
24.00 270°00'00" W
25.50 286°52'12" WbyN½N
25.50 286°52'47" WbyN½N
27.00 303°45'00" NWbyW
28.50 320°37'12" NW½N
28.50 320°37'47" NW½N
30.00 337°30'00" NNW
31.50 354°22'12" N½W
31.50 354°22'47" N½W
</pre>
=={{header|Tcl}}==
<
set dirs {
N NbE N-NE NEbN NE NEbE E-NE EbN E EbS E-SE SEbE SE SEbS S-SE SbE
Line 6,467 ⟶ 7,448:
# Pretty-print the results of converting an angle to a compass heading
puts [format "%2i %-18s %7.2f°" $index [angle2compass $heading] $heading]
}</
Output:
<pre>
Line 6,504 ⟶ 7,485:
1 North 354.38°
</pre>
=={{header|True BASIC}}==
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">DIM point$(32)
FUNCTION compasspoint$ (h)
LET x = h / 11.25 + 1.5
IF (x >= 33) THEN LET x = x - 32
LET compasspoint$ = point$(INT(x))
END FUNCTION
RESTORE
DATA "North ", "North by east ", "North-northeast ", "Northeast by north"
DATA "Northeast ", "Northeast by east ", "East-northeast ", "East by north "
DATA "East ", "East by south ", "East-southeast ", "Southeast by east "
DATA "Southeast ", "Southeast by south", "South-southeast ", "South by east "
DATA "South ", "South by west ", "South-southwest ", "Southwest by south"
DATA "Southwest ", "Southwest by west ", "West-southwest ", "West by south "
DATA "West ", "West by north ", "West-northwest ", "Northwest by west "
DATA "Northwest ", "Northwest by north", "North-northwest ", "North by west "
FOR i = 1 TO 32
READ d$
LET point$(i) = d$
NEXT i
FOR i = 0 TO 32
LET heading = i * 11.25
IF REMAINDER(i, 3) = 1 THEN
LET heading = heading + 5.62
ELSE
IF REMAINDER(i, 3) = 2 THEN LET heading = heading - 5.62
END IF
LET ind = REMAINDER(i, 32) + 1
PRINT ind, compasspoint$(heading), heading
NEXT i
END</syntaxhighlight>
=={{header|uBasic/4tH}}==
{{trans|C}}
Since uBasic is an integer interpreter, we have to do some scaling to perform this task.
<syntaxhighlight lang="text">Push 0, 1687, 1688, 3375, 5062, 5063, 6750, 8437, 8438, 10125, 11812, 11813
Push 13500, 15187, 15188, 16875, 18562, 18563, 20250, 21937, 21938, 23625
Push 25312, 25313, 27000, 28687, 28688, 30375, 32062, 32063, 33750, 35437
Line 6,561 ⟶ 7,577:
400 Print "North-northwest "; : Return
410 Print "North by west "; : Return
420 Print "North "; : Return</
{{out}}
<pre>
Line 6,600 ⟶ 7,616:
0 OK, 0:900
</pre>
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Line 6,607 ⟶ 7,622:
Requires the standard POSIX bc(1) command to perform floating-point arithmetic.
<
compass_points=( N NbE N-NE NEbN NE NEbE E-NE EbN
E EbS E-SE SEbE SE SEbS S-SE SbE
Line 6,656 ⟶ 7,671:
label="$(expand_point $abbr)"
printf "$row_format" $angle "$label" $index
done</
Output: <pre>Degrees | Closest Point | Index
Line 6,693 ⟶ 7,708:
354.38 | North | 1
</pre>
=={{header|VBA}}==
<
Dim compass_point As Integer
Dim compass_points_all As New Collection
Line 6,728 ⟶ 7,742:
Debug.Print test_points_all(i)
Next i
End Sub</
<pre> 1 North 0
2 North by east 16,87
Line 6,764 ⟶ 7,778:
=={{header|Visual Basic .NET}}==
<
Dim _points(32) As String
Line 6,810 ⟶ 7,824:
End Function
End Module
</syntaxhighlight>
Output:
<pre>
Line 6,846 ⟶ 7,860:
32: North by west 354.37°
1: North 354.38°
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
// 'cpx' returns integer index from 0 to 31 corresponding to compass point.
// Input heading h is in degrees. Note this index is a zero-based index
// suitable for indexing into the table of printable compass points,
// and is not the same as the index specified to be printed in the output.
var cpx = Fn.new { |h|
var x = (h/11.25+0.5).floor % 32
if (x < 0) x = x + 32
return x
}
// printable compass points
var compassPoint = [
"North",
"North by east",
"North-northeast",
"Northeast by north",
"Northeast",
"Northeast by east",
"East-northeast",
"East by north",
"East",
"East by south",
"East-southeast",
"Southeast by east",
"Southeast",
"Southeast by south",
"South-southeast",
"South by east",
"South",
"South by west",
"South-southwest",
"Southwest by south",
"Southwest",
"Southwest by west",
"West-southwest",
"West by south",
"West",
"West by north",
"West-northwest",
"Northwest by west",
"Northwest",
"Northwest by north",
"North-northwest",
"North by west"
]
// function required by task
var degreesToCompassPoint = Fn.new { |h| compassPoint[cpx.call(h)] }
var r = [
0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5,
84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75,
185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0,
286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38
]
System.print("Index Compass point Degree")
var i = 0
for (h in r) {
var index = i%32 + 1 // printable index computed per pseudocode
var d = degreesToCompassPoint.call(h)
Fmt.print("$4d $-19s $7.2f°", index, d, h)
i = i + 1
}</syntaxhighlight>
{{out}}
<pre>
Index Compass point Degree
1 North 0.00°
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.50°
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.00°
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.50°
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.00°
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.50°
32 North by west 354.37°
1 North 354.38°
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Deg2Index(Heading); \Return index from heading in degrees
real Heading;
def Sector = 360./32.;
return (fix(Heading/Sector) & $1F) + 1;
real Headings;
int CompassPoint, H, Index;
[Headings:= [
0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5,
84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75,
185.62, 185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0,
286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38];
CompassPoint:= [
"North", "North by east", "North-northeast", "Northeast by north",
"Northeast", "Northeast by east", "East-northeast", "East by north",
"East", "East by south", "East-southeast", "Southeast by east",
"Southeast", "Southeast by south", "South-southeast", "South by east",
"South", "South by west", "South-southwest", "Southwest by south",
"Southwest", "Southwest by west", "West-southwest", "West by south",
"West", "West by north", "West-northwest", "Northwest by west",
"Northwest", "Northwest by north", "North-northwest", "North by west"];
Text(0, "Index Heading Compass Point^m^j");
for H:= 0 to 33-1 do
[Index:= Deg2Index(Headings(H));
Format(3,0);
RlOut(0, float(Index));
Format(7,2);
RlOut(0, Headings(H));
Text(0, " ");
Text(0, CompassPoint(Index-1));
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
Index Heading Compass Point
1 0.00 North
2 16.87 North by east
3 16.88 North-northeast
4 33.75 Northeast by north
5 50.62 Northeast
6 50.63 Northeast by east
7 67.50 East-northeast
8 84.37 East by north
9 84.38 East
10 101.25 East by south
11 118.12 East-southeast
12 118.13 Southeast by east
13 135.00 Southeast
14 151.87 Southeast by south
15 151.88 South-southeast
16 168.75 South by east
17 185.62 South
18 185.63 South by west
19 202.50 South-southwest
20 219.37 Southwest by south
21 219.38 Southwest
22 236.25 Southwest by west
23 253.12 West-southwest
24 253.13 West by south
25 270.00 West
26 286.87 West by north
27 286.88 West-northwest
28 303.75 Northwest by west
29 320.62 Northwest
30 320.63 Northwest by north
31 337.50 North-northwest
32 354.37 North by west
1 354.38 North
</pre>
=={{header|Yabasic}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="yabasic">
dim point$(32)
Line 6,887 ⟶ 8,080:
70 data "West by south ", "West ", "West by north ", "West-northwest "
80 data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest "
90 data "North by west "</
=={{header|Zig}}==
{{works with|Zig|0.11.0}}
<syntaxhighlight lang="zig">const std = @import("std");</syntaxhighlight>
<syntaxhighlight lang="zig">/// Degrees is not constrained to the range 0 to 360
fn degreesToCompassPoint(degrees: f32) []const u8 {
var d = degrees + comptime (11.25 / 2.0);
while (d < 0) d += 360;
while (d >= 360) d -= 360;
const index: usize = @intFromFloat(@divFloor(d, 11.25));
const points: [32][]const u8 = comptime .{
"North", "North by east", "North-northeast", "Northeast by north",
"Northeast", "Northeast by east", "East-northeast", "East by north",
"East", "East by south", "East-southeast", "Southeast by east",
"Southeast", "Southeast by south", "South-southeast", "South by east",
"South", "South by west", "South-southwest", "Southwest by south",
"Southwest", "Southwest by west", "West-southwest", "West by south",
"West", "West by north", "West-northwest", "Northwest by west",
"Northwest", "Northwest by north", "North-northwest", "North by west",
};
return points[index];
}</syntaxhighlight>
<syntaxhighlight lang="zig">pub fn main() anyerror!void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Index Heading Compass point\n", .{});
for (0..33) |i| {
var heading = @as(f32, @floatFromInt(i)) * 11.25;
heading += switch (i % 3) {
1 => 5.62,
2 => -5.62,
else => 0,
};
const index = i % 32 + 1;
try stdout.print(" {d:2} {d:>6.2}° {s}\n", .{ index, heading, degreesToCompassPoint(heading) });
}
}</syntaxhighlight>
{{out}}
<pre>Index Heading Compass point
1 0.00° North
2 16.87° North by east
3 16.88° Northeast
...
31 337.50° Northwest
32 354.37° North by west
1 354.38° North</pre>
=={{header|zkl}}==
{{trans|AWK}}
<
"SEbE SE SEbS SSE SbE S SbW SSW SWbS SW SWbW "
"WSW WbS W WbN WNW NWbW NW NWbN NNW NbW").split(" ");
Line 6,907 ⟶ 8,149:
box:=compassBox(heading);
println("%6.2f\UB0; : %2d\t%s".fmt(heading,box,A[box]));
}</
{{out}}
<pre>
Line 6,920 ⟶ 8,162:
354.38 : 1 N
</pre>
=={{header|ZX Spectrum Basic}}==
{{trans|Liberty BASIC}}
<
20 DATA "Northeast by north","Northeast","Northeast by east","East-northeast"
30 DATA "East by north","East","East by south","East-southeast"
Line 6,949 ⟶ 8,190:
250 STOP
260 DEF FN m(i,n)=((i/n)-INT (i/n))*n : REM modulus function
</syntaxhighlight>
|