Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » GDI+ & Graphics » Lunar calendar in C#

Lunar calendar in C#

In this article I will show how to create a lunar calendar in C#.

Page Views : 3269
Downloads : 112
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MoonPhase.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


1.gif

Introduction

Lunar calendar is very important for Muslims such as the importance of solar calendar because lunar calendar was related to some elements of worship, so I looked at many sites on the internet to know how to calculate the age of the moon in any given day, I found many sites offer 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 don't 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 calculate 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.

I create a project under C# (2003) , the MoonPhase project has one form (frmMoon) with following controls:
  • MonthCalendar control (MyCalendar).
  • Button control (btnToDay).
  • Button control (btnClose).
  • PictureBox control (PicMoon).
  • Label control (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 to source file to read the code, if you have any idea or find another code to calculate moon age, please tell me. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mostafa Kaisoun
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.