You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This floating point moon phase algorithm works by simply dividing the lunar month of 29.53 days into the number
51
+
// of days elapsed since a known new moon. So, it’s highly simplified, but should be good enough to get quarter
52
+
// phases.
53
+
54
+
uint32_t yearAsDays;
55
+
uint16_t monthAsDays;
50
56
double daysSinceReferenceNewMoon;
51
57
double moonCycles;
52
58
uint16_t completedMoonCycles;
53
-
double moonAge;
59
+
double moonAge;
54
60
uint8_t phase;
55
61
62
+
// This adjustment ultimately comes from the calculation to turn y/m/d into a whole number of days offset. It’s a
63
+
// modified version of the Julian Day number, but here it’s been simplified to work only between years 2000/1/1 and
64
+
// 2099/12/31.
56
65
if (month < 3) {
57
66
year--;
58
67
month += 12;
59
68
}
60
-
++month; /* why incrementing the month? */
69
+
month++;
61
70
62
-
yearAsDays = 365.25 * year;/* 365.25 -> mean length of a calendar year*/
63
-
monthAsDays = 30.6 * month;/* 30.6 -> mean length of a month*/
64
-
daysSinceReferenceNewMoon = yearAsDays + monthAsDays + day - 694039.09; /* number of days since known new moon on 1900-01-01, 694039.09 -> days elapsed since zero*/
65
-
moonCycles = daysSinceReferenceNewMoon / 29.53;/* 29.53 -> long-term average moon cycle duration in days*/
66
-
completedMoonCycles = moonCycles;/* "casting" to int to get *completed* moon cycles i.e. only integer part*/
67
-
moonAge = moonCycles - completedMoonCycles;/* subtract integer part to leave fractional part which represents the current moon age*/
68
-
phase = moonAge * 8 + 0.5;/* scale fraction from 0-8 and round by adding 0.5*/
69
-
phase = phase & 7; /* 0 and 8 are the same so turn 8 into 0*/
71
+
yearAsDays = 365.25 * year;// 365.25 -> mean length of a calendar year
72
+
monthAsDays = 30.6 * month;// 30.6 -> mean length of a month
73
+
daysSinceReferenceNewMoon = yearAsDays + monthAsDays + day - 694039.09; // number of days since known new moon on 1900-01-01, 694039.09 -> days elapsed since zero
74
+
moonCycles = daysSinceReferenceNewMoon / 29.53;// 29.53 -> long-term average moon cycle duration in days
75
+
completedMoonCycles = moonCycles;// "casting" to int to get *completed* moon cycles i.e. only integer part
76
+
moonAge = moonCycles - completedMoonCycles;// subtract integer part to leave fractional part which represents the current moon age
77
+
phase = moonAge * 8 + 0.5;// scale fraction from 0-8 and round by adding 0.5
78
+
phase = phase & 7; // 0 and 8 are the same so turn 8 into 0
0 commit comments