11,716,249 members (86,767 online)

# Calculate and Draw Moon Phase

, 12 Aug 2010 CPOL 109.5K 2.1K 37
 Rate this:
Calculate Moon's age and draw Moon Phase on any given day

## Introduction

Many communities pay close attention to the lunar calendar next to the solar calendar, so I looked at many sites on the internet to know how to calculate the age of the moon on any given day. I found many sites offering different ways, I took what I found to give results closer to the truth.

I've noticed that most sites agree on the expense of the Julian date but do not agree to calculate the age of the moon, and found the difference between these sites up to one day, and when the moon's age is 30 days, the result is zero in some sites.

In this program, I calculated the approximate age of the moon in days and did not give attention to the parts of the day of the hours and minutes.

In order for the program would be more useful, I add `PictureBox `control to display the lighted part of the moon and darkness part of the moon commensurate with the age of the moon.

## Background

I created two projects, I wrote code of one in C# (2003) and wrote another in VB.NET (2003).

The `MoonPhase `project has one form (`frmMoon`) with the following controls:

• `MonthCalendar `control (`MyCalendar`)
• `Button `control (`btnToDay`)
• `Button `control (`btnClose`)
• `PictureBox `control (`PicMoon`)
• `Label `control (`lblAge`)

Convert date to Julian date:

```private int JulianDate(int d, int m, int y)
{
int mm, yy;
int k1, k2, k3;
int j;

yy = y - (int)((12 - m) / 10);
mm = m + 9;
if (mm >= 12)
{
mm = mm - 12;
}
k1 = (int)(365.25 * (yy + 4712));
k2 = (int)(30.6001 * mm + 0.5);
k3 = (int)((int)((yy / 100) + 49) * 0.75) - 38;
// 'j' for dates in Julian calendar:
j = k1 + k2 + d + 59;
if (j > 2299160)
{
// For Gregorian calendar:
j = j - k3; // 'j' is the Julian date at 12h UT (Universal Time)
}
return j;
}```

Calculate the approximate moon's age in days:

```private double MoonAge(int d, int m, int y)
{
int j = JulianDate(d, m, y);
//Calculate the approximate phase of the moon
ip = (j + 4.867) / 29.53059;
ip = ip - Math.Floor(ip);
//After several trials I've seen to add the following lines,
//which gave the result was not bad
if(ip < 0.5)
ag = ip * 29.53059 + 29.53059 / 2;
else
ag = ip * 29.53059 - 29.53059 / 2;
// Moon's age in days
ag = Math.Floor(ag) + 1;
return ag;
}```

Draw moon:

```private void DrawMoon()
{
int Xpos, Ypos, Rpos;
int Xpos1, Xpos2;
double Phase;

Phase = ip;
// Width of 'ImageToDraw' Object = Width of 'PicMoon' control
int PageWidth = PicMoon.Width;
// Height of 'ImageToDraw' Object = Height of 'PicMoon' control
int PageHeight = PicMoon.Height;
// Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control
Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight);
// Create graphics object for alteration.
Graphics newGraphics = Graphics.FromImage(ImageToDraw);

Pen PenB = new Pen(Color.Black); // For darkness part of the moon
Pen PenW = new Pen(Color.White); // For the lighted part of the moon

for (Ypos=0; Ypos<= 45; Ypos++)
{
Xpos = (int)(Math.Sqrt(45*45 - Ypos*Ypos));
// Draw darkness part of the moon
Point pB1 = new Point(90-Xpos, Ypos+90);
Point pB2 = new Point(Xpos+90, Ypos+90);
Point pB3 = new Point(90-Xpos, 90-Ypos);
Point pB4 = new Point(Xpos+90, 90-Ypos);
newGraphics.DrawLine(PenB, pB1, pB2);
newGraphics.DrawLine(PenB, pB3, pB4);
// Determine the edges of the lighted part of the moon
Rpos = 2 * Xpos;
if (Phase < 0.5)
{
Xpos1 = - Xpos;
Xpos2 = (int)(Rpos - 2*Phase*Rpos - Xpos);
}
else
{
Xpos1 = Xpos;
Xpos2 = (int)(Xpos - 2*Phase*Rpos + Rpos);
}
// Draw the lighted part of the moon
Point pW1 = new Point(Xpos1+90, 90-Ypos);
Point pW2 = new Point(Xpos2+90, 90-Ypos);
Point pW3 = new Point(Xpos1+90, Ypos+90);
Point pW4 = new Point(Xpos2+90, Ypos+90);
newGraphics.DrawLine(PenW, pW1, pW2);
newGraphics.DrawLine(PenW, pW3, pW4);
}
// Display the bitmap in the picture box.
PicMoon.Image = ImageToDraw;
// Release graphics object
PenB.Dispose();
PenW.Dispose();
newGraphics.Dispose();
ImageToDraw = null;
} ```

You can go back to the source file to read the VB.NET code after you extract Moon_VB.zip file.

## Final Words

If you have any idea or find another code to calculate the age of the moon, please tell me. Thanks to Code Project and thanks to all.

Mostafa Kaisoun
m_kaisoun@hotmail.com

## Share

 Egypt
No Biography provided

