-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoonPhase.cpp
90 lines (57 loc) · 1.6 KB
/
MoonPhase.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
MoonPhase.h - A Arduino library to get the moon's current phase
Copyright (c) 2020 Ashley Sheaff. All right reserved.
*/
// include this library's description file
#include <math.h>
#include "MoonPhase.h"
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
MoonPhase::MoonPhase()
{
}
// Public Methods //////////////////////////////////////////////////////////////
float MoonPhase::GetPhase(int nYear, int nMonth, int nDay) // calculate the current phase of the moon
{
float phase;
double AG, IP;
long YY, MM, K1, K2, K3, JD;
YY = nYear - floor((12 - nMonth) / 10);
MM = nMonth + 9;
if (MM >= 12)
{
MM = MM - 12;
}
K1 = floor(365.25 * (YY + 4712));
K2 = floor(30.6 * MM + 0.5);
K3 = floor(floor((YY / 100) + 49) * 0.75) - 38;
JD = K1 + K2 + nDay + 59;
if (JD > 2299160)
{
JD = JD - K3;
}
IP = MyNormalize((JD - 2451550.1) / 29.530588853);
return IP;
}
int MoonPhase::GetType(float phaseval){
if(phaseval < 0.5){
//waxing
return 1;
} else if(phaseval > 0.5){
return 2; // waning
}
}
int MoonPhase::GetPercentage(float phaseval){
if(phaseval < 0.5){
return (phaseval-0.0)/(0.5-0.0) * 100; //(value-min)/(max-min)
} else if(phaseval > 0.5){
return (1.0-((phaseval-0.5)/(1.0-0.5))) * 100; //(value-min)/(max-min) // flip for waning
}
}
double MoonPhase::MyNormalize(double v)
{
v = v - floor(v);
if (v < 0)
v = v + 1;
return v;
}