Moonphase is a versatile collection of implementations to determine the current phase of the moon. With support for multiple programming languages including C/C++, Rust, JavaScript, and Python, users can easily integrate moon phase calculations into their projects using straightforward functions and test cases.
The moonphase project offers a collection of implementations designed to determine the current phase of the moon. It supports a range of programming languages, including:
These implementations process a timestamp (typically in Unix epoch seconds) to calculate the "age" of the moon in radians. By applying the formula (1-cos(x))/2, it returns the illuminated fraction of the moon's surface. This approach is necessary because the same illuminated percentage recurs multiple times throughout the lunar cycle, particularly around the first and third quarters.
Using this framework, users can easily convert the moon's age into a range between [0,1], allowing for daily calculations with approximately a 29.5-day cycle. Additionally, the project includes functionalities to compute the phase index, which is necessary for identifying the corresponding phase name and emoji.
A noteworthy example can be seen in the following Rust code snippet:
fn phaseidx(ilumfrac: f64, ang: time::Angle) -> usize {
match (ilumfrac, ang.degrees() > 90.0) {
(0.00..0.04, _) => 0,
(0.96..1.00, _) => 4,
(0.46..0.54, true) => 6,
(0.46..0.54, false) => 2,
(0.54..0.96, true) => 5,
(0.54..0.96, false) => 3,
(_, true) => 7,
(_, false) => 1,
}
}
(This function requires the angle to be adjusted to a positive value using a modulo operation, such as a-360.0*(floor(a/360)).)
All implementations are accompanied by comprehensive test cases, demonstrating the methods for obtaining both the illuminated fraction and percentage based on the code. The algorithms in this project are derived from the methodology found in moontool, a historical GUI application created in the 1980s by John Walker, and inspired by the book Practical Astronomy With Your Calculator.
To maintain clarity and functionality, all code submissions must adhere to the following rule:
No comments yet.
Sign in to be the first to comment.