## You may also be interested in...

 First Prev Next
 Add more feature Christianirwan26-Jun-13 0:39 Christianirwan 26-Jun-13 0:39
 Re: Add more feature Mostafa Kaisoun26-Jun-13 3:02 Mostafa Kaisoun 26-Jun-13 3:02
 Moon phase Edward Omowa16-Apr-13 11:29 Edward Omowa 16-Apr-13 11:29
 Re: Moon phase Mostafa Kaisoun16-Apr-13 14:43 Mostafa Kaisoun 16-Apr-13 14:43
 Re: Moon phase t9mike28-Mar-15 15:44 t9mike 28-Mar-15 15:44
 frmoon Edward Omowa16-Apr-13 11:23 Edward Omowa 16-Apr-13 11:23
 My vote of 5 DanielSheets5-Mar-13 7:31 DanielSheets 5-Mar-13 7:31
 Re: My vote of 5 Mostafa Kaisoun6-Mar-13 5:40 Mostafa Kaisoun 6-Mar-13 5:40
 My vote of 5 manoj kumar choubey1-Mar-12 22:18 manoj kumar choubey 1-Mar-12 22:18
 Error, and another algorithm Robert Baker II7-Sep-11 5:50 Robert Baker II 7-Sep-11 5:50
 Re: Error, and another algorithm Robert Baker II7-Sep-11 5:55 Robert Baker II 7-Sep-11 5:55
 Re: Error, and another algorithm Mostafa Kaisoun7-Sep-11 9:03 Mostafa Kaisoun 7-Sep-11 9:03
 Draw Moon Phase Member 799874712-Jun-11 10:02 Member 7998747 12-Jun-11 10:02
 Re: Draw Moon Phase Mostafa Kaisoun13-Jun-11 14:24 Mostafa Kaisoun 13-Jun-11 14:24
 My vote of 5 dim13123-Dec-10 4:38 dim131 23-Dec-10 4:38
 Good job - Thanks for sharing... Steve Hageman16-Aug-10 19:11 Steve Hageman 16-Aug-10 19:11
 Re: Good job - Thanks for sharing... Mostafa Kaisoun17-Aug-10 8:34 Mostafa Kaisoun 17-Aug-10 8:34
 Good article dwilliss10-Aug-10 15:50 dwilliss 10-Aug-10 15:50
 Re: Good article Mostafa Kaisoun11-Aug-10 3:58 Mostafa Kaisoun 11-Aug-10 3:58
 My vote of 5 myker10-Aug-10 4:53 myker 10-Aug-10 4:53
 Re: My vote of 5 Mostafa Kaisoun11-Aug-10 3:55 Mostafa Kaisoun 11-Aug-10 3:55
 not Bad, Keep going Mostafa... Omar Amin Ibrahim9-Aug-10 4:11 Omar Amin Ibrahim 9-Aug-10 4:11
 Re: not Bad, Keep going Mostafa... Mostafa Kaisoun9-Aug-10 4:48 Mostafa Kaisoun 9-Aug-10 4:48
 code not available Christ Kennedy8-Aug-10 11:33 Christ Kennedy 8-Aug-10 11:33
 Re: code not available Mostafa Kaisoun8-Aug-10 15:33 Mostafa Kaisoun 8-Aug-10 15:33
 Re: code not available Christ Kennedy8-Aug-10 16:24 Christ Kennedy 8-Aug-10 16:24
 Re: code not available Mostafa Kaisoun9-Aug-10 3:03 Mostafa Kaisoun 9-Aug-10 3:03
 Re: code not available Christ Kennedy9-Aug-10 3:49 Christ Kennedy 9-Aug-10 3:49
 Re: code not available Mostafa Kaisoun9-Aug-10 4:43 Mostafa Kaisoun 9-Aug-10 4:43
 Re: code not available Christ Kennedy9-Aug-10 5:19 Christ Kennedy 9-Aug-10 5:19
 Re: code not available Pete Goodsall11-Aug-10 5:42 Pete Goodsall 11-Aug-10 5:42
 Re: code not available Mostafa Kaisoun12-Aug-10 3:10 Mostafa Kaisoun 12-Aug-10 3:10
 code formatting Christ Kennedy8-Aug-10 11:32 Christ Kennedy 8-Aug-10 11:32
 Re: code formatting Mostafa Kaisoun8-Aug-10 15:35 Mostafa Kaisoun 8-Aug-10 15:35
 My vote of 2 Toli Cuturicu8-Aug-10 3:29 Toli Cuturicu 8-Aug-10 3:29
 Re: My vote of 2 Mostafa Kaisoun8-Aug-10 15:41 Mostafa Kaisoun 8-Aug-10 15:41
 Re: My vote of 2 Digital Thunder20-Aug-10 22:27 Digital Thunder 20-Aug-10 22:27
 Re: My vote of 2 Mostafa Kaisoun21-Aug-10 13:02 Mostafa Kaisoun 21-Aug-10 13:02
 Re: My vote of 2 Digital Thunder21-Aug-10 21:02 Digital Thunder 21-Aug-10 21:02
 Re: My vote of 2 Mostafa Kaisoun22-Aug-10 13:09 Mostafa Kaisoun 22-Aug-10 13:09
 Last Visit: 31-Dec-99 18:00     Last Update: 2-Sep-15 23:05 Refresh 1