krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Commit 2381013e89

2381013e89cb22ed062e0b43e0a4c41b9b14602e

parent: ed4ae2da67

Unsigned

cmc <hello@cleberg.net> · 2026-10-22 04:00 UTC

Sky: horizon profile (!122)

- `HorizonProfile`: 36 altitudes from north in 10° steps, linear between; `stored` / `init?(stored:)` for `@AppStorage(horizonProfile)`.
- `SkyView(horizon:)`: `drawHorizon` traces the ground edge at the profile's altitude (or 0), so objects behind trees and buildings are covered.
- Settings → Display → Horizon: bar chart 0–60°, drag to set each 10° sector; Clear.
- Test: interpolation, wrap-around, storage. Rendered on macOS with a 20–30° southern skyline. iOS builds.

Layout: unified · split

App/Sources/HorizonEditor.swift added +48
@@ -0,0 +1,48 @@
1import Charts
2import SkyRenderer
3import SwiftUI
4
5/// The skyline every 10°: drag each bar up to where trees or buildings end in that direction.
6struct HorizonEditor: View {
7 @AppStorage(SettingsKey.horizonProfile) private var stored = ""
8 @State private var profile = HorizonProfile()
9
10 var body: some View {
11 Form {
12 Section {
13 Chart(Array(profile.altitudes.enumerated()), id: \.offset) { i, alt in
14 BarMark(x: .value("Azimuth", Double(i) * HorizonProfile.step + 5), y: .value("Altitude", alt), width: .ratio(0.9))
15 }
16 .chartXScale(domain: 0...360)
17 .chartYScale(domain: 0...60)
18 .chartXAxis { AxisMarks(values: [0, 90, 180, 270, 360]) { v in AxisGridLine(); AxisValueLabel(compass(v.as(Double.self) ?? 0)) } }
19 .chartYAxis { AxisMarks(values: [0, 15, 30, 45, 60]) { v in AxisGridLine(); AxisValueLabel("\(v.as(Int.self) ?? 0)°") } }
20 .chartOverlay { chart in
21 GeometryReader { g in
22 Rectangle().fill(.clear).contentShape(Rectangle())
23 .gesture(DragGesture(minimumDistance: 0).onChanged { drag in
24 guard let frame = chart.plotFrame.map({ g[$0] }),
25 let az: Double = chart.value(atX: drag.location.x - frame.minX),
26 let alt: Double = chart.value(atY: drag.location.y - frame.minY) else { return }
27 let i = min(35, max(0, Int(az / HorizonProfile.step)))
28 profile.altitudes[i] = min(60, max(0, alt.rounded()))
29 stored = profile.stored
30 })
31 }
32 }
33 .frame(height: 220)
34 .accessibilityLabel("Skyline height by direction")
35 Button("Clear", role: .destructive) { profile = HorizonProfile(); stored = profile.stored }
36 .disabled(profile.isFlat)
37 } footer: {
38 Text("Drag along the chart to set how high trees and buildings reach in each direction, from north round through east. The sky chart draws them as ground, so whatever is behind them is hidden.")
39 }
40 }
41 .navigationTitle("Horizon")
42 .onAppear { profile = HorizonProfile(stored: stored) ?? HorizonProfile() }
43 }
44
45 private func compass(_ az: Double) -> String {
46 ["N", "E", "S", "W", "N"][min(4, Int((az / 90).rounded()))]
47 }
48}
App/Sources/SettingsScreen.swift +3
@@ -24,6 +24,8 @@ enum SettingsKey {
24 /// Messier objects on the sky chart. 24 /// Messier objects on the sky chart.
25 static let showDeepSky = "showDeepSky" 25 static let showDeepSky = "showDeepSky"
26 static let showMilkyWay = "showMilkyWay" 26 static let showMilkyWay = "showMilkyWay"
27 /// HorizonProfile.stored: 36 skyline altitudes.
28 static let horizonProfile = "horizonProfile"
27 /// Cloud forecasts from Apple Weather; off unless turned on. 29 /// Cloud forecasts from Apple Weather; off unless turned on.
28 static let cloudForecast = "cloudForecast" 30 static let cloudForecast = "cloudForecast"
29 /// The eclipse eye-safety note has been shown before a countdown or rehearsal. 31 /// The eclipse eye-safety note has been shown before a countdown or rehearsal.
@@ -161,6 +163,7 @@ struct SettingsScreen: View {
161 } 163 }
162 Toggle("Messier objects", isOn: $showDeepSky) 164 Toggle("Messier objects", isOn: $showDeepSky)
163 Toggle("Milky Way", isOn: $showMilkyWay) 165 Toggle("Milky Way", isOn: $showMilkyWay)
166 NavigationLink("Horizon") { HorizonEditor() }
164 Picker("Night mode", selection: $nightMode) { 167 Picker("Night mode", selection: $nightMode) {
165 Text("Off").tag("off") 168 Text("Off").tag("off")
166 Text("On").tag("on") 169 Text("On").tag("on")
App/Sources/SkyScreen.swift +2 −1
@@ -14,6 +14,7 @@ struct SkyScreen: View {
14 @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western" 14 @AppStorage(SettingsKey.skyCulture) private var skyCulture = "western"
15 @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true 15 @AppStorage(SettingsKey.showDeepSky) private var showDeepSky = true
16 @AppStorage(SettingsKey.showMilkyWay) private var showMilkyWay = true 16 @AppStorage(SettingsKey.showMilkyWay) private var showMilkyWay = true
17 @AppStorage(SettingsKey.horizonProfile) private var horizonProfile = ""
17 @State private var showSettings = false 18 @State private var showSettings = false
18 @State private var showEvents = false 19 @State private var showEvents = false
19 @State private var showLog = false 20 @State private var showLog = false
@@ -47,7 +48,7 @@ struct SkyScreen: View {
47 style.background.ignoresSafeArea() 48 style.background.ignoresSafeArea()
48 if let scene = model.scene { 49 if let scene = model.scene {
49 let view = current(width: geo.size.width) 50 let view = current(width: geo.size.width)
50 SkyView(scene: scene, centerAzimuth: view.azimuth, centerAltitude: view.altitude, fieldOfView: view.fieldOfView, style: style, culture: Catalog.skyCultures.first { $0.id == skyCulture }, highlight: (tour.map { $0.steps[tourStep].target } ?? shown).flatMap(highlight), showDeepSky: showDeepSky, showMilkyWay: showMilkyWay) { [stats] d in 51 SkyView(scene: scene, centerAzimuth: view.azimuth, centerAltitude: view.altitude, fieldOfView: view.fieldOfView, style: style, culture: Catalog.skyCultures.first { $0.id == skyCulture }, highlight: (tour.map { $0.steps[tourStep].target } ?? shown).flatMap(highlight), showDeepSky: showDeepSky, showMilkyWay: showMilkyWay, horizon: HorizonProfile(stored: horizonProfile)) { [stats] d in
51 stats.record(d) 52 stats.record(d)
52 } 53 }
53 .ignoresSafeArea() 54 .ignoresSafeArea()
PLAN.org +2 −1
@@ -540,7 +540,8 @@ Worker; DSN Now freshness; NASA/ESA image licences; 26 LoC plates bundled.
540 1.6 MB binary), NGC objects, a deeper zoom and eyepiece field-of-view 540 1.6 MB binary), NGC objects, a deeper zoom and eyepiece field-of-view
541 circles from the equipment inventory. Merge: drop Tycho-2 stars matched to 541 circles from the equipment inventory. Merge: drop Tycho-2 stars matched to
542 BSC5 by HIP/HD, then by position (10″, ΔV 0.5); BSC5 keeps HR numbers. 542 BSC5 by HIP/HD, then by position (10″, ΔV 0.5); BSC5 keeps HR numbers.
543- Horizon profile. 543- Horizon profile. Done: 36 altitudes (10° steps) set by dragging in Settings
544 → Horizon; the sky chart draws them as ground.
544 545
545** v5.3: AR camera overlay 546** v5.3: AR camera overlay
546- First measure draw time and pointing on a device. Heading from the 547- First measure draw time and pointing on a device. Heading from the
Packages/MoonriseKit/Sources/SkyRenderer/HorizonProfile.swift added +30
@@ -0,0 +1,30 @@
1import Foundation
2
3/// The local skyline: how high trees and buildings reach, every 10° of azimuth from north, in degrees.
4public struct HorizonProfile: Sendable, Equatable, Codable {
5 public static let step = 10.0
6 public var altitudes: [Double]
7
8 public init(altitudes: [Double] = Array(repeating: 0, count: 36)) {
9 precondition(altitudes.count == 36)
10 self.altitudes = altitudes
11 }
12
13 public var isFlat: Bool { altitudes.allSatisfy { $0 <= 0 } }
14
15 /// Linear between the 10° marks.
16 public func altitude(at azimuth: Double) -> Double {
17 let a = (azimuth.truncatingRemainder(dividingBy: 360) + 360).truncatingRemainder(dividingBy: 360) / Self.step
18 let i = Int(a) % 36, f = a - a.rounded(.down)
19 return altitudes[i] * (1 - f) + altitudes[(i + 1) % 36] * f
20 }
21
22 /// For @AppStorage.
23 public var stored: String { altitudes.map { String(Int($0.rounded())) }.joined(separator: ",") }
24
25 public init?(stored: String) {
26 let v = stored.split(separator: ",").compactMap { Double($0) }
27 guard v.count == 36 else { return nil }
28 self.init(altitudes: v)
29 }
30}
Packages/MoonriseKit/Sources/SkyRenderer/SkyView.swift +5 −2
@@ -115,16 +115,19 @@ public struct SkyView: View {
115 public var highlight: SkyHighlight? 115 public var highlight: SkyHighlight?
116 public var showDeepSky: Bool 116 public var showDeepSky: Bool
117 public var showMilkyWay: Bool 117 public var showMilkyWay: Bool
118 /// Trees and buildings drawn as ground above the horizon.
119 public var horizon: HorizonProfile?
118 /// Called after each draw with the time the draw took. 120 /// Called after each draw with the time the draw took.
119 public var onDraw: (@Sendable (Duration) -> Void)? 121 public var onDraw: (@Sendable (Duration) -> Void)?
120 122
121 public init(scene: SkyScene, centerAzimuth: Double = 180, centerAltitude: Double = 55, fieldOfView: Double = 110, style: SkyStyle = SkyStyle(), culture: SkyCulture? = nil, highlight: SkyHighlight? = nil, showDeepSky: Bool = true, showMilkyWay: Bool = true, onDraw: (@Sendable (Duration) -> Void)? = nil) { 123 public init(scene: SkyScene, centerAzimuth: Double = 180, centerAltitude: Double = 55, fieldOfView: Double = 110, style: SkyStyle = SkyStyle(), culture: SkyCulture? = nil, highlight: SkyHighlight? = nil, showDeepSky: Bool = true, showMilkyWay: Bool = true, horizon: HorizonProfile? = nil, onDraw: (@Sendable (Duration) -> Void)? = nil) {
122 self.scene = scene 124 self.scene = scene
123 self.style = style 125 self.style = style
124 self.culture = culture 126 self.culture = culture
125 self.highlight = highlight 127 self.highlight = highlight
126 self.showDeepSky = showDeepSky 128 self.showDeepSky = showDeepSky
127 self.showMilkyWay = showMilkyWay 129 self.showMilkyWay = showMilkyWay
130 self.horizon = horizon
128 self.centerAzimuth = centerAzimuth 131 self.centerAzimuth = centerAzimuth
129 self.centerAltitude = centerAltitude 132 self.centerAltitude = centerAltitude
130 self.fieldOfView = fieldOfView 133 self.fieldOfView = fieldOfView
@@ -343,7 +346,7 @@ public struct SkyView: View {
343 var edge: [CGPoint] = [] 346 var edge: [CGPoint] = []
344 var az = centerAzimuth - fieldOfView 347 var az = centerAzimuth - fieldOfView
345 while az <= centerAzimuth + fieldOfView { 348 while az <= centerAzimuth + fieldOfView {
346 if let pt = p.point(Horizontal(altitude: 0, azimuth: az)) { edge.append(pt) } 349 if let pt = p.point(Horizontal(altitude: max(0, horizon?.altitude(at: az) ?? 0), azimuth: az)) { edge.append(pt) }
347 az += 1 350 az += 1
348 } 351 }
349 guard let first = edge.first, let last = edge.last else { return } 352 guard let first = edge.first, let last = edge.last else { return }
Packages/MoonriseKit/Tests/SkyRendererTests/HorizonProfileTests.swift added +16
@@ -0,0 +1,16 @@
1import Foundation
2import Testing
3@testable import SkyRenderer
4
5@Test func horizonProfileInterpolatesAndStores() throws {
6 var h = HorizonProfile()
7 #expect(h.isFlat)
8 h.altitudes[9] = 20 // 90°, east
9 #expect(h.altitude(at: 90) == 20)
10 #expect(h.altitude(at: 95) == 10)
11 #expect(h.altitude(at: 85) == 10)
12 #expect(h.altitude(at: 450) == 20)
13 #expect(h.altitude(at: -270) == 20)
14 #expect(HorizonProfile(stored: h.stored) == h)
15 #expect(HorizonProfile(stored: "1,2") == nil)
16}