The Fork
http://the.fork.pl/code/moon/
 the.fork.pl
 Site
 Credits
 Contact
 Powered by
 The Story of Fork
 Words
 Sermon on the Hill
 Antybiografia
 Angel
 Devil
 Powrót z...
 Simon T.
 Favourite poems
 Roam
 Sounds
 The Lost Faith
 Atari Reloaded
 Players
 Code
 Saker
 Moon phase
 KB24
 Attic
 PLC
 33doi
 DemoScene
 Atari
 Logrus
 Cryogen
 Crew
 History
 Stuph
 Pictures
 Ascii Art
 Set One
 Set Two
 Set Three

Moon Phase

When the sky above is cloudy or when the steel bars in your window don't let you look outside... The moon is always there - the cold mirror of nations' dreams.

Catch it.

Moon image, please wait while generating...


C code for moonphase calculation, author unknown.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. //#define TRUE 1
  6. //#define FALSE 0
  7. /* Date for which to calculate phase *//* Illuminated fraction *//* Age of moon in days *//* Distance in kilometres *//* Angular diameter in degrees *//* Distance to Sun *//* Sun's angular diameter *//*  Astronomical constants  */
  8.  
  9. #define epoch       2444238.5      /* 1980 January 0.0 */
  10.  
  11. /*  Constants defining the Sun's apparent orbit  */
  12.  
  13. #define elonge      278.833540     /* Ecliptic longitude of the Sun
  14.                                       at epoch 1980.0 */
  15. #define elongp      282.596403     /* Ecliptic longitude of the Sun at
  16.                                       perigee */
  17. #define eccent      0.016718       /* Eccentricity of Earth's orbit */
  18. #define sunsmax     1.495985e8     /* Semi-major axis of Earth's orbit, km */
  19. #define sunangsiz   0.533128       /* Sun's angular size, degrees, at
  20.                                       semi-major axis distance */
  21.  
  22. /*  Elements of the Moon's orbit, epoch 1980.0  */
  23.  
  24. #define mmlong      64.975464      /* Moon's mean longitude at the epoch */
  25. #define mmlongp     349.383063     /* Mean longitude of the perigee at the
  26.                                       epoch */
  27. #define mlnode      151.950429     /* Mean longitude of the node at the
  28.                                       epoch */
  29. #define minc        5.145396       /* Inclination of the Moon's orbit */
  30. #define mecc        0.054900       /* Eccentricity of the Moon's orbit */
  31. #define mangsiz     0.5181         /* Moon's angular size at distance a
  32.                                       from Earth */
  33. #define msmax       384401.0       /* Semi-major axis of Moon's orbit in km */
  34. #define mparallax   0.9507         /* Parallax at distance a from Earth */
  35. #define synmonth    29.53058868    /* Synodic month (new Moon to new Moon) */
  36. #define lunatbase   2423436.0      /* Base date for E. W. Brown's numbered
  37.                                       series of lunations (1923 January 16) */
  38.  
  39. /*  Properties of the Earth  */
  40.  
  41. #define earthrad    6378.16        /* Radius of Earth in kilometres */
  42.  
  43.  
  44. #define PI 3.14159265358979323846  /* Assume not near black hole nor in
  45.                                       Tennessee */
  46.  
  47. /*  Handy mathematical functions  */
  48.  
  49. #define sgn(x) (((x) < 0) ? -1 : ((x) > 0 ? 1 : 0))       /* Extract sign */
  50. //#define abs(x) ((x) < 0 ? (-(x)) : (x))                   /* Absolute val */
  51. #define fixangle(a) ((a) - 360.0 * (floor((a) / 360.0)))  /* Fix angle    */
  52. #define torad(d) ((d) * (PI / 180.0))                     /* Deg->Rad     */
  53. #define todeg(d) ((d) * (180.0 / PI))                     /* Rad->Deg     */
  54. #define dsin(x) (sin(torad((x))))                         /* Sin from deg */
  55. #define dcos(x) (cos(torad((x))))                         /* Cos from deg */
  56. //    int mm, dd;
  57. "time=%d\n""jtime=%f\n",jd);
  58. //    mm = gm->tm_mon + 1;
  59. //    dd = gm->tm_mday;
  60. //    drawmoon(hDC, "colourmoon", 64, 32, 27, 28,
  61. //             tm.tmMaxCharWidth * mgeom.moonbitx, 10, p, mm, dd);
  62.  
  63.     /* Update textual information for open window */
  64.  
  65.         /* Local time */"Local time      %02d-%02d-%4d  %02d:%02d:%02d\n""Moon age        %d day, %02d:%02d\n""Moon phase      %.0f%%\n""Moon dist.      %.0f km    %.2f e.rd.\n""Sun dist.       %.0f km    %.2f au\n"/* JDATE -- Convert internal GMT date and time to Julian day and fraction. *//* Compute century *//*  JTIME  --  Convert internal GMT  date  and  time  to  astronomical
  66.                Julian   time  (i.e. Julian  date  plus  day  fraction,
  67.                expressed as a double).  */"jdate=%d\n"/*  JYEAR  --  Convert  Julian  date  to  year,  month, day, which are
  68.                returned via integer pointers to integers.  *//* Astronomical to civil *//*  JHMS  --  Convert Julian time to hour, minutes, and seconds.  *//* Astronomical to civil *//*  KEPLER  --   Solve the equation of Kepler.  */#define EPSILON 1E-6
  69. /*  PHASE  --  Calculate phase of moon as a fraction:
  70.  
  71.     The  argument  is  the  time  for  which  the  phase is requested,
  72.     expressed as a Julian date and fraction.  Returns  the  terminator
  73.     phase  angle  as a percentage of a full circle (i.e., 0 to 1), and
  74.     stores into pointer arguments  the  illuminated  fraction  of  the
  75.     Moon's  disc, the Moon's age in days and fraction, the distance of
  76.     the Moon from the centre of the Earth, and  the  angular  diameter
  77.     subtended  by the Moon as seen by an observer at the centre of the
  78.     Earth.
  79. *//* Date for which to calculate phase *//* Illuminated fraction *//* Age of moon in days *//* Distance in kilometres *//* Angular diameter in degrees *//* Distance to Sun *//* Sun's angular diameter *//* MN,*/ Ev, Ae, A3, MmP,
  80.            mEc, A4, lP, V, lPP, // NP, y, x, Lambdamoon, BetaM,
  81.            MoonAge, MoonPhase,
  82.            MoonDist, MoonDFrac, MoonAng, // MoonPar,
  83.            F, SunDist, SunAng;
  84.  
  85.     /* Calculation of the Sun's position */
  86.  
  87.     Day = pdate - epoch;                    /* Date within epoch */
  88.     N = fixangle((360 / 365.2422) * Day);   /* Mean anomaly of the Sun *//* Convert from perigee
  89.                                                co-ordinates to epoch 1980.0 */
  90.     Ec = kepler(M, eccent);                 /* Solve equation of Kepler */
  91.     Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
  92.     Ec = 2 * todeg(atan(Ec));               /* True anomaly *//* Sun's geocentric ecliptic
  93.                                                longitude */
  94.     /* Orbital distance factor */
  95.     F = ((1 + eccent * cos(torad(Ec))) / (1 - eccent * eccent));
  96.     SunDist = sunsmax / F;                  /* Distance to Sun in km */
  97.     SunAng = F * sunangsiz;                 /* Sun's angular size in degrees */
  98.  
  99.     /* Calculation of the Moon's position */
  100.  
  101.     /* Moon's mean longitude *//* Moon's mean anomaly *//* Moon's ascending node mean longitude */
  102. //    MN = fixangle(mlnode - 0.0529539 * Day);
  103.  
  104.     /* Evection */
  105.     Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
  106.  
  107.     /* Annual equation */
  108.     Ae = 0.1858 * sin(torad(M));
  109.  
  110.     /* Correction term */
  111.     A3 = 0.37 * sin(torad(M));
  112.  
  113.     /* Corrected anomaly */
  114.     MmP = MM + Ev - Ae - A3;
  115.  
  116.     /* Correction for the equation of the centre */
  117.     mEc = 6.2886 * sin(torad(MmP));
  118.  
  119.     /* Another correction term */
  120.     A4 = 0.214 * sin(torad(2 * MmP));
  121.  
  122.     /* Corrected longitude */
  123.     lP = ml + Ev + mEc - Ae + A4;
  124.  
  125.     /* Variation */
  126.     V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
  127.  
  128.     /* True longitude */
  129.     lPP = lP + V;
  130.  
  131.     /* Corrected longitude of the node */
  132. //    NP = MN - 0.16 * sin(torad(M));
  133.  
  134.     /* Y inclination coordinate */
  135. //    y = sin(torad(lPP - NP)) * cos(torad(minc));
  136.  
  137.     /* X inclination coordinate */
  138. //    x = cos(torad(lPP - NP));
  139.  
  140.     /* Ecliptic longitude */
  141. //    Lambdamoon = todeg(atan2(y, x));
  142. //    Lambdamoon += NP;
  143.  
  144.     /* Ecliptic latitude */
  145. //    BetaM = todeg(asin(sin(torad(lPP - NP)) * sin(torad(minc))));
  146.  
  147.     /* Calculation of the phase of the Moon */
  148.  
  149.     /* Age of the Moon in degrees */
  150.     MoonAge = lPP - Lambdasun;
  151.  
  152.     /* Phase of the Moon */
  153.     MoonPhase = (1 - cos(torad(MoonAge))) / 2;
  154.  
  155.     /* Calculate distance of moon from the centre of the Earth */
  156.  
  157.     MoonDist = (msmax * (1 - mecc * mecc)) /
  158.                (1 + mecc * cos(torad(MmP + mEc)));
  159.  
  160.     /* Calculate Moon's angular diameter */
  161.  
  162.     MoonDFrac = MoonDist / msmax;
  163.     MoonAng = mangsiz / MoonDFrac;
  164.  
  165.     /* Calculate Moon's parallax */
  166.  
  167. //    MoonPar = mparallax / MoonDFrac;
  168. "%lf\n"
Last modified: 2006-11-04 01:40:44 (v423)   Valid XHTML 1.0

(c) 2009 Marcin Gryszkalis