From df208d901e3a8149a39d1c1411e1e440db1e1ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Fri, 14 Apr 2023 13:49:42 +0200 Subject: [PATCH] Reduce error of MoonIllumination.getAngle() --- .../org/shredzone/commons/suncalc/MoonIllumination.java | 6 +++++- .../org/shredzone/commons/suncalc/MoonIlluminationTest.java | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java b/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java index f8b42ed..9bf9f7a 100644 --- a/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java +++ b/src/main/java/org/shredzone/commons/suncalc/MoonIllumination.java @@ -69,11 +69,15 @@ public MoonIllumination execute() { Vector m = Moon.position(t); double phi = PI - acos(m.dot(s) / (m.getR() * s.getR())); Vector sunMoon = m.cross(s); + double angle = atan2( + cos(s.getTheta()) * sin(s.getPhi() - m.getPhi()), + sin(s.getTheta()) * cos(m.getTheta()) - cos(s.getTheta()) * sin(m.getTheta()) * cos(s.getPhi() - m.getPhi()) + ); return new MoonIllumination( (1 + cos(phi)) / 2, toDegrees(phi * signum(sunMoon.getTheta())), - toDegrees(sunMoon.getTheta())); + toDegrees(angle)); } } diff --git a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java index 96eb5bb..8617be4 100644 --- a/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java +++ b/src/test/java/org/shredzone/commons/suncalc/MoonIlluminationTest.java @@ -35,7 +35,7 @@ public void testNewMoon() { .execute(); assertThat(mi.getFraction()).as("fraction").isCloseTo(0.0, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(176.0, ERROR); // -180.0 - assertThat(mi.getAngle()).as("angle").isCloseTo(1.8, ERROR); + assertThat(mi.getAngle()).as("angle").isCloseTo(2.0, ERROR); assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.NEW_MOON); } @@ -59,7 +59,7 @@ public void testFullMoon() { .execute(); assertThat(mi.getFraction()).as("fraction").isCloseTo(1.0, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(-3.2, ERROR); // 0.0 - assertThat(mi.getAngle()).as("angle").isCloseTo(-7.0, ERROR); + assertThat(mi.getAngle()).as("angle").isCloseTo(-7.4, ERROR); assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.FULL_MOON); } @@ -71,7 +71,7 @@ public void testWaningHalfMoon() { .execute(); assertThat(mi.getFraction()).as("fraction").isCloseTo(0.5, ERROR); assertThat(mi.getPhase()).as("phase").isCloseTo(90.0, ERROR); - assertThat(mi.getAngle()).as("angle").isCloseTo(68.1, ERROR); + assertThat(mi.getAngle()).as("angle").isCloseTo(68.7, ERROR); assertThat(mi.getClosestPhase()).as("MoonPhase.Phase").isEqualTo(Phase.LAST_QUARTER); }