Box the compass: Difference between revisions

(→‎{{header|REXX}}: refurbished and extended the output)
(7 intermediate revisions by 7 users not shown)
Line 1,259:
354.37 32 North by west
354.38 1 North</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 dim names$(31)
110 restore 300
120 for c = 0 to ubound(names$)
130 read a$
140 names$(c) = a$
150 next c
160 dim degrees(32)
170 restore 380
180 for c = 0 to ubound(degrees)
190 read b
200 degrees(c) = b
210 next c
220 for i = 0 to ubound(degrees)
230 j = int((degrees(i)+5.625)/11.25)
240 if j > 31 then j = j-32
250 print using "####.##";degrees(i);
260 print using " ## ";j;
270 print names$(j)
280 next i
290 end
300 data "North","North by east","North-northeast","Northeast by north"
310 data "Northeast","Northeast by east","East-northeast","East by north"
320 data "East","East by south","East-southeast","Southeast by east"
330 data "Southeast","Southeast by south","South-southeast","South by east"
340 data "South","South by west","South-southwest","Southwest by south"
350 data "Southwest","Southwest by west","West-southwest","West by south"
360 data "West","West by north","West-northwest","Northwest by west","Northwest"
370 data "Northwest by north","North-northwest","North by west","North"
380 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
390 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
400 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
410 data 337.5,354.37,354.388</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 dim n$(31)
110 restore 300
120 for c = 0 to 31
130 read a$
140 n$(c) = a$
150 next c
160 dim d(32)
170 restore 380
180 for c = 0 to 32
190 read b
200 d(c) = b
210 next c
220 for i = 0 to 32
230 j = int((d(i)+5.625)/11.25)
240 if j > 31 then j = j-32
250 print using "####.##";d(i);
260 print using " ## ";j;
270 print n$(j)
280 next i
290 end
300 data "North","North by east","North-northeast","Northeast by north"
310 data "Northeast","Northeast by east","East-northeast","East by north"
320 data "East","East by south","East-southeast","Southeast by east"
330 data "Southeast","Southeast by south","South-southeast","South by east"
340 data "South","South by west","South-southwest","Southwest by south"
350 data "Southwest","Southwest by west","West-southwest","West by south"
360 data "West","West by north","West-northwest","Northwest by west","Northwest"
370 data "Northwest by north","North-northwest","North by west","North"
380 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
390 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
400 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
410 data 337.5,354.37,354.38</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Quite BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 array n$
110 for c = 0 to 31
120 read a$
130 let n$(c) = a$
140 next c
150 array d
160 for c = 0 to 33
170 read b
180 let d(c) = b
190 next c
200 for i = 1 to 33
210 let j = int((d(i)+5.625)/11.25)
220 if j > 31 then let j = j-32
230 print d(i);" ";j;" ";n$(j)
240 next i
250 end
260 data "North","North by east","North-northeast","Northeast by north"
270 data "Northeast","Northeast by east","East-northeast","East by north"
280 data "East","East by south","East-southeast","Southeast by east"
290 data "Southeast","Southeast by south","South-southeast","South by east"
300 data "South","South by west","South-southwest","Southwest by south"
310 data "Southwest","Southwest by west","West-southwest","West by south"
320 data "West","West by north","West-northwest","Northwest by west","Northwest"
330 data "Northwest by north","North-northwest","North by west","North"
340 data 0,16.87,16.88,33.75,50.62,50.63,67.5,84.37,84.38,101.25
350 data 118.12,118.13,135,151.87,151.88,168.75,185.62,185.63,202.5,219.37
360 data 219.38,236.25,253.12,253.13,270,286.87,286.88,303.75,320.62,320.63
370 data 337.5,354.37,354.38</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">>>::"}"9**\4+3%79*9*5-*79*9*5--+:5>>>06p:55+%68*+v
Line 1,875 ⟶ 1,992:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Box_the_compass#Pascal Pascal].
=={{header|EasyLang}}==
{{trans|Kotlin}}
<syntaxhighlight>
func$ expand cp$ .
for c$ in strchars cp$
if c$ = "N"
r$ &= "north"
elif c$ = "E"
r$ &= "east"
elif c$ = "S"
r$ &= "south"
elif c$ = "W"
r$ &= "west"
elif c$ = "b"
r$ &= "by"
else
r$ &= "-"
.
.
h$ = strchar (strcode substr r$ 1 1 - 32)
return h$ & substr r$ 2 999
.
proc main . .
cp$[] = [ "N" "NbE" "N-NE" "NEbN" "NE" "NEbE" "E-NE" "EbN" "E" "EbS" "E-SE" "SEbE" "SE" "SEbS" "S-SE" "SbE" "S" "SbW" "S-SW" "SWbS" "SW" "SWbW" "W-SW" "WbS" "W" "WbN" "W-NW" "NWbW" "NW" "NWbN" "N-NW" "NbW" ]
print "Index Degrees Compass point"
print "----- ------- -------------"
for i = 0 to 32
ind = (i + 1) mod1 32
heading = i * 11.25
if i mod 3 = 1
heading += 5.62
elif i mod 3 = 2
heading -= 5.62
.
print ind & "\t" & heading & "\t" & expand cp$[ind]
.
.
main
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
Line 1,938 ⟶ 2,095:
1 North 354.38
</pre>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">constant names = {"North","North by east","North-northeast","Northeast by north",
Line 3,615 ⟶ 3,773:
=={{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",
Line 3,635 ⟶ 3,792:
 
for .phi in .angles {
val .i = truncatetrunc(.phi x* 32 / 360 + 0.5) rem 32 + 1
writeln $"\{.i:5;} \{.phi:r2:6;} \{.box[.i];}"
}</syntaxhighlight>
 
Line 3,676 ⟶ 3,833:
1 354.38 North
</pre>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">define pointsarray() => {
Line 6,621 ⟶ 6,779:
354.38 0 North
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.9}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ { "north" "east" "south" "west" }
SWAP 8 / IP 1 + GET
LAST 1 + DUP 5 ≠ SWAP 1 IFTE GET
≫ '<span style="color:blue">I→QDT</span>' STO
≪ → pattern a b
≪ "" 1 pattern SIZE '''FOR''' j
pattern j DUP SUB
DUP "X" == a ROT DUP "Y" == b ROT IFTE IFTE
+ '''NEXT'''
≫ ≫ '<span style="color:blue">FILLXY</span>' STO
11.25 / 0.5 + FLOOR 32 MOD
{ "X" "X by Y" "X-XY" "XY by X"
"XY" "XY by Y" "Y-XY" "Y by X" "Y" }
OVER 8 MOD 1 + GET SWAP <span style="color:blue">I→QDT</span> <span style="color:blue">FILLXY</span>
≫ '<span style="color:blue">→HDG</span>' STO
|
<span style="color:blue">I→QDT</span> ''( index → "wind1" "wind2" )'' // index ∈ [0,31]
Get wind1
Get wind2
<span style="color:blue">FILLXY</span> ''( "pattern" "a" "b" → "string" ) ''
Initialize output string and loop
get jth character of pattern
replace "X" by "a" and "Y" by b
add to output string
<span style="color:blue">→HDG</span> ''( angle → "heading" )'' // angle in degreees
Convert angle into compass index
Naming patterns
Get pattern, get winds and convert into heading
|}
The demonstration has been limited to a few significant cases, due to the poor display capabilities of typical RPL devices (e.g. 4 x 22 characters on a HP-28S) :
≪ <span style="color:red">{ 0 16.87 16.88 33.75 50.62 354.37 354.38 } { } 1 3</span> PICK SIZE '''FOR''' j OVER j GET <span style="color:blue">→HDG</span> + '''NEXT''' SWAP DROP ≫ EVAL
 
