Box the compass: Difference between revisions
Content added Content deleted
m (→{{header|R}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25: | Line 25: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=11l>V majors = ‘north east south west’.split(‘ ’) |
||
majors *= 2 |
majors *= 2 |
||
V quarter1 = ‘N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N’.split(‘,’) |
V quarter1 = ‘N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N’.split(‘,’) |
||
Line 52: | Line 52: | ||
d -= 5.62 |
d -= 5.62 |
||
V n = i % 32 + 1 |
V n = i % 32 + 1 |
||
print(‘#2.0 #<18 #4.2°’.format(n, degrees2compasspoint(d), d))</ |
print(‘#2.0 #<18 #4.2°’.format(n, degrees2compasspoint(d), d))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 67: | Line 67: | ||
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. |
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}} |
{{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 |
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
Line 172: | Line 172: | ||
Position(x,y) PrintAngle(rh) |
Position(x,y) PrintAngle(rh) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Box_the_compass.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Box_the_compass.png Screenshot from Atari 8-bit computer] |
||
Line 199: | Line 199: | ||
Inspired by [[#C++|the C++ program]], but without the need for a specific library. |
Inspired by [[#C++|the C++ program]], but without the need for a specific library. |
||
< |
<syntaxhighlight lang=Ada>with Ada.Text_IO; |
||
procedure Box_The_Compass is |
procedure Box_The_Compass is |
||
Line 264: | Line 264: | ||
Put_Line(Degrees(Degrees'Base(I) * 11.25 + Difference(I mod 3))); |
Put_Line(Degrees(Degrees'Base(I) * 11.25 + Difference(I mod 3))); |
||
end loop; |
end loop; |
||
end Box_The_Compass;</ |
end Box_The_Compass;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 308: | Line 308: | ||
{{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].}} |
{{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''.}} |
{{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''.}} |
||
< |
<syntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script # |
||
[]STRING |
[]STRING |
||
Line 403: | Line 403: | ||
)) |
)) |
||
OD |
OD |
||
)</ |
)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 486: | Line 486: | ||
Functional composition, allowing for additional languages, and different numbers of compass points – see the test section) |
Functional composition, allowing for additional languages, and different numbers of compass points – see the test section) |
||
< |
<syntaxhighlight lang=AppleScript>use framework "Foundation" |
||
use scripting additions |
use scripting additions |
||
Line 878: | Line 878: | ||
((ca's NSString's stringWithString:(str))'s ¬ |
((ca's NSString's stringWithString:(str))'s ¬ |
||
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text |
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text |
||
end toUpper</ |
end toUpper</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 1 0.00° North 北 |
<pre> 1 0.00° North 北 |
||
Line 917: | Line 917: | ||
{{trans|C++}} |
{{trans|C++}} |
||
{{works with|AutoHotkey_L}} |
{{works with|AutoHotkey_L}} |
||
< |
<syntaxhighlight lang=AHK>get_Index(angle){ |
||
return Mod(floor(angle / 11.25 +0.5), 32) + 1 |
return Mod(floor(angle / 11.25 +0.5), 32) + 1 |
||
} |
} |
||
Line 955: | Line 955: | ||
. " ", 1, 24) . SubStr(" " a, -5) . "`r`n" ; |
. " ", 1, 24) . SubStr(" " a, -5) . "`r`n" ; |
||
} |
} |
||
clipboard := out</ |
clipboard := out</syntaxhighlight> |
||
;Output |
;Output |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 993: | Line 993: | ||
=={{header|AutoIt}}== |
=={{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, _ |
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, _ |
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 1,012: | Line 1,012: | ||
StringFormat("%-20s", $namearray[Mod(Floor($Degree / 11.25 + 0.5), 32)]) & " : " & $Degree & @CRLF) |
StringFormat("%-20s", $namearray[Mod(Floor($Degree / 11.25 + 0.5), 32)]) & " : " & $Degree & @CRLF) |
||
EndFunc ;==>Boxing_the_compass |
EndFunc ;==>Boxing_the_compass |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : <pre>0 : North : 0 |
Output : <pre>0 : North : 0 |
||
Line 1,049: | Line 1,049: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang=awk>#!/usr/bin/awk -f |
||
BEGIN { |
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," "); |
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 1,067: | Line 1,067: | ||
printf "%6.2f : %2d\t%s\n",$1,box,A[box]; |
printf "%6.2f : %2d\t%s\n",$1,box,A[box]; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> 0.00 : 1 N |
<pre> 0.00 : 1 N |
||
Line 1,104: | Line 1,104: | ||
=={{header|BASIC256}}== |
=={{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 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"} |
||
Line 1,114: | Line 1,114: | ||
if j > 31 then j -= 32 |
if j > 31 then j -= 32 |
||
print rjust(string(j),2); " "; ljust(string(names$[j+1]),20); grados[i] |
print rjust(string(j),2); " "; ljust(string(names$[j+1]),20); grados[i] |
||
next i</ |
next i</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang=bbcbasic> DIM bearing(32) |
||
bearing() = 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, \ |
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, \ |
\ 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, \ |
||
Line 1,145: | Line 1,145: | ||
pt$() = "north", "east", "south", "west" |
pt$() = "north", "east", "south", "west" |
||
= pt$(pt% DIV 8) |
= pt$(pt% DIV 8) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>0 1 North |
<pre>0 1 North |
||
Line 1,182: | Line 1,182: | ||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang=befunge>>>::"}"9**\4+3%79*9*5-*79*9*5--+:5>>>06p:55+%68*+v |
||
^_@#!`*84:+1<v*9"}"*+55,,,".",,,$$_^#!:-1g60/+55\< |
^_@#!`*84:+1<v*9"}"*+55,,,".",,,$$_^#!:-1g60/+55\< |
||
>_06g:v>55+,^>/5+55+/48*::::,,,,%:1+.9,:06p48*\-0v |
>_06g:v>55+,^>/5+55+/48*::::,,,,%:1+.9,:06p48*\-0v |
||
Line 1,196: | Line 1,196: | ||
W>0"tsaE"0"htron yb tsaE"0"tsaehtron-tsaE"0"tsae"v |
W>0"tsaE"0"htron yb tsaE"0"tsaehtron-tsaE"0"tsae"v |
||
#v"rtheast by north"0"Northeast"0"Northeast by "<< |
#v"rtheast by north"0"Northeast"0"Northeast by "<< |
||
^>"oN"0"tsaehtron-htroN"0"tsae yb htroN"0"htroN"01</ |
^>"oN"0"tsaehtron-htroN"0"tsae yb htroN"0"htroN"01</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,236: | Line 1,236: | ||
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.) |
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.) |
||
< |
<syntaxhighlight lang=C>#include <stdio.h> |
||
int main() |
int main() |
||
Line 1,288: | Line 1,288: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight>Output: |
||
<pre> |
<pre> |
||
1 North 0.00 |
1 North 0.00 |
||
Line 1,326: | Line 1,326: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang=csharp> |
||
using System; |
using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,363: | Line 1,363: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,404: | Line 1,404: | ||
Using the Boost libraries |
Using the Boost libraries |
||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
< |
<syntaxhighlight lang=cpp>#include <string> |
||
#include <boost/array.hpp> |
#include <boost/array.hpp> |
||
#include <boost/assign/list_of.hpp> |
#include <boost/assign/list_of.hpp> |
||
Line 1,462: | Line 1,462: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,502: | Line 1,502: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang=lisp>(ns boxing-the-compass |
||
(:use [clojure.string :only [capitalize]])) |
(:use [clojure.string :only [capitalize]])) |
||
Line 1,527: | Line 1,527: | ||
(apply str (map-indexed #(format "%2s %-18s %7.2f\n" |
(apply str (map-indexed #(format "%2s %-18s %7.2f\n" |
||
(inc (mod %1 32)) (angle2compass %2) %2) |
(inc (mod %1 32)) (angle2compass %2) %2) |
||
headings)))</ |
headings)))</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 1,565: | Line 1,565: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Works with GnuCOBOL |
Works with GnuCOBOL |
||
< |
<syntaxhighlight lang=cobol> identification division. |
||
program-id. box-compass. |
program-id. box-compass. |
||
data division. |
data division. |
||
Line 1,631: | Line 1,631: | ||
display compass-point space show-degrees |
display compass-point space show-degrees |
||
end-perform |
end-perform |
||
goback.</ |
goback.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,673: | Line 1,673: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=d>import std.stdio, std.string, std.math, std.array; |
||
struct boxTheCompass { |
struct boxTheCompass { |
||
Line 1,706: | Line 1,706: | ||
heading.boxTheCompass, heading); |
heading.boxTheCompass, heading); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 North 0 |
<pre>1 North 0 |
||
Line 1,743: | Line 1,743: | ||
===Alternative version=== |
===Alternative version=== |
||
< |
<syntaxhighlight lang=d>void main() { |
||
import std.stdio; |
import std.stdio; |
||
Line 1,766: | Line 1,766: | ||
writefln("%2d %18s %6.2f", i + 1, box[i], phi); |
writefln("%2d %18s %6.2f", i + 1, box[i], phi); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 1,807: | Line 1,807: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang=elixir>defmodule Box do |
||
defp head do |
defp head do |
||
Enum.chunk(~w(north east south west north), 2, 1) |
Enum.chunk(~w(north east south west north), 2, 1) |
||
Line 1,830: | Line 1,830: | ||
end |
end |
||
Box.compass</ |
Box.compass</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,870: | Line 1,870: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang=euphoria>constant names = {"North","North by east","North-northeast","Northeast by north", |
||
"Northeast","Northeast by east","East-northeast","East by north","East", |
"Northeast","Northeast by east","East-northeast","East by north","East", |
||
"East by south","East-southeast","Southeast by east","Southeast","Southeast by south", |
"East by south","East-southeast","Southeast by east","Southeast","Southeast by south", |
||
Line 1,892: | Line 1,892: | ||
j = deg2ind(degrees[i]) |
j = deg2ind(degrees[i]) |
||
printf(1, "%6.2f %2d %-22s\n", {degrees[i], j, names[j]}) |
printf(1, "%6.2f %2d %-22s\n", {degrees[i], j, names[j]}) |
||
end for</ |
end for</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,931: | Line 1,931: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang=fsharp>let box = [| |
||
"North"; "North by east"; "North-northeast"; "Northeast by north"; |
"North"; "North by east"; "North-northeast"; "Northeast by north"; |
||
"Northeast"; "Northeast by east"; "East-northeast"; "East by north"; |
"Northeast"; "Northeast by east"; "East-northeast"; "East by north"; |
||
Line 1,947: | Line 1,947: | ||
|> List.iter (fun phi -> |
|> List.iter (fun phi -> |
||
let i = (int (phi * 32. / 360. + 0.5)) % 32 |
let i = (int (phi * 32. / 360. + 0.5)) % 32 |
||
printf "%3d %18s %6.2f°\n" (i + 1) box.[i] phi)</ |
printf "%3d %18s %6.2f°\n" (i + 1) box.[i] phi)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 North 0.00° |
<pre> 1 North 0.00° |
||
Line 1,984: | Line 1,984: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang=Factor>USING: formatting kernel math sequences ; |
||
CONSTANT: box |
CONSTANT: box |
||
Line 2,010: | Line 2,010: | ||
dup 32 * 360 /f 0.5 + >integer 32 mod [ 1 + ] [ box nth ] bi |
dup 32 * 360 /f 0.5 + >integer 32 mod [ 1 + ] [ box nth ] bi |
||
"%6.2f° %2d %s\n" printf |
"%6.2f° %2d %s\n" printf |
||
] each</ |
] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,050: | Line 2,050: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang=fortran>Program Compass |
||
implicit none |
implicit none |
||
Line 2,087: | Line 2,087: | ||
compasspoint = points(int(x)) |
compasspoint = points(int(x)) |
||
end function compasspoint |
end function compasspoint |
||
end program Compass</ |
end program Compass</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,125: | Line 2,125: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang=freebasic>' version 04-11-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 2,156: | Line 2,156: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0.00 0 North |
<pre> 0.00 0 North |
||
Line 2,194: | Line 2,194: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=823a56ac094b8963cf11f792b381fbcc Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=823a56ac094b8963cf11f792b381fbcc Click this link to run this code]''' |
||
< |
<syntaxhighlight lang=gambas>Public Sub Main() |
||
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 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"] |
Dim cHeading As Collection = ["N": "North", "S": "South", "W": "West", "E": "East", "b": "by"] |
||
Line 2,212: | Line 2,212: | ||
Next |
Next |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,251: | Line 2,251: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang=go>package main |
||
import "fmt" |
import "fmt" |
||
Line 2,317: | Line 2,317: | ||
fmt.Printf("%4d %-19s %7.2f°\n", index, degrees2compasspoint(h), h) |
fmt.Printf("%4d %-19s %7.2f°\n", index, degrees2compasspoint(h), h) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
Index Compass point Degree |
Index Compass point Degree |
||
Line 2,356: | Line 2,356: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang=groovy>def asCompassPoint(angle) { |
||
def cardinalDirections = ["north", "east", "south", "west"] |
def cardinalDirections = ["north", "east", "south", "west"] |
||
Line 2,377: | Line 2,377: | ||
354.37, 354.38].eachWithIndex { angle, index -> |
354.37, 354.38].eachWithIndex { angle, index -> |
||
println "${(index % 32) + 1}".padRight(3) + "${angle.asCompassPoint().padLeft(20)} $angle\u00b0" |
println "${(index % 32) + 1}".padRight(3) + "${angle.asCompassPoint().padLeft(20)} $angle\u00b0" |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>1 North 0.0° |
<pre>1 North 0.0° |
||
Line 2,414: | Line 2,414: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=haskell>import Data.Char (toUpper) |
||
import Data.Maybe (fromMaybe) |
import Data.Maybe (fromMaybe) |
||
Line 2,483: | Line 2,483: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ (printPointName . show) [0 .. 31]</ |
main = mapM_ (printPointName . show) [0 .. 31]</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 1 North 0.00° |
<pre> 1 North 0.00° |
||
Line 2,520: | Line 2,520: | ||
=={{header|Huginn}}== |
=={{header|Huginn}}== |
||
< |
<syntaxhighlight lang=huginn>import Algorithms as algo; |
||
import Text as text; |
import Text as text; |
||
Line 2,565: | Line 2,565: | ||
print( "{:2d} | {:6.2f}° | {}\n".format( n, d, c.degrees_to_compasspoint( d ) ) ); |
print( "{:2d} | {:6.2f}° | {}\n".format( n, d, c.degrees_to_compasspoint( d ) ) ); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output:<pre> # | Angle | Compass point |
Output:<pre> # | Angle | Compass point |
||
---+---------|------------------- |
---+---------|------------------- |
||
Line 2,604: | Line 2,604: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
{{incomplete|Icon|354.38?}} |
{{incomplete|Icon|354.38?}} |
||
< |
<syntaxhighlight lang=Icon>link strings,numbers |
||
procedure main() |
procedure main() |
||
Line 2,629: | Line 2,629: | ||
return dirs[round(((((d%360)+360)%360)/11.25)%32 + 1)] |
return dirs[round(((((d%360)+360)%360)/11.25)%32 + 1)] |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 2,669: | Line 2,669: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang=IS-BASIC>100 PROGRAM "Compass.bas" |
||
110 STRING DR$(1 TO 33)*18 |
110 STRING DR$(1 TO 33)*18 |
||
120 FOR I=1 TO 33 |
120 FOR I=1 TO 33 |
||
Line 2,682: | Line 2,682: | ||
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 |
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 |
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</ |
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</syntaxhighlight> |
||
Output:<pre> 0 1 North |
Output:<pre> 0 1 North |
||
Line 2,720: | Line 2,720: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang=j>require'strings' |
||
subs=: 'N,north,S,south,E,east,W,west,b, by ,' |
subs=: 'N,north,S,south,E,east,W,west,b, by ,' |
||
dirs=: subs (toupper@{., }.)@rplc~L:1 0&(<;._2) 0 :0 -. ' ',LF |
dirs=: subs (toupper@{., }.)@rplc~L:1 0&(<;._2) 0 :0 -. ' ',LF |
||
Line 2,727: | Line 2,727: | ||
) |
) |
||
indice=: 32 | 0.5 <.@+ %&11.25 |
indice=: 32 | 0.5 <.@+ %&11.25 |
||
deg2pnt=: dirs {~ indice</ |
deg2pnt=: dirs {~ indice</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang=j> i.10 |
||
0 1 2 3 4 5 6 7 8 9 |
0 1 2 3 4 5 6 7 8 9 |
||
deg2pnt i.10 |
deg2pnt i.10 |
||
┌─────┬─────┬─────┬─────┬─────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐ |
┌─────┬─────┬─────┬─────┬─────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐ |
||
│North│North│North│North│North│North│North by east│North by east│North by east│North by east│ |
│North│North│North│North│North│North│North by east│North by east│North by east│North by east│ |
||
└─────┴─────┴─────┴─────┴─────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘</ |
└─────┴─────┴─────┴─────┴─────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘</syntaxhighlight> |
||
Required example: |
Required example: |
||
< |
<syntaxhighlight lang=j> (":@>:@indice,.' ',.>@deg2pnt,.' ',.":@,.)(*&11.25 + 5.62 * 0 1 _1 {~ 3&|) i.33 |
||
1 North 0 |
1 North 0 |
||
2 North by east 16.87 |
2 North by east 16.87 |
||
Line 2,773: | Line 2,773: | ||
31 North-northwest 337.5 |
31 North-northwest 337.5 |
||
32 North by west 354.37 |
32 North by west 354.37 |
||
1 North 354.38</ |
1 North 354.38</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Visual Basic .NET}} |
{{trans|Visual Basic .NET}} |
||
< |
<syntaxhighlight lang=java>public class BoxingTheCompass{ |
||
private static String[] points = new String[32]; |
private static String[] points = new String[32]; |
||
Line 2,825: | Line 2,825: | ||
return points[(int)Math.floor(testD % 32)]; |
return points[(int)Math.floor(testD % 32)]; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>1 North 0.0° |
<pre>1 North 0.0° |
||
Line 2,865: | Line 2,865: | ||
An iterative, web-based approach: |
An iterative, web-based approach: |
||
< |
<syntaxhighlight lang=javascript>function createRow(i, point, heading) { |
||
var tr = document.createElement('tr'), |
var tr = document.createElement('tr'), |
||
td; |
td; |
||
Line 2,910: | Line 2,910: | ||
table.appendChild(tbody); |
table.appendChild(tbody); |
||
document.body.appendChild(table); |
document.body.appendChild(table); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>1 North 0° |
<pre>1 North 0° |
||
Line 2,949: | Line 2,949: | ||
Functional composition, allowing for additional languages (and different numbers of compass points) |
Functional composition, allowing for additional languages (and different numbers of compass points) |
||
< |
<syntaxhighlight lang=JavaScript>(() => { |
||
'use strict'; |
'use strict'; |
||
Line 3,187: | Line 3,187: | ||
return compassTable(5, xs); // 2^5 -> 32 boxes |
return compassTable(5, xs); // 2^5 -> 32 boxes |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,228: | Line 3,228: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=julia>using Printf |
||
function degree2compasspoint(d::Float64) |
function degree2compasspoint(d::Float64) |
||
Line 3,250: | Line 3,250: | ||
i % 3 == 2 && (d -= 5.62) |
i % 3 == 2 && (d -= 5.62) |
||
@printf("%2i %-17s %10.2f°\n", i % 32 + 1, degree2compasspoint(d), d) |
@printf("%2i %-17s %10.2f°\n", i % 32 + 1, degree2compasspoint(d), d) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,290: | Line 3,290: | ||
The representation of the names was inspired by Tcl (etc.). |
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,:("E;Ebs;E-se;Sebe;Se;Sebs;S-se;Sbe;") |
||
d,:("S;Sbw;S-sw;Swbs;Sw;Swbw;W-sw;Wbs;") |
d,:("S;Sbw;S-sw;Swbs;Sw;Swbw;W-sw;Wbs;") |
||
Line 3,305: | Line 3,305: | ||
/ calculate the degrees |
/ calculate the degrees |
||
f:{m:x!3;(11.25*x)+:[1=m;+5.62;2=m;-5.62;0]}</ |
f:{m:x!3;(11.25*x)+:[1=m;+5.62;2=m;-5.62;0]}</syntaxhighlight> |
||
The table: |
The table: |
||
< |
<syntaxhighlight lang=K> `0:{((2$(1+x!32))," ",(-19$cc@dd[x]),(6.2$f@x))}'!#dd |
||
1 North 0.00 |
1 North 0.00 |
||
2 North by east 16.87 |
2 North by east 16.87 |
||
Line 3,341: | Line 3,341: | ||
31 North-northwest 337.50 |
31 North-northwest 337.50 |
||
32 North by west 354.37 |
32 North by west 354.37 |
||
1 North 354.38</ |
1 North 354.38</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>// version 1.1.2 |
||
fun expand(cp: String): String { |
fun expand(cp: String): String { |
||
Line 3,380: | Line 3,380: | ||
println(f.format(index + 1, heading, expand(cp[index]))) |
println(f.format(index + 1, heading, expand(cp[index]))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,424: | Line 3,424: | ||
{{trans|D}} |
{{trans|D}} |
||
{{works with|langur|0.6.13}} |
{{works with|langur|0.6.13}} |
||
< |
<syntaxhighlight lang=langur>val .box = ["North", "North by east", "North-northeast", "Northeast by north", |
||
"Northeast", "Northeast by east", "East-northeast", "East by north", |
"Northeast", "Northeast by east", "East-northeast", "East by north", |
||
"East", "East by south", "East-southeast", "Southeast by east", |
"East", "East by south", "East-southeast", "Southeast by east", |
||
Line 3,445: | Line 3,445: | ||
val .i = truncate(.phi x 32 / 360 + 0.5) rem 32 + 1 |
val .i = truncate(.phi x 32 / 360 + 0.5) rem 32 + 1 |
||
writeln $"\.i:5; \.phi:r2:6; \.box[.i];" |
writeln $"\.i:5; \.phi:r2:6; \.box[.i];" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,486: | Line 3,486: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang=Lasso>define pointsarray() => { |
||
local(points = array) |
local(points = array) |
||
loop(-from=0,-to=32) => { |
loop(-from=0,-to=32) => { |
||
Line 3,537: | Line 3,537: | ||
#pformatted + ' | ' + (#counter < 10 ? ' ') + #counter + ' | ' + compassLong(compassShort->get(#counter)) + '\r' |
#pformatted + ' | ' + (#counter < 10 ? ' ') + #counter + ' | ' + compassLong(compassShort->get(#counter)) + '\r' |
||
^}</ |
^}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,577: | Line 3,577: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang=lb>dim point$( 32) |
||
for i =1 to 32 |
for i =1 to 32 |
||
Line 3,610: | Line 3,610: | ||
data "West by south ", "West ", "West by north ", "West-northwest " |
data "West by south ", "West ", "West by north ", "West-northwest " |
||
data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest " |
data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest " |
||
data "North by west</ |
data "North by west</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,649: | Line 3,649: | ||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=llvm>; This is not strictly LLVM, as it uses the C library function "printf". |
||
; LLVM does not provide a way to print values, so the alternative would be |
; 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. |
; to just load the string into memory, and that would be boring. |
||
Line 3,717: | Line 3,717: | ||
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 #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 }</ |
attributes #1 = { argmemonly nounwind }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 3,754: | Line 3,754: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang=logo>; List of abbreviated compass point labels |
||
make "compass_points [ N NbE N-NE NEbN NE NEbE E-NE EbN |
make "compass_points [ N NbE N-NE NEbN NE NEbE E-NE EbN |
||
E EbS E-SE SEbE SE SEbS S-SE SbE |
E EbS E-SE SEbE SE SEbS S-SE SbE |
||
Line 3,818: | Line 3,818: | ||
; and exit |
; and exit |
||
bye |
bye |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 3,858: | Line 3,858: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|Logo}} |
{{trans|Logo}} |
||
< |
<syntaxhighlight lang=lua>-- List of abbreviated compass point labels |
||
compass_points = { "N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN", |
compass_points = { "N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN", |
||
"E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE", |
"E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE", |
||
Line 3,906: | Line 3,906: | ||
label = expand_point(abbr) |
label = expand_point(abbr) |
||
print(row_format:format(angle, label, index)) |
print(row_format:format(angle, label, index)) |
||
end</ |
end</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,947: | Line 3,947: | ||
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. |
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 { |
Module CheckIt { |
||
Locale 1033 'change decimal point char to dot. |
Locale 1033 'change decimal point char to dot. |
||
Line 3,975: | Line 3,975: | ||
} |
} |
||
CheckIt |
CheckIt |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,017: | Line 4,017: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>Map[List[Part[#,1], dirs[[Part[#,1]]], ToString@Part[#,2]<>"°"]&, |
||
Map[{Floor[Mod[ #+5.625 , 360]/11.25]+1,#}&,input] ]//TableForm</ |
Map[{Floor[Mod[ #+5.625 , 360]/11.25]+1,#}&,input] ]//TableForm</syntaxhighlight> |
||
<pre>1 North 0.° |
<pre>1 North 0.° |
||
2 North by east 16.87° |
2 North by east 16.87° |
||
Line 4,054: | Line 4,054: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang=MATLAB>function b = compassbox(d) |
||
b = ceil(mod(d+360/64,360)*32/360); |
b = ceil(mod(d+360/64,360)*32/360); |
||
end; </ |
end; </syntaxhighlight> |
||
Output: |
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]'; |
<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 4,096: | Line 4,096: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang=modula2>MODULE BoxTheCompass; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM RealStr IMPORT RealToStr; |
FROM RealStr IMPORT RealToStr; |
||
Line 4,177: | Line 4,177: | ||
ReadChar |
ReadChar |
||
END BoxTheCompass.</ |
END BoxTheCompass.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
The TCL implementation was the starting point, but this isn't an exact translation. |
The TCL implementation was the starting point, but this isn't an exact translation. |
||
< |
<syntaxhighlight lang=MUMPS>BOXING(DEGREE) |
||
;This takes in a degree heading, nominally from 0 to 360, and returns the compass point name. |
;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" |
QUIT:((DEGREE<0)||(DEGREE>360)) "land lubber can't read a compass" |
||
Line 4,214: | Line 4,214: | ||
. WRITE $PIECE(X,"^"),?5,DIR,?40,$JUSTIFY($PIECE(POINTS,",",P),10,2),! |
. WRITE $PIECE(X,"^"),?5,DIR,?40,$JUSTIFY($PIECE(POINTS,",",P),10,2),! |
||
KILL POINTS,UP,LO,DIR,P,X |
KILL POINTS,UP,LO,DIR,P,X |
||
QUIT</ |
QUIT</syntaxhighlight> |
||
<p>Output:<pre> |
<p>Output:<pre> |
||
Debugger executing 'BOXWRITE^COMPASS' |
Debugger executing 'BOXWRITE^COMPASS' |
||
Line 4,252: | Line 4,252: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang=NetRexx>/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary utf8 |
options replace format comments java crossref savelog symbols nobinary utf8 |
||
Line 4,453: | Line 4,453: | ||
return table |
return table |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
Line 4,528: | Line 4,528: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>import math, sequtils, strformat, strutils |
||
const |
const |
||
Line 4,557: | Line 4,557: | ||
index = heading.toCompassIndex |
index = heading.toCompassIndex |
||
compassHeading = heading.toCompassHeading.alignLeft(maxNameLength) |
compassHeading = heading.toCompassHeading.alignLeft(maxNameLength) |
||
echo fmt"{index:>2} {compassHeading} {heading:6.2f}"</ |
echo fmt"{index:>2} {compassHeading} {heading:6.2f}"</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 4,594: | Line 4,594: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang=objeck> |
||
class BoxCompass { |
class BoxCompass { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 4,629: | Line 4,629: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output |
Output |
||
Line 4,669: | Line 4,669: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang=ocaml> |
||
let test_cases = [0.0; 16.87; 16.88; 33.75; 50.62; 50.63; 67.5; |
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; |
84.37; 84.38; 101.25; 118.12; 118.13; 135.0; |
||
Line 4,702: | Line 4,702: | ||
List.iter (print_direction) test_cases;; |
List.iter (print_direction) test_cases;; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output: |
Sample output: |
||
<pre> |
<pre> |
||
Line 4,741: | Line 4,741: | ||
=={{header|OoRexx}}== |
=={{header|OoRexx}}== |
||
< |
<syntaxhighlight lang=OOREXX>/* Rexx */ |
||
Do |
Do |
||
Line 5,000: | Line 5,000: | ||
End |
End |
||
Exit |
Exit |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
Line 5,074: | Line 5,074: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang=parigp>box(x)={["North","North by east","North-northeast","Northeast by north","Northeast", |
||
"Northeast by east","East-northeast","East by north","East","East by south","East-southeast", |
"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", |
"Southeast by east","Southeast","Southeast by south","South-southeast","South by east","South", |
||
Line 5,080: | Line 5,080: | ||
"West by south","West","West by north","West-northwest","Northwest by west","Northwest", |
"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]}; |
"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))</ |
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))</syntaxhighlight> |
||
Output: |
Output: |
||
Line 5,119: | Line 5,119: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{trans|Fortran}} |
{{trans|Fortran}} |
||
< |
<syntaxhighlight lang=pascal>program BoxTheCompass(output); |
||
function compasspoint(angle: real): string; |
function compasspoint(angle: real): string; |
||
Line 5,155: | Line 5,155: | ||
writeln((i mod 32) + 1:2, ' ', compasspoint(heading), ' ', heading:8:4); |
writeln((i mod 32) + 1:2, ' ', compasspoint(heading), ' ', heading:8:4); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 5,196: | Line 5,196: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Don't waste brain cells calculating names, not worth the effort. Code is probably shorter, faster, and easier to read this way. |
Don't waste brain cells calculating names, not worth the effort. Code is probably shorter, faster, and easier to read this way. |
||
< |
<syntaxhighlight lang=Perl>use utf8; |
||
my @names = ( |
my @names = ( |
||
Line 5,240: | Line 5,240: | ||
my $i = int(($_ * 32 / 360) + .5) % 32; |
my $i = int(($_ * 32 / 360) + .5) % 32; |
||
printf "%3d %18s %6.2f°\n", $i + 1, $names[$i], $_; |
printf "%3d %18s %6.2f°\n", $i + 1, $names[$i], $_; |
||
}</ |
}</syntaxhighlight>output<syntaxhighlight lang=text> 1 North 0.00° |
||
2 North by east 16.87° |
2 North by east 16.87° |
||
3 North-northeast 16.88° |
3 North-northeast 16.88° |
||
Line 5,272: | Line 5,272: | ||
31 North-northwest 337.50° |
31 North-northwest 337.50° |
||
32 North by west 354.37° |
32 North by west 354.37° |
||
1 North 354.38°</ |
1 North 354.38°</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Unlike the Perl guy, for me the maths is the boring bit, building those strings is the fun!<br> |
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;">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: #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> |
||
Line 5,323: | Line 5,323: | ||
<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: #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> |
<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> |
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}} |
{{out}} |
||
Line 5,362: | Line 5,362: | ||
</pre> |
</pre> |
||
of course the following (more sensible but less fun, full standalone program) way works just as well, and produces the same output |
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;">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: #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> |
||
Line 5,379: | Line 5,379: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang=Picat>go => |
||
Names = ["North", "North by east", "North-northeast", "Northeast by north", |
Names = ["North", "North by east", "North-northeast", "Northeast by north", |
||
"Northeast", "Northeast by east", "East-northeast", "East by north", |
"Northeast", "Northeast by east", "East-northeast", "East by north", |
||
Line 5,400: | Line 5,400: | ||
printf("%2d %-20s %6.2f\n", J+1, Names[J+1], D) |
printf("%2d %-20s %6.2f\n", J+1, Names[J+1], D) |
||
end, |
end, |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,438: | Line 5,438: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(scl 3) |
||
(setq *Compass # Build lookup table |
(setq *Compass # Build lookup table |
||
Line 5,471: | Line 5,471: | ||
NIL |
NIL |
||
(cdr (heading H)) |
(cdr (heading H)) |
||
(round H 2) ) ) )</ |
(round H 2) ) ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> 1 north 0.00 |
<pre> 1 north 0.00 |
||
Line 5,508: | Line 5,508: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang=powershell>function Convert-DegreeToDirection ( [double]$Degree ) |
||
{ |
{ |
||
Line 5,523: | Line 5,523: | ||
$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 = 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 $_ }</ |
$x | % { Convert-DegreeToDirection -Degree $_ }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>north |
<pre>north |
||
Line 5,559: | Line 5,559: | ||
north</pre> |
north</pre> |
||
A more general solution allowing you to choose whether to use 4, 8, 16, or 32 compass points. |
A more general solution allowing you to choose whether to use 4, 8, 16, or 32 compass points. |
||
< |
<syntaxhighlight lang=powershell>function Convert-DegreeToDirection ( [double]$Degree, [int]$Points ) |
||
{ |
{ |
||
Line 5,581: | Line 5,581: | ||
8 = ( Convert-DegreeToDirection -Degree $Degree -Points 8 ) |
8 = ( Convert-DegreeToDirection -Degree $Degree -Points 8 ) |
||
4 = ( Convert-DegreeToDirection -Degree $Degree -Points 4 ) } } |
4 = ( Convert-DegreeToDirection -Degree $Degree -Points 4 ) } } |
||
$Values | Format-Table</ |
$Values | Format-Table</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Degree 32 16 8 4 |
<pre>Degree 32 16 8 4 |
||
Line 5,621: | Line 5,621: | ||
=={{header|Prolog}}== |
=={{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. |
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(1, 'North',n, 0.00). |
||
compassangle(2, 'North by east', nbe, 11.25). |
compassangle(2, 'North by east', nbe, 11.25). |
||
Line 5,659: | Line 5,659: | ||
resolveindex(Angle, Index) :- N is Angle / 11.25 + 0.5, I is floor(N),Index is (I mod 32) + 1. |
resolveindex(Angle, Index) :- N is Angle / 11.25 + 0.5, I is floor(N),Index is (I mod 32) + 1. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Part 2 : The following rules print a table of indexes. |
Part 2 : The following rules print a table of indexes. |
||
< |
<syntaxhighlight lang=prolog> |
||
printTableRow(Angle) :- compassangle(Index, Name, _, Angle), |
printTableRow(Angle) :- compassangle(Index, Name, _, Angle), |
||
write(Index), write(' '), |
write(Index), write(' '), |
||
Line 5,669: | Line 5,669: | ||
printTable([X|Xs]) :- printTableRow(X), nl, printTable(Xs),!. |
printTable([X|Xs]) :- printTableRow(X), nl, printTable(Xs),!. |
||
printTable([]). |
printTable([]). |
||
</syntaxhighlight> |
|||
</lang> |
|||
The following query prints the required table. |
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, |
?- 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]). |
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 5,708: | Line 5,708: | ||
1 North 354.38 |
1 North 354.38 |
||
true. |
true. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang=PureBasic>DataSection |
||
Data.s "N", "north", "E", "east", "W", "west", "S", "south", "b", " by " ;abbreviations, expansions |
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 |
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,783: | Line 5,783: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre> 1 North 0.00 |
<pre> 1 North 0.00 |
||
Line 5,820: | Line 5,820: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang=python>majors = 'north east south west'.split() |
||
majors *= 2 # no need for modulo later |
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(',') |
quarter1 = 'N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N'.split(',') |
||
Line 5,844: | Line 5,844: | ||
elif m == 2: d -= 5.62 |
elif m == 2: d -= 5.62 |
||
n = i % 32 + 1 |
n = i % 32 + 1 |
||
print( '%2i %-18s %7.2f°' % (n, degrees2compasspoint(d), d) )</ |
print( '%2i %-18s %7.2f°' % (n, degrees2compasspoint(d), d) )</syntaxhighlight> |
||
;Output |
;Output |
||
Line 5,886: | Line 5,886: | ||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
< |
<syntaxhighlight lang=qbasic>DECLARE FUNCTION compasspoint$ (h!) |
||
DIM SHARED point$(32) |
DIM SHARED point$(32) |
||
Line 5,919: | Line 5,919: | ||
IF (x >= 33!) THEN x = x - 32! |
IF (x >= 33!) THEN x = x - 32! |
||
compasspoint$ = point$(INT(x)) |
compasspoint$ = point$(INT(x)) |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
Line 5,926: | Line 5,926: | ||
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: |
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 |
# Build a table of directions |
||
pts <- data.frame( |
pts <- data.frame( |
||
Line 5,947: | Line 5,947: | ||
res[1,]$des[1] |
res[1,]$des[1] |
||
}) |
}) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Here are the test inputs: |
Here are the test inputs: |
||
< |
<syntaxhighlight lang=rsplus> |
||
test <- data.frame( |
test <- data.frame( |
||
deg = c( 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, |
deg = c( 0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, |
||
Line 5,962: | Line 5,962: | ||
test$heading <- heading(test$deg) |
test$heading <- heading(test$deg) |
||
test |
test |
||
</syntaxhighlight> |
|||
</lang> |
|||
Check that the output headings cover the full range of headings: |
Check that the output headings cover the full range of headings: |
||
<syntaxhighlight lang=R> |
|||
<lang R> |
|||
all.equal(test$heading, pts$des) |
all.equal(test$heading, pts$des) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 6,013: | Line 6,013: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=racket>#lang racket |
||
;;; Generate the headings and boxes |
;;; Generate the headings and boxes |
||
Line 6,067: | Line 6,067: | ||
(check-= (heading->box 5.62) 1 0) |
(check-= (heading->box 5.62) 1 0) |
||
(check-= (heading->box 5.63) 2 0) |
(check-= (heading->box 5.63) 2 0) |
||
(check-= (heading->box 16.87) 2 0))</ |
(check-= (heading->box 16.87) 2 0))</syntaxhighlight> |
||
Output: |
Output: |
||
Line 6,109: | Line 6,109: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2015.12}} |
{{works with|Rakudo|2015.12}} |
||
<lang |
<syntaxhighlight lang=raku line>sub point (Int $index) { |
||
my $ix = $index % 32; |
my $ix = $index % 32; |
||
if $ix +& 1 |
if $ix +& 1 |
||
Line 6,130: | Line 6,130: | ||
𝜽, |
𝜽, |
||
tc point angle-to-point 𝜽; |
tc point angle-to-point 𝜽; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 6,167: | Line 6,167: | ||
1 354.38° North</pre> |
1 354.38° North</pre> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang=Rebol>Red [] |
||
d: charset [#"N" #"E" #"S" #"W"] ;; main directions |
d: charset [#"N" #"E" #"S" #"W"] ;; main directions |
||
Line 6,189: | Line 6,189: | ||
hm/:heading: reduce [ (i % 32 + 1 ) expand to-string compass-points/1 [N north b " by " S south E east W west] ] |
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 |
print-line heading |
||
]</ |
]</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 6,229: | Line 6,229: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This version does normalization of the (degree) heading and can also handle negative headings. |
This version does normalization of the (degree) heading and can also handle negative headings. |
||
< |
<syntaxhighlight lang=rexx>/*REXX program "boxes the compass" [from degree (º) headings ───► a 32 point set]. */ |
||
parse arg $ /*allow º headings to be specified.*/ |
parse arg $ /*allow º headings to be specified.*/ |
||
if $='' then $= 0 16.87 16.88 33.75 50.62 50.63 67.5 84.37 84.38 101.25 118.12 118.13 , |
if $='' then $= 0 16.87 16.88 33.75 50.62 50.63 67.5 84.37 84.38 101.25 118.12 118.13 , |
||
Line 6,257: | Line 6,257: | ||
z= changestr( left(d, 1), z, d) |
z= changestr( left(d, 1), z, d) |
||
end /*k*/ /* [↑] old, haystack, new*/ |
end /*k*/ /* [↑] old, haystack, new*/ |
||
return changestr('b', z, " by ") /*expand "b" ───► " by ".*/</ |
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]]. |
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
||
<br><br> |
<br><br> |
||
Line 6,300: | Line 6,300: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang=ring> |
||
# Project : Box the compass |
# Project : Box the compass |
||
Line 6,330: | Line 6,330: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 6,370: | Line 6,370: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang=ruby>Headings = %w(north east south west north).each_cons(2).flat_map do |a, b| |
||
[a, |
[a, |
||
"#{a} by #{b}", |
"#{a} by #{b}", |
||
Line 6,393: | Line 6,393: | ||
index, name = heading degrees |
index, name = heading degrees |
||
printf "%2d %20s %6.2f\n", index, name.center(20), degrees |
printf "%2d %20s %6.2f\n", index, name.center(20), degrees |
||
end</ |
end</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 6,431: | Line 6,431: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang=runbasic>global direct$ |
||
dim direct$(22) |
dim direct$(22) |
||
direct$(1) = "y" 'by |
direct$(1) = "y" 'by |
||
Line 6,467: | Line 6,467: | ||
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 "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 "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"</ |
data "W-sw","W b s","W","W b n","W-nw","Nw b w","Nw","Nw b n","N-nw","N b w"</syntaxhighlight> |
||
<code><table border=1> |
<code><table border=1> |
||
<TR><TD align=right>1</td><td>North</td><td>0.0</td></tr> |
<TR><TD align=right>1</td><td>North</td><td>0.0</td></tr> |
||
Line 6,507: | Line 6,507: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=Rust>fn expand(cp: &str) -> String { |
||
let mut out = String::new(); |
let mut out = String::new(); |
||
for c in cp.chars() { |
for c in cp.chars() { |
||
Line 6,546: | Line 6,546: | ||
); |
); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Index Degrees Compass point |
<pre>Index Degrees Compass point |
||
Line 6,586: | Line 6,586: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Inspired by Java version |
Inspired by Java version |
||
< |
<syntaxhighlight lang=Scala>object BoxingTheCompass extends App { |
||
val cardinal = List("north", "east", "south", "west") |
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") |
val pointDesc = List("1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1") |
||
Line 6,607: | Line 6,607: | ||
(0 to 32).map(i=>Triple(pointDeg(i),deg2ind(pointDeg(i)),pointName(deg2ind(pointDeg(i))))) |
(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))} |
.map{t=>(printf("%s\t%18s\t%s°\n",t._2,t._3,t._1))} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 6,647: | Line 6,647: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
Line 6,670: | Line 6,670: | ||
degrees[index] digits 2 lpad 6); |
degrees[index] digits 2 lpad 6); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 6,711: | Line 6,711: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=ruby>func point (index) { |
||
var ix = (index % 32); |
var ix = (index % 32); |
||
if (ix & 1) { "#{point((ix + 1) & 28)} by #{point(((2 - (ix & 2)) * 4) + ix & 24)}" } |
if (ix & 1) { "#{point((ix + 1) & 28)} by #{point(((2 - (ix & 2)) * 4) + ix & 24)}" } |
||
Line 6,725: | Line 6,725: | ||
var 𝜽 = test_angle(ix); |
var 𝜽 = test_angle(ix); |
||
printf(" %2d %6.2f° %s\n", ix % 32 + 1, 𝜽, point(angle_to_point(𝜽)).tc); |
printf(" %2d %6.2f° %s\n", ix % 32 + 1, 𝜽, point(angle_to_point(𝜽)).tc); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,764: | Line 6,764: | ||
=={{header|smart BASIC}}== |
=={{header|smart BASIC}}== |
||
< |
<syntaxhighlight lang=smart BASIC>/*Boxing The Compass by rbytes December 2017*/ |
||
GET SCREEN SIZE sw,sh |
GET SCREEN SIZE sw,sh |
||
OPTION BASE 1 |
OPTION BASE 1 |
||
Line 6,800: | Line 6,800: | ||
DATA "Northwest", "NW", "309.38", "315.00", "320.62", "Northwest by north", "NWbN", "320.63", "326.25", "331.87" |
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" |
DATA "North-northwest", "NNW", "331.88", "337.50", "343.12", "North by west", "NbW", "343.13", "348.75", "354.37" |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 6,841: | Line 6,841: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang=tcl>proc angle2compass {angle} { |
||
set dirs { |
set dirs { |
||
N NbE N-NE NEbN NE NEbE E-NE EbN E EbS E-SE SEbE SE SEbS S-SE SbE |
N NbE N-NE NEbN NE NEbE E-NE EbN E EbS E-SE SEbE SE SEbS S-SE SbE |
||
Line 6,867: | Line 6,867: | ||
# Pretty-print the results of converting an angle to a compass heading |
# Pretty-print the results of converting an angle to a compass heading |
||
puts [format "%2i %-18s %7.2f°" $index [angle2compass $heading] $heading] |
puts [format "%2i %-18s %7.2f°" $index [angle2compass $heading] $heading] |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 6,907: | Line 6,907: | ||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
{{trans|QBasic}} |
{{trans|QBasic}} |
||
< |
<syntaxhighlight lang=qbasic>DIM point$(32) |
||
FUNCTION compasspoint$ (h) |
FUNCTION compasspoint$ (h) |
||
Line 6,940: | Line 6,940: | ||
PRINT ind, compasspoint$(heading), heading |
PRINT ind, compasspoint$(heading), heading |
||
NEXT i |
NEXT i |
||
END</ |
END</syntaxhighlight> |
||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
{{trans|C}} |
{{trans|C}} |
||
Since uBasic is an integer interpreter, we have to do some scaling to perform this task. |
Since uBasic is an integer interpreter, we have to do some scaling to perform this task. |
||
<lang>Push 0, 1687, 1688, 3375, 5062, 5063, 6750, 8437, 8438, 10125, 11812, 11813 |
<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 13500, 15187, 15188, 16875, 18562, 18563, 20250, 21937, 21938, 23625 |
||
Push 25312, 25313, 27000, 28687, 28688, 30375, 32062, 32063, 33750, 35437 |
Push 25312, 25313, 27000, 28687, 28688, 30375, 32062, 32063, 33750, 35437 |
||
Line 6,998: | Line 6,998: | ||
400 Print "North-northwest "; : Return |
400 Print "North-northwest "; : Return |
||
410 Print "North by west "; : Return |
410 Print "North by west "; : Return |
||
420 Print "North "; : Return</ |
420 Print "North "; : Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,044: | Line 7,044: | ||
Requires the standard POSIX bc(1) command to perform floating-point arithmetic. |
Requires the standard POSIX bc(1) command to perform floating-point arithmetic. |
||
< |
<syntaxhighlight lang=sh># List of abbreviated compass point labels |
||
compass_points=( N NbE N-NE NEbN NE NEbE E-NE EbN |
compass_points=( N NbE N-NE NEbN NE NEbE E-NE EbN |
||
E EbS E-SE SEbE SE SEbS S-SE SbE |
E EbS E-SE SEbE SE SEbS S-SE SbE |
||
Line 7,093: | Line 7,093: | ||
label="$(expand_point $abbr)" |
label="$(expand_point $abbr)" |
||
printf "$row_format" $angle "$label" $index |
printf "$row_format" $angle "$label" $index |
||
done</ |
done</syntaxhighlight> |
||
Output: <pre>Degrees | Closest Point | Index |
Output: <pre>Degrees | Closest Point | Index |
||
Line 7,132: | Line 7,132: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang=vb>Public Sub box_the_compass() |
||
Dim compass_point As Integer |
Dim compass_point As Integer |
||
Dim compass_points_all As New Collection |
Dim compass_points_all As New Collection |
||
Line 7,165: | Line 7,165: | ||
Debug.Print test_points_all(i) |
Debug.Print test_points_all(i) |
||
Next i |
Next i |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre> 1 North 0 |
<pre> 1 North 0 |
||
2 North by east 16,87 |
2 North by east 16,87 |
||
Line 7,202: | Line 7,202: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang=vbnet>Module BoxingTheCompass |
||
Dim _points(32) As String |
Dim _points(32) As String |
||
Line 7,248: | Line 7,248: | ||
End Function |
End Function |
||
End Module |
End Module |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 7,289: | Line 7,289: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
||
// 'cpx' returns integer index from 0 to 31 corresponding to compass point. |
// 'cpx' returns integer index from 0 to 31 corresponding to compass point. |
||
Line 7,354: | Line 7,354: | ||
System.print("%(Fmt.d(4, index)) %(Fmt.s(-19, d)) %(Fmt.f(7, h, 2))°") |
System.print("%(Fmt.d(4, index)) %(Fmt.s(-19, d)) %(Fmt.f(7, h, 2))°") |
||
i = i + 1 |
i = i + 1 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,396: | Line 7,396: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
< |
<syntaxhighlight lang=Yabasic> |
||
dim point$(32) |
dim point$(32) |
||
Line 7,433: | Line 7,433: | ||
70 data "West by south ", "West ", "West by north ", "West-northwest " |
70 data "West by south ", "West ", "West by north ", "West-northwest " |
||
80 data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest " |
80 data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest " |
||
90 data "North by west "</ |
90 data "North by west "</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|AWK}} |
{{trans|AWK}} |
||
< |
<syntaxhighlight lang=zkl>A:=("X N NbE NNE NEbN NE NEbE ENE EbN E EbS ESE " // one based array |
||
"SEbE SE SEbS SSE SbE S SbW SSW SWbS SW SWbW " |
"SEbE SE SEbS SSE SbE S SbW SSW SWbS SW SWbW " |
||
"WSW WbS W WbN WNW NWbW NW NWbN NNW NbW").split(" "); |
"WSW WbS W WbN WNW NWbW NW NWbN NNW NbW").split(" "); |
||
Line 7,453: | Line 7,453: | ||
box:=compassBox(heading); |
box:=compassBox(heading); |
||
println("%6.2f\UB0; : %2d\t%s".fmt(heading,box,A[box])); |
println("%6.2f\UB0; : %2d\t%s".fmt(heading,box,A[box])); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,469: | Line 7,469: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
{{trans|Liberty BASIC}} |
{{trans|Liberty BASIC}} |
||
< |
<syntaxhighlight lang=zxbasic>10 DATA "North","North by east","North-northeast" |
||
20 DATA "Northeast by north","Northeast","Northeast by east","East-northeast" |
20 DATA "Northeast by north","Northeast","Northeast by east","East-northeast" |
||
30 DATA "East by north","East","East by south","East-southeast" |
30 DATA "East by north","East","East by south","East-southeast" |
||
Line 7,495: | Line 7,495: | ||
250 STOP |
250 STOP |
||
260 DEF FN m(i,n)=((i/n)-INT (i/n))*n : REM modulus function |
260 DEF FN m(i,n)=((i/n)-INT (i/n))*n : REM modulus function |
||
</syntaxhighlight> |
|||
</lang> |