Box the compass: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
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}}
<syntaxhighlight lang="11l">V majors = ‘north east south west’.split(‘ ’)
majors *= 2
V quarter1 = ‘N,N by E,N-NE,NE by N,NE,NE by E,E-NE,E by N’.split(‘,’)
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"action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
Line 194 ⟶ 192:
1 N 354.38
</pre>
 
=={{header|Ada}}==
 
Inspired by [[#C++|the C++ program]], but without the need for a specific library.
 
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
 
procedure Box_The_Compass is
Line 303 ⟶ 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''.}}
<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
[]STRING
Line 476 ⟶ 472:
32|North by West |NbW | |343°07.50′|348°45.00′|354°22.49′
</pre>
 
=={{header|AppleScript}}==
 
Line 486 ⟶ 481:
Functional composition, allowing for additional languages, and different numbers of compass points – see the test section)
 
<syntaxhighlight lang=AppleScript"applescript">use framework "Foundation"
use scripting additions
 
Line 913 ⟶ 908:
32 354.37° North by west 北微西
1 354.38° North 北 </pre>
 
=={{header|AutoHotkey}}==
{{trans|C++}}
{{works with|AutoHotkey_L}}
<syntaxhighlight lang=AHK"ahk">get_Index(angle){
return Mod(floor(angle / 11.25 +0.5), 32) + 1
}
Line 990 ⟶ 984:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|AutoIt}}==
 
<syntaxhighlight lang=AutoIt"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 1,047 ⟶ 1,040:
31 : North by west : 354.37
0 : North : 354.38</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">#!/usr/bin/awk -f
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 1,102 ⟶ 1,094:
354.37 : 32 NbW
354.38 : 1 N</pre>
 
=={{header|BASIC256}}==
<syntaxhighlight lang=BASIC256"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"}
Line 1,115 ⟶ 1,106:
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}}
<syntaxhighlight lang="bbcbasic"> DIM bearing(32)
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,180 ⟶ 1,170:
354.37 32 North by west
354.38 1 North</pre>
 
=={{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\<
>_06g:v>55+,^>/5+55+/48*::::,,,,%:1+.9,:06p48*\-0v
Line 1,232 ⟶ 1,221:
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.)
 
<syntaxhighlight lang=C"c">#include <stdio.h>
 
int main()
Line 1,324 ⟶ 1,312:
1 North 354.38
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 1,400 ⟶ 1,387:
1: North : 354.38
</pre>
 
=={{header|C++}}==
Using the Boost libraries
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <string>
#include <boost/array.hpp>
#include <boost/assign/list_of.hpp>
Line 1,499 ⟶ 1,485:
1 North 354.38
</pre>
 
=={{header|Clojure}}==
{{trans|Tcl}}
<syntaxhighlight lang="lisp">(ns boxing-the-compass
(:use [clojure.string :only [capitalize]]))
Line 1,562 ⟶ 1,547:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|COBOL}}==
Works with GnuCOBOL
<syntaxhighlight lang="cobol"> identification division.
program-id. box-compass.
data division.
Line 1,670 ⟶ 1,654:
1 North 354.38
</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.stdio, std.string, std.math, std.array;
 
