-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoonPhase.java
44 lines (34 loc) · 1.47 KB
/
MoonPhase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package littlesky.model.moon;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.value.ObservableValue;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import static littlesky.util.BindingBuilder.*;
public class MoonPhase {
private static final LocalDate BASE_DATE = LocalDate.of(1999, 1, 1);
private static final double AGE_AT_BASE_DATE = 13.17;
private static final double AVERAGE_OF_MOON_PERIOD = 29.53059;
private static final double METONIC_CYCLE = 6939.68842;
private final ReadOnlyDoubleWrapper phase = new ReadOnlyDoubleWrapper();
public void bind(ObservableValue<LocalDate> date) {
this.phase.bind(
binding(date).computeValue(() -> this.calculatePhase(date.getValue()))
);
}
public ReadOnlyDoubleProperty phaseProperty() {
return this.phase.getReadOnlyProperty();
}
public double getPhase() {
return this.phase.get();
}
private double calculatePhase(LocalDate date) {
double age = this.calculateAge(date);
return age / AVERAGE_OF_MOON_PERIOD;
}
private double calculateAge(LocalDate date) {
long days = Math.abs(BASE_DATE.until(date, ChronoUnit.DAYS));
double normalizedDays = days % METONIC_CYCLE + 0.5; // at noon
return (AGE_AT_BASE_DATE + normalizedDays) % AVERAGE_OF_MOON_PERIOD;
}
}