## ProblemYou want to calculate the phase of the moon for a given date. ## SolutionSample code folder: Chapter 07\MoonPhase Use Visual Basic 2005's ## DiscussionThe linear curve fit equation presented here was researched and created only recently, using data from the Internet that provided the date and time of all new moons over a period of several centuries. The results are surprisingly accurate, and the equation is easy to use, especially with the helpful math and date functions available in Visual Basic. Here's the Public Function MoonPhase(ByVal dateUtc As Date) As Double ' ----- Determine the phase of the moon for any date. Dim days As Double = dateUtc.Subtract(#1/1/1600#).TotalDays Dim cycles As Double = days * 0.03386319 - 12.5 Return Math.IEEERemainder(cycles, 1.0) * 29.53059 End Function The date for determining the moon's phase is passed to this function as The heart of this algorithm is in the second line of the function. The number of elapsed The following code reports the closest new moon using the Dim phaseDay As Double Dim result As String ' ----- Determine the phase of the moon. phaseDay = MoonPhase(Now.ToUniversalTime) ' ----- Show the nearest new moon. result = "UTC is now: " & _ Now.ToUniversalTime.ToString("u") & vbNewLine & vbNewLine If (phaseDay < 0) Then result &= "Approx days until new moon: " & _ (-phaseDay).ToString("F1") Else result &= "Approx days since new moon: " & _ phaseDay.ToString("F1") End If MsgBox(result) This code converts the current local time to UTC using the ## Figure 7-20. The moon is waxing and it's about ¼ lit by the sunAfter running a curve fit program to compute the equation used earlier, a second program was written to find the maximum absolute error in time for all new moons in the range of years from 1600 to 2500. Surprisingly, the maximum drift of the time of new moon was less than half an hour. This equation, even though it's a simple one, is good enough to allow you to predict when you'll be able to fish, plant, and dance by the light of the moon. |

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

ISBN: 0596101775

EAN: 2147483647

EAN: 2147483647

Year: 2006

Pages: 400

Pages: 400

Authors: Tim Patrick, John Craig

Similar book on Amazon

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net