| [3ae31e9] | 1 | /* pom.c -- The phase of the moon, for your safety and convenience.
 | 
|---|
 | 2 | **
 | 
|---|
 | 3 | **  Stolen from ArchMach & converted from PL/I by Brian Hess.
 | 
|---|
 | 4 | **  Extensively cleaned up by Rich Salz.
 | 
|---|
 | 5 | **
 | 
|---|
 | 6 | **  Minor bug fixes by D.N. Lynx Crowe  1985-06-05
 | 
|---|
 | 7 | **
 | 
|---|
 | 8 | **  If you can figure out how this program works, then YOU deserve
 | 
|---|
 | 9 | **  to be working on it, sucker!  Here's a hint:  The epoch is 13:23,
 | 
|---|
 | 10 | **  10/1/78.
 | 
|---|
 | 11 | */
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | #ifdef  UNIX
 | 
|---|
 | 14 | #include <math.h>
 | 
|---|
 | 15 | #include <sys/types.h>
 | 
|---|
 | 16 | #include <time.h>
 | 
|---|
 | 17 | #else
 | 
|---|
 | 18 | #include "math.h"
 | 
|---|
 | 19 | #include "types.h"
 | 
|---|
 | 20 | #include "time.h"
 | 
|---|
 | 21 | #endif
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /* Globals. */
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | long             Day;
 | 
|---|
 | 26 | long             Hour;
 | 
|---|
 | 27 | long             Minute;
 | 
|---|
 | 28 | double           Fraction;
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | char            *Moon[] = {
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 |     "new",
 | 
|---|
 | 33 |     "first quarter of the",
 | 
|---|
 | 34 |     "full",
 | 
|---|
 | 35 |     "last quarter of the"
 | 
|---|
 | 36 | };
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | /* Linked in later. */
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | time_t           time();
 | 
|---|
 | 42 | struct tm       *localtime();
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | #define LINES           24
 | 
|---|
 | 46 | #define WIDTH           80
 | 
|---|
 | 47 | #define CENTER          ((WIDTH - 2 * LINES) / 2)
 | 
|---|
 | 48 | #define BRIGHT          '@'
 | 
|---|
 | 49 | #define LEDGE           '('
 | 
|---|
 | 50 | #define REDGE           ')'
 | 
|---|
 | 51 | #define FULL            0.5
 | 
|---|
 | 52 | #define TwoPi           (2 * 3.1415927)
 | 
|---|
 | 53 | #define ZERO            0.03
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | #define Plural(X)       if (X != 1) printf("s");
 | 
|---|
 | 56 |  | 
|---|
 | 57 | 
 | 
|---|
 | 58 | /*
 | 
|---|
 | 59 |    -----------------------------------------------------------------------
 | 
|---|
 | 60 |         Calculate() -- calculate the phase of the moon
 | 
|---|
 | 61 |    -----------------------------------------------------------------------
 | 
|---|
 | 62 | */
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | long
 | 
|---|
 | 65 | Calculate()
 | 
