Home » Source Code » Calculate and Draw Moon Phase

Calculate and Draw Moon Phase

maninwest
2015-02-08 03:28:30
The author
View(s):
Download(s): 2
Point (s): 1 
Category Category:
WinXPWinXP VBScriptVBScript

Description

Translated by maninwest@Codeforge Author:Mostafa Kaisoun@CodeProject
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 addPictureBoxcontrol 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).
TheMoonPhaseproject has one form (frmMoon) with the following controls:
  • MonthCalendarcontrol (MyCalendar)
  • Buttoncontrol (btnToDay)
  • Buttoncontrol (btnClose)
  • PictureBoxcontrol (PicMoon)
  • Labelcontrol (lblAge)
About the Code

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.


Mostafa Kaisoun
m_kaisoun@hotmail.com
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
AssemblyInfo.vb1.02 kB2010-08-07 18:49
MoonPhase.exe19.50 kB2010-08-08 08:21
MoonPhase.pdb27.50 kB2010-08-08 08:21
frmMoon.resx10.85 kB2010-08-07 19:46
frmMoon.vb9.98 kB2010-08-07 19:46
icnMoon.ICO1.05 kB1998-04-23 18:00
MoonPhase.vbproj4.12 kB2010-08-07 19:39
MoonPhase.vbproj.user1.77 kB2010-08-08 08:21
MoonPhase.sln913.00 B2010-08-07 18:49
...
Sponsored links

Comments

(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
  • 1
  • Page 1
  • Total 1

Calculate and Draw Moon Phase (19.70 kB)(15.98 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D