{{out}}
<pre>
1: { "north" "north by east" "north-northeast" "northeast by north" "northeast" "north by west" "north" }
</pre>
 
=={{header|Ruby}}==
 
Line 6,682 ⟶ 6,895:
32 north by west 354.37
1 north 354.38</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">global direct$
Line 7,086 ⟶ 7,300:
32 North by west NbW 343.13 348.75 354.37
</pre>
=={{header|Swift}}==
Implementation:
<syntaxhighlight lang="swift">
import Foundation
 
extension Double {
var nearestQr: Double {(self * 4.0).rounded(.toNearestOrAwayFromZero) / 4.0}
}
 
extension Measurement<UnitAngle>/*: ExpressibleByFloatLiteral*/ {
var cos: Double { Darwin.cos(self.converted(to: .radians).value) }
var sin: Double { Darwin.sin(self.converted(to: .radians).value) }
}
 
struct Compass {
var bearing: Measurement<UnitAngle>
var style: Style = .RN
init(_ deg: Double, _ min: Double = 0, _ sec: Double = 0, style: Style = .RN
) {
self.bearing = .init(value: deg + min/60.0 + sec/360.0, unit: .degrees)
self.style = style
}
static func degreesToPoints(_ deg: Double) -> Double {
(deg/360 * 32).nearestQr
}
var point: Double {
Self.degreesToPoints(self.bearing.value)
}
var quad: (String,String) {
(bearing.cos < 0 ? "S" : "N", bearing.sin < 0 ? "W" : "E")
}
var step: (Int,Double) {
let temp = 8 - abs(abs(self.point - 16) - 8)
return (Int(temp),temp.truncatingRemainder(dividingBy: 1))
}
enum Style {case RN, USN, noBy}
var formats = ["N", "NxE", "NNE", "NExN", "NE", "NExE", "ENE", "ExN", "E"]
let fractions = ["¼","½","¾"]
var invertedPoints: [Int] {
switch self.style {
case .RN: [3,6,7]
case .USN: [3,7]
case .noBy: [1,5,7]
}
}
 
func named() -> String {
var (pt,frac) = self.step
var fracStr: String = ""
if frac != 0.0 {
if invertedPoints.contains(pt) {
pt += 1
fracStr = fractions.reversed()[Int(frac * 4) - 1] + "N"
} else {
fracStr = fractions[Int(frac * 4) - 1] + "E"
}
}
return (self.formats[pt] + fracStr)
.replacing(/(N|E)/) { $0.output.0 == "N" ? self.quad.0 : self.quad.1 }
.replacing(/x/) {_ in "by"}
}
}
</syntaxhighlight>
Execution:
<syntaxhighlight lang="swift">
let arr = [
000.00, 016.87, 016.88, 033.75, 050.62, 050.63, 067.50, 084.37, 084.38,
090.00, 101.25, 118.12, 118.13, 135.00, 151.87, 151.88, 168.75, 185.62,
185.63, 202.50, 219.37, 219.38, 236.25, 253.12, 253.13, 270.00, 286.87,
286.88, 303.75, 320.62, 320.63, 337.50, 354.37, 354.38
]
 
