This commit is contained in:
Ingy döt Net 2013-04-10 16:19:29 -07:00
parent e5e8880e41
commit 518da4a923
1019 changed files with 15877 additions and 0 deletions

View file

@ -0,0 +1,21 @@
Avast me hearties!
There be many a [http://www.talklikeapirate.com/howto.html land lubber] that knows [http://oxforddictionaries.com/view/entry/m_en_gb0550020#m_en_gb0550020 naught] of the pirate ways and gives direction by degree!
They know not how to [[wp:Boxing the compass|box the compass]]!
'''Task description'''
# Create a function that takes a heading in degrees and returns the correct 32-point compass heading.
# Use the function to print and display a table of Index, Compass point, and Degree; rather like the corresponding columns from, the first table of the [[wp:Boxing the compass|wikipedia article]], but use only the following 33 headings as input:
:<code>[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]</code>. (They should give the same order of points but are spread throughout the ranges of acceptance).
;Notes;
* The headings and indices can be calculated from this pseudocode:
<pre>for i in 0..32 inclusive:
heading = i * 11.25
case i %3:
if 1: heading += 5.62; break
if 2: heading -= 5.62; break
end
index = ( i mod 32) + 1</pre>
* 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]])..

View file

@ -0,0 +1,96 @@
#!/usr/local/bin/a68g --script #
[]STRING
long by nesw = (" by ", "North", "East", "South", "West"),
short by nesw = ("b", "N", "E", "S", "W");
MODE MINUTES = REAL; # minutes type #
INT last minute=360*60;
INT point width=last minute OVER 32;
PROC direction name = (REAL direction in minutes, []STRING locale directions)STRING: (
STRING by = locale directions[1];
[]STRING nesw = locale directions[@-1];
PRIO MASK = 7; # same PRIOrity as * and / #
OP MASK = (INT n, BITS lower)INT: ABS (BIN n AND NOT lower),
DECAP = (STRING s)STRING: IF UPB s > 1 THEN REPR (ABS s[1]-ABS "A"+ABS "a")+s[2:] ELSE s FI;
PROC point name = (INT in point)STRING: (
INT point = in point MOD 32 # 32 points of a compass #;
IF point MOD 8 = 0 THEN
# name the principle point: eg. N, E, S or W #
nesw[point OVER 8]
ELIF point MOD 4 = 0 THEN
# name the half: eg. NE, SE, SW or NW #
point name((point+8)MASK 2r1111)+DECAP point name(point MASK 2r1111 + 8)
ELIF point MOD 2 = 0 THEN
# name the quarter: eg. N-NE, E-NE, E-SE, S-SE, S-SW, W-SW, W-NW or N-NW #
point name((point+4)MASK 2r111)+"-"+point name(point MASK 2r111 + 4)
ELSE # Name the sixteenth point: #
# eg. NbE,NEbN,NEbE,EbN,EbS,SEbE,SEbS,SbE,SbW,SWbS,SWbW,WbS,WbN,NWbW,NWbN,NbW #
INT opp point = point OVER 8 + ABS NOT ODD (point OVER 2);
point name((point+2)MASK 2r11)+ by +nesw(opp point MOD 4)
FI
);
point name(ROUND(direction in minutes/point width))
);
PROC traditional name = (MINUTES minutes)STRING: (
INT degrees = ROUND(minutes / 60);
degrees=0 |"Tramontana" |:
degrees=45 |"Greco or Bora" |:
degrees=90 |"Levante" |:
degrees=135|"Sirocco" |:
degrees=180|"Ostro" |:
degrees=225|"Libeccio" |:
degrees=270|"Poniente or Zephyrus"|:
degrees=315|"Mistral" |:
degrees=360|"Tramontana" |""
);
# First generate the test set results #
test:(
printf($"Test:"l$);
FOR i FROM 0 TO 32 DO
REAL heading = i * 11.25 +
CASE i MOD 3 IN
+5.62,
-5.62
OUT 0
ESAC;
INT index = ( i MOD 32) + 1;
printf(($zd" ", g23k, zzd.zzl$, index , direction name(heading*60, long by nesw), heading))
OD
);
table:(
OP OVER = (REAL r, INT base)INT: ENTIER ABS r OVER base,
MOD = (REAL r, INT base)REAL: ( INT i = ENTIER r; i MOD base + r - i);
printf(
$l"Table:"l
" #|Compass point"22k"|Abbr|Traditional wind point| Lowest° | Middle° | Highest°"l$
);
OP DEGMIN = (REAL minutes)STRUCT(INT d, REAL m): (minutes MOD last minute OVER 60, minutes MOD 60);
FOR point FROM 1 TO 32 DO
REAL centre = (point-1) * point width;
REAL from = centre - point width/2,
to = centre + point width/2-1/120;
printf((
$g(-2)"|"$, point,
$g$, direction name(centre, long by nesw),
$22k"|"g$, direction name(centre, short by nesw),
$27k"|"g$, traditional name(centre),
$50k$, $"|"g(-3)"°", dd.dd""$, DEGMIN from, DEGMIN centre, DEGMIN to,
$l$
))
OD
)