struct boxTheCompass {
Line 1,743 ⟶ 1,726:
 
===Alternative version===
<syntaxhighlight lang="d">void main() {
import std.stdio;
 
Line 1,801 ⟶ 1,784:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Box_the_compass#Pascal Pascal].
 
=={{header|Elixir}}==
{{trans|Ruby}}
<syntaxhighlight lang="elixir">defmodule Box do
defp head do
Enum.chunk(~w(north east south west north), 2, 1)
Line 1,868 ⟶ 1,849:
1 North 354.38
</pre>
 
=={{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",
"East by south","East-southeast","Southeast by east","Southeast","Southeast by south",
Line 1,928 ⟶ 1,908:
354.37 32 North by west
354.38 1 North</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|Perl}}
<syntaxhighlight lang="fsharp">let box = [|
"North"; "North by east"; "North-northeast"; "Northeast by north";
"Northeast"; "Northeast by east"; "East-northeast"; "East by north";
Line 1,982 ⟶ 1,961:
32 North by west 354.37°
1 North 354.38°</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang=Factor"factor">USING: formatting kernel math sequences ;
 
CONSTANT: box
Line 2,047 ⟶ 2,025:
354.38° 1 North
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<syntaxhighlight lang="fortran">Program Compass
implicit none
 
Line 2,123 ⟶ 2,100:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 04-11-2016
' compile with: fbc -s console
 
Line 2,191 ⟶ 2,167:
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]'''
<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 cHeading As Collection = ["N": "North", "S": "South", "W": "West", "E": "East", "b": "by"]
Line 2,249 ⟶ 2,224:
Index= 1 354.38 degrees = North
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 2,354 ⟶ 2,328:
1 North 354.38°
</pre>
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">def asCompassPoint(angle) {
def cardinalDirections = ["north", "east", "south", "west"]
 
Line 2,412 ⟶ 2,385:
32 North by west 354.37°
1 North 354.38°</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Char (toUpper)
 
import Data.Maybe (fromMaybe)
Line 2,518 ⟶ 2,490:
32 North by west 354.37°
1 North 354.38°</pre>
 
=={{header|Huginn}}==
<syntaxhighlight lang="huginn">import Algorithms as algo;
import Text as text;
 
Line 2,601 ⟶ 2,572:
32 | 354.37° | North by west
1 | 354.38° | North</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
{{incomplete|Icon|354.38?}}
<syntaxhighlight lang=Icon"icon">link strings,numbers
procedure main()
Line 2,667 ⟶ 2,637:
31 north-northwest 337.50
32 north by west 354.37</pre>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang=IS"is-BASICbasic">100 PROGRAM "Compass.bas"
110 STRING DR$(1 TO 33)*18
120 FOR I=1 TO 33
Line 2,717 ⟶ 2,686:
354.37 32 North by west
354.38 1 North</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang="j">require'strings'
subs=: 'N,north,S,south,E,east,W,west,b, by ,'
dirs=: subs (toupper@{., }.)@rplc~L:1 0&(<;._2) 0 :0 -. ' ',LF
Line 2,731 ⟶ 2,699:
Example use:
 
<syntaxhighlight lang="j"> i.10
0 1 2 3 4 5 6 7 8 9
deg2pnt i.10
Line 2,740 ⟶ 2,708:
Required example:
 
<syntaxhighlight lang="j"> (":@>:@indice,.' ',.>@deg2pnt,.' ',.":@,.)(*&11.25 + 5.62 * 0 1 _1 {~ 3&|) i.33
1 North 0
2 North by east 16.87
Line 2,774 ⟶ 2,742:
32 North by west 354.37
1 North 354.38</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="java">public class BoxingTheCompass{
private static String[] points = new String[32];
Line 2,860 ⟶ 2,827:
32 North by west 354.37°
1 North 354.38°</pre>
 
=={{header|JavaScript}}==
===ES5===
An iterative, web-based approach:
 
<syntaxhighlight lang="javascript">function createRow(i, point, heading) {
var tr = document.createElement('tr'),
td;
Line 2,949 ⟶ 2,915:
 
Functional composition, allowing for additional languages (and different numbers of compass points)
<syntaxhighlight lang=JavaScript"javascript">(() => {
'use strict';
 
Line 3,223 ⟶ 3,189:
32 354.37° North by west 北微西
1 354.38° North 北 </pre>
 
=={{header|Julia}}==
{{works with|Julia|1.2}}
{{trans|Python}}
 
<syntaxhighlight lang="julia">using Printf
 
function degree2compasspoint(d::Float64)
Line 3,286 ⟶ 3,251:
32 North By West 354.37°
1 North 354.38°</pre>
 
=={{header|K}}==
The representation of the names was inspired by Tcl (etc.).
 
<syntaxhighlight lang=K"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;")
Line 3,308 ⟶ 3,272:
The table:
<syntaxhighlight lang=K"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
Line 3,342 ⟶ 3,306:
32 North by west 354.37
1 North 354.38</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
fun expand(cp: String): String {
Line 3,420 ⟶ 3,383:
1 354.38 North
</pre>
 
=={{header|langur}}==
{{trans|D}}
{{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",
"East", "East by south", "East-southeast", "Southeast by east",
Line 3,484 ⟶ 3,446:
1 354.38 North
</pre>
 
=={{header|Lasso}}==
<syntaxhighlight lang=Lasso"lasso">define pointsarray() => {
local(points = array)
loop(-from=0,-to=32) => {
Line 3,575 ⟶ 3,536:
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
Line 3,646 ⟶ 3,606:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|LLVM}}==
{{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
; to just load the string into memory, and that would be boring.
Line 3,752 ⟶ 3,711:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">; List of abbreviated compass point labels
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,855 ⟶ 3,813:
354.37 | North by west | 32
354.38 | North | 1</pre>
 
=={{header|Lua}}==
{{trans|Logo}}
<syntaxhighlight lang="lua">-- List of abbreviated compass point labels
compass_points = { "N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN",
"E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE",
Line 3,943 ⟶ 3,900:
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"m2000 Interpreterinterpreter">
Module CheckIt {
Locale 1033 'change decimal point char to dot.
Line 4,015 ⟶ 3,971:
354.38°| 1 | North
</pre >
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Map[List[Part[#,1], dirs[[Part[#,1]]], ToString@Part[#,2]<>"°"]&,
Map[{Floor[Mod[ #+5.625 , 360]/11.25]+1,#}&,input] ]//TableForm</syntaxhighlight>
<pre>1 North 0.°
Line 4,052 ⟶ 4,007:
32 North by west 354.37°
1 North 354.38°</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=MATLAB"matlab">function b = compassbox(d)
b = ceil(mod(d+360/64,360)*32/360);
end; </syntaxhighlight>
Line 4,094 ⟶ 4,048:
354.37 : 32
354.38 : 1</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE BoxTheCompass;
FROM FormatString IMPORT FormatString;
FROM RealStr IMPORT RealToStr;
Line 4,178 ⟶ 4,131:
ReadChar
END BoxTheCompass.</syntaxhighlight>
 
=={{header|MUMPS}}==
The TCL implementation was the starting point, but this isn't an exact translation.
<syntaxhighlight lang=MUMPS"mumps">BOXING(DEGREE)
;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,250 ⟶ 4,202:
32 North by west 354.37
1 North 354.38</pre></p>
 
=={{header|NetRexx}}==
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary utf8
 
Line 4,526 ⟶ 4,477:
1 N North 354.380
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, sequtils, strformat, strutils
 
const
Line 4,592 ⟶ 4,542:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">
class BoxCompass {
function : Main(args : String[]) ~ Nil {
Line 4,667 ⟶ 4,616:
1 North 354.38
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="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,739 ⟶ 4,687:
1 North 354.38
</pre>
 
=={{header|OoRexx}}==
<syntaxhighlight lang=OOREXX"oorexx">/* Rexx */
 
Do
Line 5,072 ⟶ 5,019:
1 N North 354.380° (354°22′48″)
</pre>
 
=={{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",
"Southeast by east","Southeast","Southeast by south","South-southeast","South by east","South",
Line 5,116 ⟶ 5,062:
32 North by west 354.370000
1 North 354.380000</pre>
 
=={{header|Pascal}}==
{{trans|Fortran}}
<syntaxhighlight lang="pascal">program BoxTheCompass(output);
 
function compasspoint(angle: real): string;
Line 5,193 ⟶ 5,138:
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.
<syntaxhighlight lang=Perl"perl">use utf8;
 
my @names = (
Line 5,240 ⟶ 5,184:
my $i = int(($_ * 32 / 360) + .5) % 32;
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°
3 North-northeast 16.88°
Line 5,273 ⟶ 5,217:
32 North by west 354.37°
1 North 354.38°</syntaxhighlight>
 
=={{header|Phix}}==
Unlike the Perl guy, for me the maths is the boring bit, building those strings is the fun!<br>
<!--<syntaxhighlight lang=Phix"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>
Line 5,362 ⟶ 5,305:
</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"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>
Line 5,380 ⟶ 5,323:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
 
=={{header|Picat}}==
{{trans|Nim}}
<syntaxhighlight lang=Picat"picat">go =>
Names = ["North", "North by east", "North-northeast", "Northeast by north",
"Northeast", "Northeast by east", "East-northeast", "East by north",
Line 5,436 ⟶ 5,378:
32 North by west 354.37
1 North 354.38</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(scl 3)
 
(setq *Compass # Build lookup table
Line 5,506 ⟶ 5,447:
32 north by west 354.37
1 north 354.38</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">function Convert-DegreeToDirection ( [double]$Degree )
{
Line 5,559 ⟶ 5,499:
north</pre>
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,618 ⟶ 5,558:
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).
Line 5,661 ⟶ 5,600:
</syntaxhighlight>
Part 2 : The following rules print a table of indexes.
<syntaxhighlight lang="prolog">
printTableRow(Angle) :- compassangle(Index, Name, _, Angle),
write(Index), write(' '),
Line 5,671 ⟶ 5,610:
</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]).
Line 5,709 ⟶ 5,648:
true.
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">DataSection
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,818 ⟶ 5,756:
32 North by west 354.37
1 354.38</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">majors = 'north east south west'.split()
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,880 ⟶ 5,817:
32 North by west 354.37°
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)
Line 5,920 ⟶ 5,855:
compasspoint$ = point$(INT(x))
END FUNCTION</syntaxhighlight>
 
 
=={{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(
Line 5,951 ⟶ 5,884:
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,
Line 5,966 ⟶ 5,899:
Check that the output headings cover the full range of headings:
 
<syntaxhighlight lang=R"r">
all.equal(test$heading, pts$des)
</syntaxhighlight>
Line 6,011 ⟶ 5,944:
[1] TRUE
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
 
;;; Generate the headings and boxes
Line 6,105 ⟶ 6,037:
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
Line 6,167 ⟶ 6,098:
1 354.38° North</pre>
=={{header|Red}}==
<syntaxhighlight lang=Rebol"rebol">Red []
 
d: charset [#"N" #"E" #"S" #"W"] ;; main directions
Line 6,229 ⟶ 6,160:
=={{header|REXX}}==
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.*/
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,298 ⟶ 6,229:
354.38º north
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Box the compass
 
Line 6,367 ⟶ 6,297:
354.38 0 North
</pre>
 
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">Headings = %w(north east south west north).each_cons(2).flat_map do |a, b|
[a,
"#{a} by #{b}",
Line 6,429 ⟶ 6,358:
32 north by west 354.37
1 north 354.38</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">global direct$
dim direct$(22)
direct$(1) = "y" 'by
Line 6,504 ⟶ 6,432:
</table>
</code>
 
=={{header|Rust}}==
{{trans|Kotlin}}
<syntaxhighlight lang=Rust"rust">fn expand(cp: &str) -> String {
let mut out = String::new();
for c in cp.chars() {
Line 6,583 ⟶ 6,510:
32 354.37 north by west
1 354.38 north</pre>
 
=={{header|Scala}}==
Inspired by Java version
<syntaxhighlight lang=Scala"scala">object BoxingTheCompass extends App {
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,645 ⟶ 6,571:
1 North 354.38°
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 6,708 ⟶ 6,633:
1 North 354.38
</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func point (index) {
var ix = (index % 32);
if (ix & 1) { "#{point((ix + 1) & 28)} by #{point(((2 - (ix & 2)) * 4) + ix & 24)}" }
Line 6,762 ⟶ 6,686:
1 354.38° North
</pre>
 
=={{header|smart BASIC}}==
<syntaxhighlight lang="smart BASICbasic">/*Boxing The Compass by rbytes December 2017*/
GET SCREEN SIZE sw,sh
OPTION BASE 1
Line 6,839 ⟶ 6,762:
32 North by west NbW 343.13 348.75 354.37
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc angle2compass {angle} {
set dirs {
N NbE N-NE NEbN NE NEbE E-NE EbN E EbS E-SE SEbE SE SEbS S-SE SbE
Line 6,904 ⟶ 6,826:
1 North 354.38°
</pre>
 
=={{header|True BASIC}}==
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">DIM point$(32)
 
FUNCTION compasspoint$ (h)
Line 6,941 ⟶ 6,862:
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 7,037 ⟶ 6,957:
0 OK, 0:900
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Line 7,044 ⟶ 6,963:
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
E EbS E-SE SEbE SE SEbS S-SE SbE
Line 7,130 ⟶ 7,049:
354.38 | North | 1
</pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Public Sub box_the_compass()
Dim compass_point As Integer
Dim compass_points_all As New Collection
Line 7,199 ⟶ 7,117:
32 North by west 354,37
1 North 354,38 </pre>
 
=={{header|Visual Basic .NET}}==
 
<syntaxhighlight lang="vbnet">Module BoxingTheCompass
Dim _points(32) As String
 
Line 7,285 ⟶ 7,202:
1: North 354.38°
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
// 'cpx' returns integer index from 0 to 31 corresponding to compass point.
Line 7,393 ⟶ 7,309:
1 North 354.38°
</pre>
 
=={{header|Yabasic}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang=Yabasic"yabasic">
dim point$(32)
Line 7,434 ⟶ 7,349:
80 data "Northwest by west ", "Northwest ", "Northwest by north", "North-northwest "
90 data "North by west "</syntaxhighlight>
 
=={{header|zkl}}==
{{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 "
"WSW WbS W WbN WNW NWbW NW NWbN NNW NbW").split(" ");
Line 7,466 ⟶ 7,380:
354.38 : 1 N
</pre>
 
=={{header|ZX Spectrum 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"
30 DATA "East by north","East","East by south","East-southeast"
10,333

edits