DayByDay/Quiz/YearWheel.swift
32 lines · 1178 bytes
1//
2// YearWheel.swift
3// DayByDay
4//
5
6import Foundation
7
8/// Geometry of the year wheel. Month `i` (January = 0) is drawn centered
9/// `i * 30°` clockwise from the pointer at the top; the wheel then turns
10/// clockwise by its rotation.
11nonisolated enum YearWheel {
12 static let wedge = 30.0
13
14 /// The month under the pointer after turning the wheel clockwise by `degrees`.
15 static func month(atRotation degrees: Double) -> MonthOfYear {
16 let index = Int((-degrees / wedge).rounded())
17 return MonthOfYear.allCases[((index % 12) + 12) % 12]
18 }
19
20 /// The first rotation at least `turns` full turns past `rotation` that
21 /// puts the center of `month` under the pointer.
22 static func target(from rotation: Double, landingOn month: MonthOfYear, turns: Int) -> Double {
23 let index = Double(MonthOfYear.allCases.firstIndex(of: month)!)
24 let base = -index * wedge
25 let minimum = rotation + Double(turns) * 360
26 return base + 360 * ((minimum - base) / 360).rounded(.up)
27 }
28
29 static func line(for month: MonthOfYear) -> String {
30 "\(month.name)! \(month.name) is in \(month.season.name.lowercased())."
31 }
32}