Box the compass: Difference between revisions

m (→‎{{header|Wren}}: Minor tidy)
imported>Regattaguru
Line 7,259:
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} {
Anonymous user