Skip to content

Commit

Permalink
Reduce error of MoonIllumination.getAngle()
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Apr 14, 2023
1 parent 8944642 commit df208d9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit df208d9

Please sign in to comment.