|---|
 | 66 | {
 | 
|---|
 | 67 |     register struct tm  *tm;
 | 
|---|
 | 68 |     register long        Length;
 | 
|---|
 | 69 |     register long        Phase;
 | 
|---|
 | 70 |     register long        DeltaH;
 | 
|---|
 | 71 |     register long        Delta;
 | 
|---|
 | 72 |     register long        offset;
 | 
|---|
 | 73 |     time_t               tick;
 | 
|---|
 | 74 |     long                 julian;
 | 
|---|
 | 75 |     long                 year;
 | 
|---|
 | 76 |     long                 hour;
 | 
|---|
 | 77 |     long                 minute;
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |     tick        = time((time_t *)0);
 | 
|---|
 | 80 |     tm          = localtime(&tick);
 | 
|---|
 | 81 |     julian      = tm->tm_yday + 1;
 | 
|---|
 | 82 |     year        = tm->tm_year - 78;
 | 
|---|
 | 83 |     hour        = tm->tm_hour;
 | 
|---|
 | 84 |     minute      = tm->tm_min;
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |     Length      = (double)2551 / 60 * 1000 + (double)443 / 60;
 | 
|---|
 | 87 |     offset      = ((year * 365L + julian) * 24L + hour) * 60L + minute;
 | 
|---|
 | 88 |     Delta       = offset - (273L * 24L + 13L) * 60L + 23L;
 | 
|---|
 | 89 |     Phase       = Delta - (Delta / Length) * Length;
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |     Fraction    = (double)Phase / (double)Length;
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |     Length      = Length / 4;
 | 
|---|
 | 94 |     Phase       = Phase / Length;
 | 
|---|
 | 95 |     Delta       = Delta - (Delta / Length) * Length;
 | 
|---|
 | 96 |     DeltaH      = Delta / 60;
 | 
|---|
 | 97 |     Minute      = Delta - DeltaH * 60;
 | 
|---|
 | 98 |     Day         = DeltaH / 24;
 | 
|---|
 | 99 |     Hour        = DeltaH - Day * 24;
 | 
|---|
 | 100 |     return(Phase);
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 |  | 
|---|
 | 103 | 
 | 
|---|
 | 104 | /*
 | 
|---|
 | 105 |    -----------------------------------------------------------------------
 | 
|---|
 | 106 |         CharPos(x) -- calculate character position for Draw
 | 
|---|
 | 107 |    -----------------------------------------------------------------------
 | 
|---|
 | 108 | */
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 | int
 | 
|---|
 | 111 | CharPos(x)
 | 
|---|
 | 112 |     double              x;
 | 
|---|
 | 113 | {
 | 
|---|
 | 114 |     register int        i;
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |     i = x * LINES + 0.5;
 | 
|---|
 | 117 |     if ((i += LINES + CENTER) < 1)
 | 
|---|
 | 118 |         i = 1;
 | 
|---|
 | 119 |     return(i);
 | 
|---|
 | 120 | }
 | 
|---|
 | 121 |  | 
|---|
 | 122 | 
 | 
|---|
 | 123 | /*
 | 
|---|
 | 124 |    -----------------------------------------------------------------------
 | 
|---|
 | 125 |         Draw() -- draw the moon
 | 
|---|
 | 126 |    -----------------------------------------------------------------------
 | 
|---|
 | 127 | */
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | void
 | 
|---|
 | 130 | Draw()
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |     register char       *p;
 | 
|---|
 | 133 |     register int         i;
 | 
|---|
 | 134 |     register int         end;
 | 
|---|
 | 135 |     register double      y;
 | 
|---|
 | 136 |     register double      cht;
 | 
|---|
 | 137 |     register double      squisher;
 | 
|---|
 | 138 |     register double      horizon;
 | 
|---|
 | 139 |     register double      terminator;
 | 
|---|
 | 140 |     char                 Buffer[256];
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 |     /* Clear screen? */
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |     if (Fraction < FULL)
 | 
|---|
 | 145 |         squisher = cos(TwoPi * Fraction);
 | 
|---|
 | 146 |     else
 | 
|---|
 | 147 |         squisher = cos(TwoPi * (Fraction - FULL));
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |     cht = (double)2.0 / (LINES - 6.0);
 | 
|---|
 | 150 |     for (y = 0.93; y > -1.0; y -= cht)
 | 
|---|
 | 151 |     {
 | 
|---|
 | 152 |         for (i = sizeof Buffer, p = Buffer; --i >= 0; )
 | 
|---|
 | 153 |             *p++ = ' ';
 | 
|---|
 | 154 |         horizon = sqrt(1.0 - y * y);
 | 
|---|
 | 155 |         Buffer[    CharPos(-horizon)]   = LEDGE;
 | 
|---|
 | 156 |         Buffer[i = CharPos( horizon)]   = REDGE;
 | 
|---|
 | 157 |         Buffer[++i]                     = '\0';
 | 
|---|
 | 158 |         terminator = horizon * squisher;
 | 
|---|
 | 159 |         if (Fraction > ZERO && Fraction < (1.0 - ZERO))
 | 
|---|
 | 160 |         {
 | 
|---|
 | 161 |             if (Fraction < FULL)
 | 
|---|
 | 162 |             {
 | 
|---|
 | 163 |                 i   = CharPos( terminator);
 | 
|---|
 | 164 |                 end = CharPos( horizon);
 | 
|---|
 | 165 |             }
 | 
|---|
 | 166 |             else
 | 
|---|
 | 167 |             {
 | 
|---|
 | 168 |                 i   = CharPos(-horizon);
 | 
|---|
 | 169 |                 end = CharPos( terminator);
 | 
|---|
 | 170 |             }
 | 
|---|
 | 171 |             while (i <= end)
 | 
|---|
 | 172 |                 Buffer[i++] = BRIGHT;
 | 
|---|
 | 173 |         }
 | 
|---|
 | 174 |         printf(" %s\n", Buffer);
 | 
|---|
 | 175 |     }
 | 
|---|
 | 176 | }
 | 
|---|
 | 177 |  | 
|---|
 | 178 | 
 | 
|---|
 | 179 | /*
 | 
|---|
 | 180 |    -----------------------------------------------------------------------
 | 
|---|
 | 181 |         main(ac) -- calculate the phase of the moon and optionally draw it
 | 
|---|
 | 182 |    -----------------------------------------------------------------------
 | 
|---|
 | 183 | */
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 | main(ac)
 | 
|---|
 | 186 |     int                  ac;
 | 
|---|
 | 187 | {
 | 
|---|
 | 188 |     register long        Phase;
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |     Phase = Calculate();
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |     if (ac != 1)
 | 
|---|
 | 193 |         Draw();
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 |     printf("\nIt is ");
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 |     if (Day == 0 && Hour == 0 && Minute == 0)
 | 
|---|
 | 198 |         printf("exactly");
 | 
|---|
 | 199 |     else {
 | 
|---|
 | 200 | 
 | 
|---|
 | 201 |         if (Day) {
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |             printf("%ld day", Day);
 | 
|---|
 | 204 |             Plural(Day);
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 |             if (Hour | Day)
 | 
|---|
 | 207 |                 printf(", ");
 | 
|---|
 | 208 |         }
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 |         if (Hour) {
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |             printf("%ld hour", Hour);
 | 
|---|
 | 213 |             Plural(Hour);
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |             if (Minute)
 | 
|---|
 | 216 |                 printf(", ");
 | 
|---|
 | 217 |         }
 | 
|---|
 | 218 | 
 | 
|---|
 | 219 |         if (Minute) {
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 |             printf("%ld minute", Minute);
 | 
|---|
 | 222 |             Plural(Minute);
 | 
|---|
 | 223 |         }
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 |         printf(" since");
 | 
|---|
 | 226 |     }
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 |     printf(" the %s moon.\n", Moon[Phase]);
 | 
|---|
 | 229 | 
 | 
|---|
 | 230 |     exit(0);
 | 
|---|
 | 231 | }
 | 
|---|