Created
October 16, 2017 18:21
-
-
Save adrianstevens/776530e198734b34a9c8a43aaf880041 to your computer and use it in GitHub Desktop.
MoonPhase C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MoonPhase | |
{ | |
public class MoonPhase | |
{ | |
static double MoonCycleLength = 29.53059; | |
public int GetJulianDate(int day, int month, int year) | |
{ | |
year = year - (12 - month) / 10; | |
month = month + 9; | |
if (month >= 12) | |
month = month - 12; | |
var k1 = (int)(365.25 * (year + 4712)); | |
var k2 = (int)(30.6001 * month + 0.5); | |
// 'j' for dates in Julian calendar: | |
var julianDate = k1 + k2 + day + 59; | |
//Gregorian calendar | |
if (julianDate > 2299160) | |
{ | |
var k3 = (int)((year / 100 + 49) * 0.75) - 38; | |
julianDate = julianDate - k3; //at 12h UT (Universal Time) | |
} | |
return julianDate; | |
} | |
public double GetMoonAge(int day, int month, int year) | |
{ | |
double ip, age; | |
int julianDate = GetJulianDate(day, month, year); | |
ip = (julianDate + 4.867) / 29.53059; | |
ip = ip - Math.Floor(ip); | |
age = ip * MoonCycleLength + MoonCycleLength / 2; | |
if (age > MoonCycleLength) | |
age -= MoonCycleLength; | |
return age; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment