1/*
2 * deg - calculate in degrees, minutes, and seconds
3 *
4 * Copyright (C) 1999  David I. Bell
5 *
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
9 *
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
13 * Public License for more details.
14 *
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL.  You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 *
20 * Under source code control:	1990/02/15 01:50:33
21 * File existed as early as:	before 1990
22 *
23 * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
24 */
25
26
27obj deg {deg, min, sec};
28
29define deg(deg, min, sec)
30{
31	local ans;
32
33	if (isnull(sec))
34		sec = 0;
35	if (isnull(min))
36		min = 0;
37	obj deg ans;
38	ans.deg = deg;
39	ans.min = min;
40	ans.sec = sec;
41	fixdeg(ans);
42	return ans;
43}
44
45
46define deg_add(a, b)
47{
48	local obj deg	ans;
49
50	ans.deg = 0;
51	ans.min = 0;
52	ans.sec = 0;
53	if (istype(a, ans)) {
54		ans.deg += a.deg;
55		ans.min += a.min;
56		ans.sec += a.sec;
57	} else
58		ans.deg += a;
59	if (istype(b, ans)) {
60		ans.deg += b.deg;
61		ans.min += b.min;
62		ans.sec += b.sec;
63	} else
64		ans.deg += b;
65	fixdeg(ans);
66	return ans;
67}
68
69
70define deg_neg(a)
71{
72	local obj deg	ans;
73
74	ans.deg = -a.deg;
75	ans.min = -a.min;
76	ans.sec = -a.sec;
77	return ans;
78}
79
80
81define deg_sub(a, b)
82{
83	return a - b;
84}
85
86
87define deg_mul(a, b)
88{
89	local obj deg	ans;
90
91	if (istype(a, ans) && istype(b, ans))
92		quit "Cannot multiply degrees together";
93	if (istype(a, ans)) {
94		ans.deg = a.deg * b;
95		ans.min = a.min * b;
96		ans.sec = a.sec * b;
97	} else {
98		ans.deg = b.deg * a;
99		ans.min = b.min * a;
100		ans.sec = b.sec * a;
101	}
102	fixdeg(ans);
103	return ans;
104}
105
106
107define deg_print(a)
108{
109	print a.deg : 'd' : a.min : 'm' : a.sec : 's' :;
110}
111
112
113define deg_abs(a)
114{
115	return a.deg + a.min / 60 + a.sec / 3600;
116}
117
118
119define fixdeg(a)
120{
121	a.min += frac(a.deg) * 60;
122	a.deg = int(a.deg);
123	a.sec += frac(a.min) * 60;
124	a.min = int(a.min);
125	a.min += a.sec // 60;
126	a.sec %= 60;
127	a.deg += a.min // 60;
128	a.min %= 60;
129	a.deg %= 360;
130}
131
132if (config("resource_debug") & 3) {
133    print "obj deg {deg, min, sec} defined";
134}
135