xref: /plan9-contrib/sys/src/cmd/scat/util.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "sky.h"
5 
6 double	PI_180	= 0.0174532925199432957692369;
7 double	TWOPI	= 6.2831853071795864769252867665590057683943387987502;
8 double	LN2	= 0.69314718055994530941723212145817656807550013436025;
9 static double angledangle=(180./PI)*60.*60.*1000.;
10 
11 int
12 rint(char *p, int n)
13 {
14 	int i=0;
15 
16 	while(*p==' ' && n)
17 		p++, --n;
18 	while(n--)
19 		i=i*10+*p++-'0';
20 	return i;
21 }
22 
23 DAngle
24 dangle(Angle angle)
25 {
26 	return angle*angledangle;
27 }
28 
29 Angle
30 angle(DAngle dangle)
31 {
32 	return dangle/angledangle;
33 }
34 
35 double
36 rfloat(char *p, int n)
37 {
38 	double i, d=0;
39 
40 	while(*p==' ' && n)
41 		p++, --n;
42 	if(*p == '+')
43 		return rfloat(p+1, n-1);
44 	if(*p == '-')
45 		return -rfloat(p+1, n-1);
46 	while(*p == ' ' && n)
47 		p++, --n;
48 	if(n == 0)
49 		return 0.0;
50 	while(n-- && *p!='.')
51 		d = d*10+*p++-'0';
52 	if(n <= 0)
53 		return d;
54 	p++;
55 	i = 1;
56 	while(n--)
57 		d+=(*p++-'0')/(i*=10.);
58 	return d;
59 }
60 
61 int
62 sign(int c)
63 {
64 	if(c=='-')
65 		return -1;
66 	return 1;
67 }
68 
69 char*
70 hms(Angle a)
71 {
72 	static char buf[20];
73 	double x;
74 	int h, m, s, ts;
75 
76 	x=DEG(a)/15;
77 	x += 0.5/36000.;	/* round up half of 0.1 sec */
78 	h = floor(x);
79 	x -= h;
80 	x *= 60;
81 	m = floor(x);
82 	x -= m;
83 	x *= 60;
84 	s = floor(x);
85 	x -= s;
86 	ts = 10*x;
87 	sprint(buf, "%dh%.2dm%.2d.%ds", h, m, s, ts);
88 	return buf;
89 }
90 
91 char*
92 dms(Angle a)
93 {
94 	static char buf[20];
95 	double x;
96 	int sign, d, m, s, ts;
97 
98 	x = DEG(a);
99 	sign='+';
100 	if(a<0){
101 		sign='-';
102 		x=-x;
103 	}
104 	x += 0.5/36000.;	/* round up half of 0.1 arcsecond */
105 	d = floor(x);
106 	x -= d;
107 	x *= 60;
108 	m = floor(x);
109 	x -= m;
110 	x *= 60;
111 	s = floor(x);
112 	x -= s;
113 	ts = floor(10*x);
114 	sprint(buf, "%c%d°%.2d'%.2d.%d\"", sign, d, m, s, ts);
115 	return buf;
116 }
117 
118 char*
119 ms(Angle a)
120 {
121 	static char buf[20];
122 	double x;
123 	int d, m, s, ts;
124 
125 	x = DEG(a);
126 	x += 0.5/36000.;	/* round up half of 0.1 arcsecond */
127 	d = floor(x);
128 	x -= d;
129 	x *= 60;
130 	m = floor(x);
131 	x -= m;
132 	x *= 60;
133 	s = floor(x);
134 	x -= s;
135 	ts = floor(10*x);
136 	if(d != 0)
137 		sprint(buf, "%d°%.2d'%.2d.%d\"", d, m, s, ts);
138 	else
139 		sprint(buf, "%.2d'%.2d.%d\"", m, s, ts);
140 	return buf;
141 }
142 
143 char*
144 hm(Angle a)
145 {
146 	static char buf[20];
147 	double x;
148 	int h, m, n;
149 
150 	x = DEG(a)/15;
151 	x += 0.5/600.;	/* round up half of tenth of minute */
152 	h = floor(x);
153 	x -= h;
154 	x *= 60;
155 	m = floor(x);
156 	x -= m;
157 	x *= 10;
158 	n = floor(x);
159 	sprint(buf, "%dh%.2d.%1dm", h, m, n);
160 	return buf;
161 }
162 
163 char*
164 dm(Angle a)
165 {
166 	static char buf[20];
167 	double x;
168 	int sign, d, m, n;
169 
170 	x = DEG(a);
171 	sign='+';
172 	if(a<0){
173 		sign='-';
174 		x=-x;
175 	}
176 	x += 0.5/600.;	/* round up half of tenth of arcminute */
177 	d = floor(x);
178 	x -= d;
179 	x *= 60;
180 	m = floor(x);
181 	x -= m;
182 	x *= 10;
183 	n = floor(x);
184 	sprint(buf, "%c%d°%.2d.%.1d'", sign, d, m, n);
185 	return buf;
186 }
187 
188 char*
189 getword(char *ou, char *in)
190 {
191 	int c;
192 
193 	for(;;) {
194 		c = *in++;
195 		if(c == ' ' || c == '\t')
196 			continue;
197 		if(c == 0)
198 			return 0;
199 		break;
200 	}
201 
202 	if(c == '\'')
203 		for(;;) {
204 			if(c >= 'A' && c <= 'Z')
205 				c += 'a' - 'A';
206 			*ou++ = c;
207 			c = *in++;
208 			if(c == 0)
209 				return 0;
210 			if(c == '\'') {
211 				*ou = 0;
212 				return in-1;
213 			}
214 		}
215 	for(;;) {
216 		if(c >= 'A' && c <= 'Z')
217 			c += 'a' - 'A';
218 		*ou++ = c;
219 		c = *in++;
220 		if(c == ' ' || c == '\t' || c == 0) {
221 			*ou = 0;
222 			return in-1;
223 		}
224 	}
225 }
226 
227 /*
228  * Read formatted angle.  Must contain no embedded blanks
229  */
230 Angle
231 getra(char *p)
232 {
233 	Rune r;
234 	char *q;
235 	Angle f, d;
236 	int neg;
237 
238 	neg = 0;
239 	d = 0;
240 	while(*p == ' ')
241 		p++;
242 	for(;;) {
243 		if(*p == ' ')
244 			goto Return;
245 		if(*p == '-') {
246 			neg = 1;
247 			p++;
248 		}
249 		q = p;
250 		f = strtod(p, &q);
251 		if(q > p) {
252 			p = q;
253 		}
254 		p += chartorune(&r, p);
255 		switch(r) {
256 		default:
257 		Return:
258 			if(neg)
259 				d = -d;
260 			return RAD(d);
261 		case 'h':
262 			d += f*15;
263 			break;
264 		case 'm':
265 			d += f/4;
266 			break;
267 		case 's':
268 			d += f/240;
269 			break;
270 		case L'°':
271 			d += f;
272 			break;
273 		case '\'':
274 			d += f/60;
275 			break;
276 		case '\"':
277 			d += f/3600;
278 			break;
279 		}
280 	}
281 	return 0;
282 }
283 
284 double
285 xsqrt(double a)
286 {
287 
288 	if(a < 0)
289 		return 0;
290 	return sqrt(a);
291 }
292 
293 Angle
294 dist(Angle ra1, Angle dec1, Angle ra2, Angle dec2)
295 {
296 	double a;
297 
298 	a = sin(dec1) * sin(dec2) +
299 		cos(dec1) * cos(dec2) *
300 		cos(ra1 - ra2);
301 	a = atan2(xsqrt(1 - a*a), a);
302 	if(a < 0)
303 		a = -a;
304 	return a;
305 }
306 
307 int
308 dogamma(Pix c)
309 {
310 	float f;
311 
312 	f = c - gam.min;
313 	if(f < 1)
314 		f = 1;
315 
316 	if(gam.absgamma == 1)
317 		c = f * gam.mult2;
318 	else
319 		c = exp(log(f*gam.mult1) * gam.absgamma) * 255;
320 	if(c > 255)
321 		c = 255;
322 	if(gam.neg)
323 		c = 255-c;
324 	return c;
325 }
326