Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
59
Task/Box-the-compass/Swift/box-the-compass-1.swift
Normal file
59
Task/Box-the-compass/Swift/box-the-compass-1.swift
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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"}
|
||||
}
|
||||
}
|
||||
17
Task/Box-the-compass/Swift/box-the-compass-2.swift
Normal file
17
Task/Box-the-compass/Swift/box-the-compass-2.swift
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue