Skip to content

Commit d9cd4cd

Browse files
committed
refactor: update MoonCalculator to use horizon and rotation for true limb calculation
1 parent fea7dda commit d9cd4cd

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

‎app/src/main/kotlin/dev/maxine/librelune/moon/MoonCalculator.kt‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import io.github.cosinekitty.astronomy.Aberration
55
import io.github.cosinekitty.astronomy.Body
66
import io.github.cosinekitty.astronomy.EquatorEpoch
77
import io.github.cosinekitty.astronomy.Observer
8+
import io.github.cosinekitty.astronomy.Refraction
89
import io.github.cosinekitty.astronomy.Time
910
import io.github.cosinekitty.astronomy.equator
10-
import io.github.cosinekitty.astronomy.hourAngle
11+
import io.github.cosinekitty.astronomy.horizon
1112
import io.github.cosinekitty.astronomy.illumination
1213
import io.github.cosinekitty.astronomy.moonPhase
14+
import io.github.cosinekitty.astronomy.rotationEqdHor
1315
import io.github.cosinekitty.astronomy.searchMoonPhase
1416
import java.time.ZoneId
1517
import java.time.ZoneOffset
1618
import java.time.ZonedDateTime
1719
import kotlin.math.atan2
18-
import kotlin.math.cos
1920
import kotlin.math.roundToInt
20-
import kotlin.math.sin
2121

2222
private const val PHASE_SEARCH_DAYS = 35.0
2323

@@ -51,23 +51,15 @@ class MoonCalculator(
5151
val observer = Observer(latitude, longitude, 0.0)
5252
val moon = equator(Body.Moon, time, observer, EquatorEpoch.OfDate, Aberration.Corrected)
5353
val sun = equator(Body.Sun, time, observer, EquatorEpoch.OfDate, Aberration.Corrected)
54-
val deltaRa = Math.toRadians((sun.ra - moon.ra) * 15.0)
55-
val moonDec = Math.toRadians(moon.dec)
56-
val sunDec = Math.toRadians(sun.dec)
57-
val brightLimbDeg = Math.toDegrees(atan2(
58-
cos(sunDec) * sin(deltaRa),
59-
sin(sunDec) * cos(moonDec) -
60-
cos(sunDec) * sin(moonDec) * cos(deltaRa),
61-
))
54+
// Keep geometric coordinates: refraction would change the existing tilt.
55+
val moonHorizon = horizon(time, observer, moon.ra, moon.dec, Refraction.None)
6256

63-
val hourAngleRad = Math.toRadians(hourAngle(Body.Moon, time, observer) * 15.0)
64-
val latitudeRad = Math.toRadians(latitude)
65-
val parallacticAngleDeg = Math.toDegrees(atan2(
66-
sin(hourAngleRad) * cos(latitudeRad),
67-
sin(latitudeRad) * cos(moonDec) -
68-
cos(latitudeRad) * sin(moonDec) * cos(hourAngleRad),
69-
))
70-
val trueLimbDeg = -(brightLimbDeg - parallacticAngleDeg).toFloat()
57+
// Point the horizontal frame at the Moon: x toward it, y screen-left, z screen-up.
58+
val moonFrame = rotationEqdHor(time, observer)
59+
.pivot(2, moonHorizon.azimuth)
60+
.pivot(1, moonHorizon.altitude)
61+
val sunInMoonFrame = moonFrame.rotate(sun.vec)
62+
val trueLimbDeg = -Math.toDegrees(atan2(sunInMoonFrame.y, sunInMoonFrame.z)).toFloat()
7163

7264
// The base artwork (and the procedural line path) already draws the
7365
// illuminated limb at a canonical orientation: 90deg (right) when the

‎app/src/test/kotlin/dev/maxine/librelune/moon/MoonCalculatorTest.kt‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,51 @@ class MoonCalculatorTest {
8686
assertTrue(result.wobbleDeg in -90f..90f)
8787
assertTrue(kotlin.math.abs(result.wobbleDeg) > 0.01f)
8888
}
89+
90+
@Test
91+
fun `horizontal frame wobble matches reference angles`() {
92+
data class Case(val time: String, val latitude: Double, val longitude: Double, val north: Float, val south: Float)
93+
94+
// Reference angles from the previous bright-limb/parallactic-angle formulas.
95+
val cases = listOf(
96+
Case("2024-04-01T00:00Z", 52.5, 13.4, -36.096344f, 90f),
97+
Case("2024-04-13T12:00Z", -33.87, 151.21, 90f, -54.06665f),
98+
Case("2024-04-08T01:00Z", 0.0, 0.0, -55.570618f, 90f),
99+
Case("2024-04-23T10:00Z", 0.0, 180.0, -75.7387f, 90f),
100+
Case("2024-04-15T02:00Z", 90.0, -180.0, -7.0262146f, 90f),
101+
Case("2024-04-30T11:00Z", -90.0, 180.0, -90f, 10.786774f),
102+
Case("2024-04-20T13:00Z", 89.999, -179.999, -24.896347f, 90f),
103+
Case("2024-04-10T15:00Z", -100.0, 220.0, -90f, 24.218506f),
104+
)
105+
106+
for (case in cases) {
107+
for (hemisphere in Hemisphere.entries) {
108+
val result = MoonCalculator(
109+
zoneId = ZoneOffset.UTC,
110+
wobbleEnabled = true,
111+
latitudeDeg = case.latitude,
112+
longitudeDeg = case.longitude,
113+
hemisphere = hemisphere,
114+
).now(ZonedDateTime.parse(case.time))
115+
val expected = if (hemisphere == Hemisphere.NORTHERN) case.north else case.south
116+
117+
assertEquals(expected, result.wobbleDeg, 0.001f, "$case, $hemisphere")
118+
}
119+
}
120+
}
121+
122+
@Test
123+
fun `enabling wobble leaves other moon state unchanged`() {
124+
val now = ZonedDateTime.of(2024, 4, 13, 20, 0, 0, 0, ZoneOffset.UTC)
125+
val disabled = MoonCalculator(ZoneOffset.UTC, latitudeDeg = 52.5, longitudeDeg = 13.4).now(now)
126+
val enabled = MoonCalculator(
127+
ZoneOffset.UTC,
128+
wobbleEnabled = true,
129+
latitudeDeg = 52.5,
130+
longitudeDeg = 13.4,
131+
).now(now)
132+
133+
assertEquals(0f, disabled.wobbleDeg)
134+
assertEquals(disabled, enabled.copy(wobbleDeg = 0f))
135+
}
89136
}

0 commit comments

Comments
 (0)