View file

@ -0,0 +1,18 @@
#!/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," ");
}
function ceil(x) {
y = int(x)
return y < x ? y + 1 : y
}
function compassbox(d) {
return ceil( ( (d + 360 / 64) % 360) * 32 / 360);
}
{
box = compassbox($1);
printf "%6.2f : %2d\t%s\n",$1,box,A[box];
}

View file

@ -0,0 +1,66 @@
with Ada.Text_IO;
procedure Box_The_Compass is
type Degrees is digits 5 range 0.00 .. 359.99;
type Index_Type is mod 32;
function Long_Name(Short: String) return String is
function Char_To_Name(Char: Character) return String is
begin
case Char is
when 'N' | 'n' => return Char & "orth";
when 'S' | 's' => return Char & "outh";
when 'E' | 'e' => return Char & "ast";
when 'W' | 'w' => return Char & "est";
when 'b' => return " by ";
when '-' => return "-";
when others => raise Constraint_Error;
end case;
end Char_To_Name;
begin
if Short'Length = 0 or else Short(Short'First)=' ' then
return "";
else
return Char_To_Name(Short(Short'First))
& Long_Name(Short(Short'First+1 .. Short'Last));
end if;
end Long_Name;
procedure Put_Line(Angle: Degrees) is
function Index(D: Degrees) return Index_Type is
begin
return Index_Type(Integer(Degrees'Rounding(D/11.25)) mod 32);
end Index;
I: Integer := Integer(Index(Angle))+1;
package DIO is new Ada.Text_IO.Float_IO(Degrees);
Abbr: constant array(Index_Type) of String(1 .. 4)
:= ("N ", "Nbe ", "N-ne", "Nebn", "Ne ", "Nebe", "E-ne", "Ebn ",
"E ", "Ebs ", "E-se", "Sebe", "Se ", "Sebs", "S-se", "Sbe ",
"S ", "Sbw ", "S-sw", "Swbs", "Sw ", "Swbw", "W-sw", "Wbs ",
"W ", "Wbn ", "W-nw", "Nwbw", "Nw ", "Nwbn", "N-nw", "Nbw ");
begin
DIO.Put(Angle, Fore => 3, Aft => 2, Exp => 0); -- format "zzx.xx"
Ada.Text_IO.Put(" |");
if I <= 9 then
Ada.Text_IO.Put(" ");
end if;
Ada.Text_IO.Put_Line(" " & Integer'Image(I) & " | "
& Long_Name(Abbr(Index(Angle))));
end Put_Line;
Difference: constant array(0..2) of Degrees'Base
:= (0=> 0.0, 1=> +5.62, 2=> - 5.62);
begin
Ada.Text_IO.Put_Line(" angle | box | compass point");
Ada.Text_IO.Put_Line(" ---------------------------------");
for I in 0 .. 32 loop
Put_Line(Degrees(Degrees'Base(I) * 11.25 + Difference(I mod 3)));
end loop;
end Box_The_Compass;

View file

@ -0,0 +1,39 @@
get_Index(angle){
return Mod(floor(angle / 11.25 +0.5), 32) + 1
}
get_Abbr_From_Index(i){
static points
:= [ "N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN"
,"E", "EbS", "ESE", "SEbE", "SE", "SEbS", "SSE", "SbE"
,"S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS"
,"W", "WbN", "WNW", "NWbW", "NW", "NWbN", "NNW", "NbW" ]
return points[i]
}
Build_Name_From_Abbr(a){
Loop Parse, a
{
i := A_Index
if ((i = 2) && (SubStr(a, i, 1) != "b") && (StrLen(a) == 3))
retval .= "-"
retval .= {N: "north", S: "south", E: "east"
, W: "west" , b: " by "}[A_LoopField]
}
return Chr(Asc(SubStr(retval, 1, 1))-32) . SubStr(retval, 2)
}
; test
headings:= [0.00, 16.87, 16.88, 33.75, 50.62, 50.63, 67.50, 84.37, 84.38, 101.25
, 118.12, 118.13, 135.00, 151.87, 151.88, 168.75, 185.62, 185.63
, 202.50, 219.37, 219.38, 236.25, 253.12, 253.13, 270.00, 286.87
, 286.88, 303.75, 320.62, 320.63, 337.50, 354.37, 354.38]
For n, a in headings
{
i := get_Index(a)
out .= SubStr(" " i, -1) " "
. SubStr(Build_Name_From_Abbr(get_Abbr_From_Index(i))
. " ", 1, 24) . SubStr(" " a, -5) . "`r`n" ;
}
clipboard := out

View file

@ -0,0 +1,18 @@
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, _
303.75, 320.62, 320.63, 337.5, 354.37, 354.38]
For $i = 0 To UBound($avArray) - 1
Boxing_the_compass($avArray[$i])
Next
Func Boxing_the_compass($Degree)
Local $namearray[33] = ["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"]
ConsoleWrite(StringFormat("%-2s", Mod(Floor($Degree / 11.25 + 0.5), 32)) & " : " & _
StringFormat("%-20s", $namearray[Mod(Floor($Degree / 11.25 + 0.5), 32)]) & " : " & $Degree & @CRLF)
EndFunc ;==>Boxing_the_compass

View file

@ -0,0 +1,27 @@
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, \
\ 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
FOR i% = 0 TO 32
box% = FNcompassbox(bearing(i%), compass$)
PRINT ; bearing(i%), ; box%, compass$
NEXT
END
DEF FNcompassbox(bearing, RETURN box$)
LOCAL pt%
pt% = INT(bearing / 360 * 32 + 0.5) MOD 32
box$ = FNpt(pt%)
LEFT$(box$,1) = CHR$(ASC(LEFT$(box$,1))-32)
= pt% + 1
DEF FNpt(pt%)
LOCAL pt$() : DIM pt$(3)
IF pt% AND 1 THEN = FNpt((pt% + 1) AND 28) + " by " + \
\ FNpt(((2 - (pt% AND 2)) * 4) + pt% AND 24)
IF pt% AND 2 THEN = FNpt((pt% + 2) AND 24) + "-" + FNpt((pt% OR 4) AND 28)
IF pt% AND 4 THEN = FNpt((pt% + 8) AND 16) + FNpt((pt% OR 8) AND 24)
pt$() = "north", "east", "south", "west"
= pt$(pt% DIV 8)

View file

@ -0,0 +1,53 @@
#include <stdio.h>
int main()
{
int i, j;
double degrees[] = { 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 };
const char * 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 ";
for (i = 0; i < 33; i++) {
j = .5 + degrees[i] * 32 / 360;
printf("%2d %.22s %6.2f\n", (j % 32) + 1, names + (j % 32) * 22,
degrees[i]);
}
return 0;
}

View file

@ -0,0 +1,26 @@
(ns boxing-the-compass
(:use [clojure.string :only [capitalize]]))
(def headings
(for [i (range 0 (inc 32))]
(let [heading (* i 11.25)]
(case (mod i 3)
1 (+ heading 5.62)
2 (- heading 5.62)
heading))))
(defn angle2compass
[angle]
(let [dirs ["N" "NbE" "N-NE" "NEbN" "NE" "NEbE" "E-NE" "EbN"
"E" "EbS" "E-SE" "SEbE" "SE" "SEbS" "S-SE" "SbE"
"S" "SbW" "S-SW" "SWbS" "SW" "SWbW" "W-SW" "WbS"
"W" "WbN" "W-NW" "NWbW" "NW" "NWbN" "N-NW" "NbW"]
unpack {\N "north" \E "east" \W "west" \S "south" \b " by " \- "-"}
sep (/ 360 (count dirs))
dir (int (/ (mod (+ angle (/ sep 2)) 360) sep))]
(capitalize (apply str (map unpack (dirs dir))))))
(print
(apply str (map-indexed #(format "%2s %-18s %7.2f\n"
(inc (mod %1 32)) (angle2compass %2) %2)
headings)))

View file

@ -0,0 +1,38 @@
Program Compass
implicit none
integer :: i, ind
real :: heading
do i = 0, 32
heading = i * 11.25
if (mod(i, 3) == 1) then
heading = heading + 5.62
else if (mod(i, 3) == 2) then
heading = heading - 5.62
end if
ind = mod(i, 32) + 1
write(*, "(i2, a20, f8.2)") ind, compasspoint(heading), heading
end do
contains
function compasspoint(h)
character(18) :: compasspoint
character(18) :: points(32) = (/ "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 " /)
real, intent(in) :: h
real :: x
x = h / 11.25 + 1.5
if (x >= 33.0) x = x - 32.0
compasspoint = points(int(x))
end function compasspoint
end program Compass

View file

@ -0,0 +1,67 @@
package main
import "fmt"
// function required by task
func degrees2compasspoint(h float32) string {
return compassPoint[cpx(h)]
}
// cpx returns integer index from 0 to 31 corresponding to compass point.
// input heading h is in degrees. Note this index is a zero-based index
// suitable for indexing into the table of printable compass points,
// and is not the same as the index specified to be printed in the output.
func cpx(h float32) int {
x := int(h/11.25+.5) % 32
if x < 0 {
x += 32
}
return x
}
// printable compass points
var compassPoint = []string{
"North",
"North by east",
"North-northeast",
"Northeast by north",
"Northeast",
"Northeast by east",
"East-northeast",
"East by north",
"East",
"East by south",
"East-southeast",
"Southeast by east",
"Southeast",
"Southeast by south",
"South-southeast",
"South by east",
"South",
"South by west",
"South-southwest",
"Southwest by south",
"Southwest",
"Southwest by west",
"West-southwest",
"West by south",
"West",
"West by north",
"West-northwest",
"Northwest by west",
"Northwest",
"Northwest by north",
"North-northwest",
"North by west",
}
func main() {
fmt.Println("Index Compass point Degree")
for i, h := range []float32{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} {
index := i%32 + 1 // printable index computed per pseudocode
fmt.Printf("%4d %-19s %7.2f°\n", index, degrees2compasspoint(h), h)
}
}

View file

@ -0,0 +1,25 @@
import Data.Char
import Data.Maybe
import Text.Printf
dirs = ["N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN",
"E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE",
"S", "SbW", "S-SW", "SWbS", "SW", "SWbW", "W-SW", "WbS",
"W", "WbN", "W-NW", "NWbW", "NW", "NWbN", "N-NW", "NbW"]
-- Given an index between 0 and 31 return the corresponding compass point name.
pointName = capitalize . concatMap (fromMaybe "?" . fromChar) . (dirs !!)
where fromChar c = lookup c [('N', "north"), ('S', "south"), ('E', "east"),
('W', "west"), ('b', " by "), ('-', "-")]
capitalize (c:cs) = toUpper c : cs
-- Convert degrees to a compass point index between 0 and 31.
pointIndex d = (round (d*1000) + 5625) `mod` 360000 `div` 11250
printPointName d = let deg = read d :: Double
idx = pointIndex deg
in printf "%2d %-18s %6.2f°\n" (idx+1) (pointName idx) deg
main = do
input <- getContents
mapM_ printPointName $ lines input

View file

@ -0,0 +1,49 @@
public class BoxingTheCompass{
private static String[] points = new String[32];
public static void main(String[] args){
buildPoints();
double heading = 0;
for(int i = 0; i<= 32;i++){
heading = i * 11.25;
switch(i % 3){
case 1:
heading += 5.62;
break;
case 2:
heading -= 5.62;
break;
default:
}
System.out.printf("%s\t%18s\t%s°\n",(i % 32) + 1, initialUpper(getPoint(heading)), heading);
}
}
private static void buildPoints(){
String[] cardinal = {"north", "east", "south", "west"};
String[] pointDesc = {"1", "1 by 2", "1-C", "C by 1", "C", "C by 2", "2-C", "2 by 1"};
String str1, str2, strC;
for(int i = 0;i <= 3;i++){
str1 = cardinal[i];
str2 = cardinal[(i + 1) % 4];
strC = (str1.equals("north") || str1.equals("south")) ? (str1 + str2): (str2 + str1);
for(int j = 0;j <= 7;j++){
points[i * 8 + j] = pointDesc[j].replace("1", str1).replace("2", str2).replace("C", strC);
}
}
}
private static String initialUpper(String s){
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
private static String getPoint(double degrees){
double testD = (degrees / 11.25) + 0.5;
return points[(int)Math.floor(testD % 32)];
}
}

View file

@ -0,0 +1,45 @@
function createRow(i, point, heading) {
var tr = document.createElement('tr'),
td;
td = document.createElement('td');
td.appendChild(document.createTextNode(i));
tr.appendChild(td);
td = document.createElement('td');
point = point.substr(0, 1).toUpperCase() + point.substr(1);
td.appendChild(document.createTextNode(point));
tr.appendChild(td);
td = document.createElement('td');
td.appendChild(document.createTextNode(heading));
tr.appendChild(td);
return tr;
}
function getPoint(i) {
var j = i % 8,
i = Math.floor(i / 8) % 4,
cardinal = ['north', 'east', 'south', 'west'],
pointDesc = ['1', '1 by 2', '1-C', 'C by 1', 'C', 'C by 2', '2-C', '2 by 1'],
str1, str2, strC;
str1 = cardinal[i];
str2 = cardinal[(i + 1) % 4];
strC = (str1 === 'north' || str1 === 'south') ? str1 + str2 : str2 + str1;
return pointDesc[j].replace('1', str1).replace('2', str2).replace('C', strC);
}
var i,
heading,
table = document.createElement('table'),
tbody = document.createElement('tbody'),
tr;
for (i = 0; i <= 32; i += 1) {
heading = i * 11.25 + [0, 5.62, -5.62][i % 3];
tr = createRow(i % 32 + 1, getPoint(i), heading + '°');
tbody.appendChild(tr);
}
table.appendChild(tbody);
document.body.appendChild(table);

View file

@ -0,0 +1,49 @@
-- 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",
"S", "SbW", "S-SW", "SWbS", "SW", "SWbW", "W-SW", "WbS",
"W", "WbN", "W-NW", "NWbW", "NW", "NWbN", "N-NW", "NbW" }
-- List of angles to test
test_angles = { 0.00, 16.87, 16.88, 33.75, 50.62, 50.63, 67.50,
84.37, 84.38, 101.25, 118.12, 118.13, 135.00, 151.87,
151.88, 168.75, 185.62, 185.63, 202.50, 219.37, 219.38,
236.25, 253.12, 253.13, 270.00, 286.87, 286.88, 303.75,
320.62, 320.63, 337.50, 354.37, 354.38 }
-- capitalize a string
function capitalize(s)
return s:sub(1,1):upper() .. s:sub(2)
end
-- convert compass point abbreviation to full text of label
function expand_point(abbr)
for from, to in pairs( { N="north", E="east", S="south", W="west",
b=" by " }) do
abbr = abbr:gsub(from, to)
end
return capitalize(abbr)
end
-- modulus function that returns 1..N instead of 0..N-1
function adjusted_modulo(n, d)
return 1 + (n - 1) % d
end
-- convert a compass angle from degrees into a box index (1..32)
function compass_point(degrees)
degrees = degrees % 360
return adjusted_modulo(1 + math.floor( (degrees+5.625) / 11.25), 32)
end
-- Now output the table of test data
header_format = "%-7s | %-18s | %s"
row_format = "%7.2f | %-18s | %2d"
print(header_format:format("Degrees", "Closest Point", "Index"))
for i, angle in ipairs(test_angles) do
index = compass_point(angle)
abbr = compass_points[index]
label = expand_point(abbr)
print(row_format:format(angle, label, index))
end

View file

@ -0,0 +1,3 @@
function b = compassbox(d)
b = ceil(mod(d+360/64,360)*32/360);
end;

View file

@ -0,0 +1,45 @@
use utf8;
my @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",
);
my @angles = (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);
for (@angles) {
my $i = int(($_ * 32 / 360) + .5) % 32;
printf "%3d %18s %6.2f°\n", $i + 1, $names[$i], $_;
}

View file

@ -0,0 +1,33 @@
1 North 0.00°
2 North by east 16.87°
3 North-northeast 16.88°
4 Northeast by north 33.75°
5 Northeast 50.62°
6 Northeast by east 50.63°
7 East-northeast 67.50°
8 East by north 84.37°
9 East 84.38°
10 East by south 101.25°
11 East-southeast 118.12°
12 Southeast by east 118.13°
13 Southeast 135.00°
14 Southeast by south 151.87°
15 South-southeast 151.88°
16 South by east 168.75°
17 South 185.62°
18 South by west 185.63°
19 South-southwest 202.50°
20 Southwest by south 219.37°
21 Southwest 219.38°
22 Southwest by west 236.25°
23 West-southwest 253.12°
24 West by south 253.13°
25 West 270.00°
26 West by north 286.87°
27 West-northwest 286.88°
28 Northwest by west 303.75°
29 Northwest 320.62°
30 Northwest by north 320.63°
31 North-northwest 337.50°
32 North by west 354.37°
1 North 354.38°

View file

@ -0,0 +1,34 @@
(scl 3)
(setq *Compass # Build lookup table
(let H -16.875
(mapcar
'((Str)
(cons
(inc 'H 11.25) # Heading in degrees
(pack # Compass point
(replace (chop Str)
"N" "north"
"E" "east"
"S" "south"
"W" "west"
"b" " by " ) ) ) )
'("N" "NbE" "N-NE" "NEbN" "NE" "NEbE" "E-NE" "EbN"
"E" "EbS" "E-SE" "SEbE" "SE" "SEbS" "S-SE" "SbE"
"S" "SbW" "S-SW" "SWbS" "SW" "SWbW" "W-SW" "WbS"
"W" "WbN" "W-NW" "NWbW" "NW" "NWbN" "N-NW" "NbW"
"N" ) ) ) )
(de heading (Deg)
(rank (% Deg 360.00) *Compass) )
(for I (range 0 32)
(let H (* I 11.25)
(case (% I 3)
(1 (inc 'H 5.62))
(2 (dec 'H 5.62)) )
(tab (3 1 -18 8)
(inc (% I 32))
NIL
(cdr (heading H))
(round H 2) ) ) )

View file

@ -0,0 +1,37 @@
compassangle(1, 'North',n, 0.00).
compassangle(2, 'North by east', nbe, 11.25).
compassangle(3, 'North-northeast', nne,22.50).
compassangle(4, 'Northeast by north', nebn,33.75).
compassangle(5, 'Northeast', ne,45.00).
compassangle(6, 'Norteast by east', nebe,56.25).
compassangle(7, 'East-northeast', ene,67.50).
compassangle(8, 'East by North', ebn,78.75).
compassangle(9, 'East', e,90.00).
compassangle(10, 'East by south', ebs, 101.25).
compassangle(11, 'East-southeast', ese,112.50).
compassangle(12, 'Southeast by east', sebe, 123.75).
compassangle(13, 'Southeast', se, 135.00).
compassangle(14, 'Southeast by south', sebs, 146.25).
compassangle(15, 'South-southeast',sse, 157.50).
compassangle(16, 'South by east', sbe, 168.75).
compassangle(17, 'South', s, 180.00).
compassangle(18, 'South by west', sbw, 191.25).
compassangle(19, 'South-southwest', ssw, 202.50).
compassangle(20, 'Southwest by south', swbs, 213.75).
compassangle(21, 'Southwest', sw, 225.00).
compassangle(22, 'Southwest by west', swbw, 236.25).
compassangle(23, 'West-southwest', wsw, 247.50).
compassangle(24, 'West by south', wbs, 258.75).
compassangle(25, 'West', w, 270.00).
compassangle(26, 'West by north', wbn, 281.25).
compassangle(27, 'West-northwest', wnw, 292.50).
compassangle(28, 'Northwest by west', nwbw, 303.75).
compassangle(29, 'Northwest', nw, 315.00).
compassangle(30, 'Northwest by north', nwbn, 326.25).
compassangle(31, 'North-northwest', nnw, 337.50).
compassangle(32, 'North by west', nbw, 348.75).
compassangle(1, 'North', n, 360.00).
compassangle(Index , Name, Heading, Angle) :- nonvar(Angle), resolveindex(Angle, Index),
compassangle(Index,Name, Heading, _).
resolveindex(Angle, Index) :- N is Angle / 11.25 + 0.5, I is floor(N),Index is (I mod 32) + 1.

View file

@ -0,0 +1,7 @@
printTableRow(Angle) :- compassangle(Index, Name, _, Angle),
write(Index), write(' '),
write(Name), write(' '),
write(Angle).
printTable([X|Xs]) :- printTableRow(X), nl, printTable(Xs),!.
printTable([]).

View file

@ -0,0 +1,36 @@
?- printTable([0.0, 16.87, 16.88, 33.75, 50.62, 50.63, 67.5, 84.37, 84.38, 101.25, 118.12, 118.13, 135.0, 151.87, 151.88, 168.75, 185.62,
185.63, 202.5, 219.37, 219.38, 236.25, 253.12, 253.13, 270.0, 286.87, 286.88, 303.75, 320.62, 320.63, 337.5, 354.37, 354.38]).
1 North 0.0
2 North by east 16.87
3 North-northeast 16.88
4 Northeast by north 33.75
5 Northeast 50.62
6 Norteast by east 50.63
7 East-northeast 67.5
8 East by North 84.37
9 East 84.38
10 East by south 101.25
11 East-southeast 118.12
12 Southeast by east 118.13
13 Southeast 135.0
14 Southeast by south 151.87
15 South-southeast 151.88
16 South by east 168.75
17 South 185.62
18 South by west 185.63
19 South-southwest 202.5
20 Southwest by south 219.37
21 Southwest 219.38
22 Southwest by west 236.25
23 West-southwest 253.12
24 West by south 253.13
25 West 270.0
26 West by north 286.87
27 West-northwest 286.88
28 Northwest by west 303.75
29 Northwest 320.62
30 Northwest by north 320.63
31 North-northwest 337.5
32 North by west 354.37
1 North 354.38
true.

View file

@ -0,0 +1,25 @@
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(',')
quarter2 = [p.replace('NE','EN') for p in quarter1]
def degrees2compasspoint(d):
d = (d % 360) + 360/64
majorindex, minor = divmod(d, 90.)
majorindex = int(majorindex)
minorindex = int( (minor*4) // 45 )
p1, p2 = majors[majorindex: majorindex+2]
if p1 in {'north', 'south'}:
q = quarter1
else:
q = quarter2
return q[minorindex].replace('N', p1).replace('E', p2).capitalize()
if __name__ == '__main__':
for i in range(33):
d = i * 11.25
m = i % 3
if m == 1: d += 5.62
elif m == 2: d -= 5.62
n = i % 32 + 1
print( '%2i %-18s %7.2f°' % (n, degrees2compasspoint(d), d) )

View file

@ -0,0 +1,32 @@
/*REXX program boxes the compass (from º headings ───> 32 point set). */
headings=0 16.87 16.88 33.75 50.62 50.63 67.5 84.37 84.38 101.25 118.12,
118.13 135 151.87 151.88 168.75 185.62 185.63 202.5 219.37,
219.38 236.25 253.12 253.13 270 286.87 286.88 303.75 320.62,
320.63 337.5 354.37 354.38 /*some sample compass headings. */
points='n nbe n-ne nebn ne nebe e-ne ebn e ebs e-se sebe se sebs s-se sbe',
's sbw s-sw swbs sw swbw w-sw wbs w wbn w-nw nwbw nw nwbn n-nw nbw'
dirs='north south east west'
@ebcdic= 1=='f1'x
if @ebcdic then degSym='a1'x
else degSym='f8'x /*although, 'a7'x looks better, */
/*see line 1 (above).*/
say right(degSym'heading',30) center('compass heading',20)
say right( '',30) copies('' ,20)
do j=1 for words(headings)
x=word(headings,j)
say right(format(x,,2)degSym,30-1) ' ' boxHeading(x)
end /*j*/
exit
/*─────────────────────────────────────BOXHEADING subroutine────────────*/
boxHeading: _=arg(1)//360; if _<0 then _=360-_ /*normalize the heading.*/
_=word(points,trunc(max(1,(_/11.25+1.5)//33)))
do k=1 for words(dirs); d=word(dirs,k)
_=changestr(left(d,1),_,d)
end /*k*/
return changestr('b',_," by ")
/*next statement is ignored. */
return upper(changestr('b',_," by "),1) /*capitalizes the 1st letter.*/

View file

@ -0,0 +1,55 @@
#lang racket
;;; Generate the headings and boxes
(define (i->heading/box i)
(values (let ((heading (* i #e11.25)))
(case (modulo i 3)
((1) (+ heading #e5.62))
((2) (- heading #e5.62))
(else heading)))
(add1 (modulo i 32))))
(define-values (headings-list box-list)
(for/lists (h-lst i-lst) ((i (in-range 0 (add1 32))))
(i->heading/box i)))
(define box-names
(list "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"))
(define (heading->box h)
(let* ((n-boxes (length box-names))
(box-width (/ 360 n-boxes)))
(add1 (modulo (ceiling (- (/ h box-width) 1/2)) n-boxes))))
;;; displays a single row of the table, can also be used for titles
(define (display-row b a p)
(printf "~a | ~a | ~a~%"
(~a b #:width 2 #:align 'right)
(~a a #:width 6 #:align 'right)
(~a p)))
;;; display the table
(display-row "#" "Angle" "Point")
(displayln "---+--------+-------------------")
(for ((heading headings-list))
(let* ((box-number (heading->box heading)))
;; by coincidence, default value of the second argument to
;; real->decimal-string "decimal-digits" is 2,... just what we want!
(display-row box-number
(real->decimal-string heading)
(list-ref box-names (sub1 box-number)))))
(module+ test
(require rackunit)
;;; unit test heading->box (the business end of the implementation)
(check-= (heading->box 354.38) 1 0)
(check-= (heading->box 5.62) 1 0)
(check-= (heading->box 5.63) 2 0)
(check-= (heading->box 16.87) 2 0))

View file

@ -0,0 +1,16 @@
h = []
["north", "east", "south", "west", "north"].each_cons(2) do |a, b|
c = if ["north", "south"].include? a then "#{a}#{b}" else "#{b}#{a}" end
h << a
h << "#{a} by #{b}"
h << "#{a}-#{c}"
h << "#{c} by #{a}"
h << "#{c}"
h << "#{c} by #{b}"
h << "#{b}-#{c}"
h << "#{b} by #{a}"
end
puts "Headings = {"
h.each_with_index { |n, i| puts " #{i+1} => #{n.inspect}," }
puts "}"

View file

@ -0,0 +1,53 @@
Headings = {
1 => "north",
2 => "north by east",
3 => "north-northeast",
4 => "northeast by north",
5 => "northeast",
6 => "northeast by east",
7 => "east-northeast",
8 => "east by north",
9 => "east",
10 => "east by south",
11 => "east-southeast",
12 => "southeast by east",
13 => "southeast",
14 => "southeast by south",
15 => "south-southeast",
16 => "south by east",
17 => "south",
18 => "south by west",
19 => "south-southwest",
20 => "southwest by south",
21 => "southwest",
22 => "southwest by west",
23 => "west-southwest",
24 => "west by south",
25 => "west",
26 => "west by north",
27 => "west-northwest",
28 => "northwest by west",
29 => "northwest",
30 => "northwest by north",
31 => "north-northwest",
32 => "north by west",
}
# Finds the 32-point compass heading nearest _degrees_, and
# returns an array of the index and name.
#
# p heading(60)
# # => [6, "northeast by east"]
#
def heading(degrees)
i = degrees.quo(360).*(32).round.%(32).+(1)
[i, Headings[i]]
end
# an array of angles, in degrees
angles = (0..32).map { |i| i * 11.25 + [0, 5.62, -5.62][i % 3] }
angles.each do |degrees|
index, name = heading degrees
printf "%2d %20s %6.2f\n", index, name.center(20), degrees
end

View file

@ -0,0 +1,22 @@
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")
val pointDeg: Int => Double = i => {
val fswitch: Int => Int = i => i match {case 1 => 1; case 2 => -1; case _ => 0}
i*11.25+fswitch(i%3)*5.62
}
val deg2ind: Double => Int = deg => (deg*32/360+.5).toInt%32+1
val pointName: Int => String = ind => {
val i = ind - 1
val str1 = cardinal(i%32/8)
val str2 = cardinal((i%32/8+1)%4)
val strC = if ((str1 == "north") || (str1 == "south")) str1+str2 else str2+str1
pointDesc(i%32%8).replace("1", str1).replace("2", str2).replace("C", strC).capitalize
}
(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))}
}

View file

@ -0,0 +1,27 @@
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
S SbW S-SW SWbS SW SWbW W-SW WbS W WbN W-NW NWbW NW NWbN N-NW NbW
}
set unpack {N "north" E "east" W "west" S "south" b " by "}
# Compute the width of each compass segment
set sep [expr {360.0 / [llength $dirs]}]
# Work out which segment contains the compass angle
set dir [expr {round((fmod($angle + $sep/2, 360) - $sep/2) / $sep)}]
# Convert to a named direction, capitalized as in the wikipedia article
return [string totitle [string map $unpack [lindex $dirs $dir]]]
}
# Box the compass, using the variable generation algorithm described
for {set i 0} {$i < 33} {incr i} {
set heading [expr {$i * 11.25}]
if {$i % 3 == 1} {set heading [expr {$heading + 5.62}]}
if {$i % 3 == 2} {set heading [expr {$heading - 5.62}]}
set index [expr {$i % 32 + 1}]
# Pretty-print the results of converting an angle to a compass heading
puts [format "%2i %-18s %7.2f°" $index [angle2compass $heading] $heading]
}