xref: /dragonfly/games/pom/pom.c (revision a3127495)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software posted to USENET.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
32  * @(#)pom.c       8.1 (Berkeley) 5/31/93
33  * $FreeBSD: src/games/pom/pom.c,v 1.9 1999/11/30 03:49:09 billf Exp $
34  */
35 
36 /*
37  * Phase of the Moon.  Calculates the current phase of the moon.
38  * Based on routines from `Practical Astronomy with Your Calculator',
39  * by Duffett-Smith.  Comments give the section from the book that
40  * particular piece of code was adapted from.
41  *
42  * -- Keith E. Brandt  VIII 1984
43  *
44  */
45 
46 #include <time.h>
47 #include <stdio.h>
48 #include <math.h>
49 
50 #define	EPOCH	  85
51 #define	EPSILONg  279.611371	/* solar ecliptic long at EPOCH */
52 #define	RHOg	  282.680403	/* solar ecliptic long of perigee at EPOCH */
53 #define	ECCEN	  0.01671542	/* solar orbit eccentricity */
54 #define	lzero	  18.251907	/* lunar mean long at EPOCH */
55 #define	Pzero	  192.917585	/* lunar mean long of perigee at EPOCH */
56 #define	Nzero	  55.204723	/* lunar mean long of node at EPOCH */
57 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
58 
59 static void	adj360 (double *);
60 static double	dtor (double);
61 static double	potm (double);
62 
63 int
64 main(void)
65 {
66 	time_t tt;
67 	struct tm *GMT;
68 	double days, today, tomorrow;
69 	int cnt;
70 
71 	time(&tt);
72 	GMT = gmtime(&tt);
73 	days = (GMT->tm_yday + 1) + ((GMT->tm_hour +
74 	    (GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0);
75 	for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt)
76 		days += isleap(1900 + cnt) ? 366 : 365;
77 	today = potm(days) + .5;
78 	printf("The Moon is ");
79 	if ((int)today == 100)
80 		printf("Full\n");
81 	else if (!(int)today)
82 		printf("New\n");
83 	else {
84 		tomorrow = potm(days + 1);
85 		if ((int)today == 50)
86 			printf("%s\n", tomorrow > today ?
87 			    "at the First Quarter" : "at the Last Quarter");
88 		else {
89 			printf("%s ", tomorrow > today ?
90 			    "Waxing" : "Waning");
91 			if (today > 50)
92 				printf("Gibbous (%1.0f%% of Full)\n", today);
93 			else if (today < 50)
94 				printf("Crescent (%1.0f%% of Full)\n", today);
95 		}
96 	}
97 
98 	return 0;
99 }
100 
101 /*
102  * potm --
103  *	return phase of the moon
104  */
105 static double
106 potm(double days)
107 {
108 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
109 	double A4, lprime, V, ldprime, D, Nm;
110 
111 	N = 360 * days / 365.2422;				/* sec 42 #3 */
112 	adj360(&N);
113 	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
114 	adj360(&Msol);
115 	Ec = 360 / M_PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
116 	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
117 	adj360(&LambdaSol);
118 	l = 13.1763966 * days + lzero;				/* sec 61 #4 */
119 	adj360(&l);
120 	Mm = l - (0.1114041 * days) - Pzero;			/* sec 61 #5 */
121 	adj360(&Mm);
122 	Nm = Nzero - (0.0529539 * days);			/* sec 61 #6 */
123 	adj360(&Nm);
124 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
125 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
126 	A3 = 0.37 * sin(dtor(Msol));
127 	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
128 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
129 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
130 	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
131 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
132 	ldprime = lprime + V;					/* sec 61 #14 */
133 	D = ldprime - LambdaSol;				/* sec 63 #2 */
134 	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
135 }
136 
137 /*
138  * dtor --
139  *	convert degrees to radians
140  */
141 static double
142 dtor(double deg)
143 {
144 	return(deg * M_PI / 180);
145 }
146 
147 /*
148  * adj360 --
149  *	adjust value so 0 <= deg <= 360
150  */
151 static void
152 adj360(double *deg)
153 {
154 	for (;;)
155 		if (*deg < 0)
156 			*deg += 360;
157 		else if (*deg > 360)
158 			*deg -= 360;
159 		else
160 			break;
161 }
162