Hi,
does anyone has a Lua Library I could implement into my App
that would give me the option to know Moon Phase for a specific Day
Thanks
Chris
Hi,
does anyone has a Lua Library I could implement into my App
that would give me the option to know Moon Phase for a specific Day
Thanks
Chris
I don’t know of a Lua source off of the top of my head, but after a quick google search, here is some C code:
int moon\_phase(int y, int m, int d) { /\* calculates the moon phase (0-7), accurate to 1 segment. 0 = \> new moon. 4 =\> full moon. \*/ int c,e; double jd; int b; if (m \< 3) { y--; m += 12; } ++m; c = 365.25\*y; e = 30.6\*m; jd = c+e+d-694039.09; /\* jd is total days elapsed \*/ jd /= 29.53; /\* divide by the moon cycle (29.53 days) \*/ b = jd; /\* int(jd) -\> b, take integer part of jd \*/ jd -= b; /\* subtract integer part to leave fractional part of original jd \*/ b = jd\*8 + 0.5; /\* scale fraction from 0-8 and round by adding 0.5 \*/ b = b & 7; /\* 0 and 8 are the same so turn 8 into 0 \*/ return b; }
Rob
thanks so much, i tried as bellow, but my C knowledge is BAD and I guess there is not just one bug in my converting to lua 
function moon_phase(y, m, d)
--[[
calculates the moon phase (0-7), accurate to 1 segment.
0 = > new moon.
4 => full moon.
--]]
local c = 0;
local e = 0;
local jd = 0;
local b = 0;
if (m < 3) then
y=y-1;
m=m+ 12;
end
m = m+1; – ++m;
c = 365.25*y;
e = 30.6*m;
jd = c+e+d-694039.09; – jd is total days elapsed */
jd = jd / 29.53; – divide by the moon cycle (29.53 days) */
b = jd; – int(jd) -> b, take integer part of jd */
jd = jd- b; – subtract integer part to leave fractional part of original jd */
b = jd*8 + 0.5; – scale fraction from 0-8 and round by adding 0.5 */
– b = b & 7; – 0 and 8 are the same so turn 8 into 0 */
if b == 8 then b = 0 end
return b;
end
i guess special the ++m is wrong and b = b & 7
any bug fix is welcomed 
Well the ++m means take the value of “m” and add one to it. It’s the same as m = m + 1
The b & 7 is masking off larger numbers. Basically keeping number in a range of 0 - 7. You probably would do the same thing with a modulus operator: b = b % 8
Rob
hi and thanks
i got it.
anyhow its not 100% accurate… i did some tests and compared with other lunar apps.
… also its a start… thanks
if someone has a more accurate source (best in lua
its welcome
greets
chris
How is it not accurate?
Rob
the moon phase calculation itself, when i go for example to
http://www.moonconnection.com/moon_phases_calendar.phtml
or http://stardate.org/nightsky/moon
and look for example 2009 and to 2019 …
the full moon, new moon is in some month 1-2 days off
with above code, compared to the online links
i had to do already jd = c+e+d-694039.09-2.4
to come more near
here my actual code
function moon_phase(y, m, d)
--[[
calculates the moon phase (0-7), accurate to 1 segment.
0 = > new moon.
4 => full moon.
--]]
local c = 0;
local e = 0;
local jd = 0;
local b = 0;
if (m < 3) then
y=y-1;
m=m+ 12;
end
m = m+1; – ++m;
c = 365.25*y;
e = 30.6*m;
jd = c+e+d-694039.09-2.45; – jd is total days elapsed */
jd = jd / 29.53; – divide by the moon cycle (29.53 days) */
b,jd = math.modf(jd)
b = jd*8 + 0.5; – scale fraction from 0-8 and round by adding 0.5 */
if b > 8 then b = b-8 end
return b;
end
compared to other sources like: – http://www.codeproject.com/Articles/100174/Calculate-and-Draw-Moon-Phase
ur code is much shorts (and for that he does a great job 
I found this to be more accurate. It’s modified from VB code I found here:
http://www.codeproject.com/KB/graphics/MoonPhase/Moon_VB.zip
local centerX = display.contentCenterX local centerY = display.contentCenterY local moonText = display.newText("Moon phase.",centerX, centerY, nil, 40) moonText:setFillColor(1,0,0) local function julianDate(d, m, y) local mm, yy, k1, k2, k3, j yy = y - math.floor((12 - m) / 10) mm = m + 9 if (mm \>= 12) then mm = mm - 12 end k1 = math.floor(365.25 \* (yy + 4712)) k2 = math.floor(30.6001 \* mm + 0.5) k3 = math.floor(math.floor((yy / 100) + 49) \* 0.75) - 38 j = k1 + k2 + d + 59 if (j \> 2299160) then j = j - k3 end return j end local function moonAge(d, m, y) local j, ip, ag j = julianDate(d, m, y) ip = (j + 4.867) / 29.53059 ip = ip - math.floor(ip) if (ip \< 0.5) then ag = ip \* 29.53059 + 29.53059 / 2 else ag = ip \* 29.53059 - 29.53059 / 2 end print(ag) return ag end local theMoon = moonAge(22,02,2016) print(theMoon) if theMoon \>= 29 then moonText.text = "New Moon" elseif theMoon \< 29 and theMoon \> 23 then moonText.text = "Waning Crescent" elseif theMoon \< 23 and theMoon \> 22 then moonText.text = "Last Quarter" elseif theMoon \< 22 and theMoon \> 15 then moonText.text = "Waning gibbous" elseif theMoon \< 15 and theMoon \> 13 then moonText.text = "Full Moon" elseif theMoon \< 13 and theMoon \> 8 then moonText.text = "Waxing gibbous" elseif theMoon \< 8 and theMoon \> 6 then moonText.text = "First Moon" elseif theMoon \< 6 and theMoon \> 1 then moonText.text = "Waxing crescent" else moonText.text = "New Moon" end
that looks great 
i guess its from here: http://www.codeproject.com/Articles/100174/Calculate-and-Draw-Moon-Phase
could u also give the lua for the last part of that source DRAWMOON in LUA Corona working way.
i would need than also that drawing the moon part, but they work lots with POINTS… and i dont see that in corona.
Maybe a Corona Stuff Member can help to translate 
Thanks a lot again
Chris
I’d be glad to take a look at the draw moon source as well. Ultimately, I think you’re best bet would be to just swap in images of the different phases. I’ll provide an example.
Here is the project. I think the calculations could be tweaked more to get better results, but this seems to work pretty well as it is.
wohhhh, thats great…!!!
Thank you so much !
… really thank you.
just for ‘interest’. if someone would know how to make that moonphases by painting, not gfx, i would be really interested.
its more a techi interest how to do. Im not so good in that sinus thing how to make the curve etc. so it would be a real
study code 
Wish you all the best
greets
chris
@quruk, you don’t need graphics to pull off what you are wanting, simply use the above code from @James and draw two circles one with color a and one with a dark color then the one with the dark color just offset the X position by MINUS/PLUS X which will result in a moon phase.
If you want it to look slicker than that use the “dark side” as a mask and use a single “round moon image” 
Easy/Peasy 
What a clever Idea… !! For sure, thats a way I did not thought about, also its logic. I will check it out.
Yes, really easy peasy.
And still
if there is a algorithm Geek, i know for a math freak it would be just 2-3 Lines of code to make that happen.
And I was always fascinated what can be done by math and a bit logic.
… in another way here I point also on some Corona Features. If we as Devs would share some cool Libs of Routines , the Corona
Devs could save some work with 2d Functions developing and use some time for things we really cant realize, like compiling for OSX :)
… so if there is a math geek, please share your wisdom./smile/
and @Christoph… again thanks… you made a very interesting point.
Greetsy/Peatsy
Chris
Do you mean some math to create a half moon/ quarter moon etc. by math?
yes 
and than with the painting possibilities we have in corona.
maybe with a display.newRoundedRect() or anything else thats faster than painting point by point.
but if thats the simplest way and at the end also not too slow… yeah… why not.
as said, im just interested how that would be solved by a algorithm (sinus etc) without using a predefined gfx used as complete or moved around in parts.
I have to go to a dinner but will whip something up for you in the morning it is pretty simple you can actually just do it drawing lines via angles etc. I do something similar with my charts :) (pie charts that is)
@Christopher and @guruk,
I’m curious. From this conversation, four options have been discussed regarding the moon phase graphics.
1.) pre-made gfx
2.) overlaying shapes
3.) shapes as masks
4.) using math
What are the advantages/disadvantages to these four approaches? If anyone else has input, please chime in.
good question 
most simple. but limited as premade gfx, needs more memory
interesting concept , allows less gfx and quiet good ‘faking’ 
even better faking… just overlaying shapes would need cutting of the surrounding.
finest work. if well done, less memory. needs a clever head (not me 
thats in short my answers on that. for sure the other coders may have better explanations.
have a good time
chris
I don’t know of a Lua source off of the top of my head, but after a quick google search, here is some C code:
int moon\_phase(int y, int m, int d) { /\* calculates the moon phase (0-7), accurate to 1 segment. 0 = \> new moon. 4 =\> full moon. \*/ int c,e; double jd; int b; if (m \< 3) { y--; m += 12; } ++m; c = 365.25\*y; e = 30.6\*m; jd = c+e+d-694039.09; /\* jd is total days elapsed \*/ jd /= 29.53; /\* divide by the moon cycle (29.53 days) \*/ b = jd; /\* int(jd) -\> b, take integer part of jd \*/ jd -= b; /\* subtract integer part to leave fractional part of original jd \*/ b = jd\*8 + 0.5; /\* scale fraction from 0-8 and round by adding 0.5 \*/ b = b & 7; /\* 0 and 8 are the same so turn 8 into 0 \*/ return b; }
Rob