let arr2 = stride(from: 0, through: 360, by: 22.5/8.0)
 
let pointFormatter = NumberFormatter()
pointFormatter.minimumIntegerDigits = 2
pointFormatter.minimumFractionDigits = 2
 
for d in arr {
let c = Compass(d, style: .RN)
print(pointFormatter.string(from: c.point as NSNumber)!, c.dms , c.named())
}
</syntaxhighlight>
Result:
<pre>
00.00 000°00'00" N
01.50 016°52'12" NbyE½E
01.50 016°52'47" NbyE½E
03.00 033°45'00" NEbyN
04.50 050°37'11" NE½E
04.50 050°37'48" NE½E
06.00 067°30'00" ENE
07.50 084°22'12" E½N
07.50 084°22'47" E½N
08.00 090°00'00" E
09.00 101°15'00" EbyS
10.50 118°07'12" SEbyE½E
10.50 118°07'47" SEbyE½E
12.00 135°00'00" SE
13.50 151°52'12" SSE½E
13.50 151°52'47" SSE½E
15.00 168°45'00" SbyE
16.50 185°37'12" S½W
16.50 185°37'47" S½W
18.00 202°30'00" SSW
19.50 219°22'12" SW½S
19.50 219°22'47" SW½S
21.00 236°15'00" SWbyW
22.50 253°07'12" WbyS½S
22.50 253°07'47" WbyS½S
24.00 270°00'00" W
25.50 286°52'12" WbyN½N
25.50 286°52'47" WbyN½N
27.00 303°45'00" NWbyW
28.50 320°37'12" NW½N
28.50 320°37'47" NW½N
30.00 337°30'00" NNW
31.50 354°22'12" N½W
31.50 354°22'47" N½W
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc angle2compass {angle} {
Line 7,529 ⟶ 7,864:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
// 'cpx' returns integer index from 0 to 31 corresponding to compass point.
Line 7,592 ⟶ 7,927:
var index = i%32 + 1 // printable index computed per pseudocode
var d = degreesToCompassPoint.call(h)
SystemFmt.print("%(Fmt.d(4,$4d index)) $-19s %(Fmt$7.s(-192f°", index, d)) %(Fmt.f(7, h,) 2))°")
i = i + 1
}</syntaxhighlight>
Line 7,748 ⟶ 8,083:
 
=={{header|Zig}}==
{{works with|Zig|0.11.0dev0}}
<syntaxhighlight lang="zig">const std = @import("std");</syntaxhighlight>
<syntaxhighlight lang="zig">/// Degrees areis not constrained to the range 0 to 360
fn degreesToCompassPoint(degrees: f32) []const u8 {
var d = degrees + comptime (11.25 / 2.0);
while (d < 0) d += 360;
while (d >= 360) d -= 360;
const index: usize = @floatToIntintFromFloat(usize, @divFloor(d, 11.25));
 
const points: [32][]const u8 = comptime .{
// Concatenation to overcome the inability of "zig fmt" to nicely format long arrays.
"North", "North by east", "North-northeast", "Northeast by north",
const points: [32][]const u8 = comptime .{} ++
.{ "NorthNortheast", "NorthNortheast by east", "NorthEast-northeast", "NortheastEast by north" } ++,
.{ "NortheastEast", "NortheastEast by eastsouth", "East-northeastsoutheast", "EastSoutheast by northeast" } ++,
.{ "EastSoutheast", "EastSoutheast by south", "EastSouth-southeast", "SoutheastSouth by east" } ++,
.{ "SoutheastSouth", "SoutheastSouth by southwest", "South-southeastsouthwest", "SouthSouthwest by eastsouth" } ++,
.{ "SouthSouthwest", "SouthSouthwest by west", "SouthWest-southwest", "SouthwestWest by south" } ++,
.{ "SouthwestWest", "SouthwestWest by westnorth", "West-southwestnorthwest", "WestNorthwest by southwest" } ++,
.{ "WestNorthwest", "WestNorthwest by north", "WestNorth-northwest", "NorthwestNorth by west" } ++,
};
.{ "Northwest", "Northwest by north", "North-northwest", "North by west" };
 
return points[index];
Line 7,773 ⟶ 8,108:
const stdout = std.io.getStdOut().writer();
 
_ = try stdout.print("Index Heading Compass point\n", .{});
 
for (0..33) |i| {
var heading = @intToFloatas(f32, @floatFromInt(i)) * 11.25;
heading += switch (i % 3) {
1 => 5.62,
Line 7,783 ⟶ 8,118:
};
const index = i % 32 + 1;
_ = try stdout.print(" {d:2} {d:>6.2}° {s}\n", .{ index, heading, degreesToCompassPoint(heading) });
}
}</syntaxhighlight>
885

edits