-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
190 lines (165 loc) · 3.4 KB
/
utils.c
File metadata and controls
190 lines (165 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>
#include "astro.h"
/* conversion and print utilities */
/**
* convert hours to "hh:mm:ss.ssss"
* Returns a buffer that will become invalid the next time this
* function is called. Not thread-safe.
*/
const char *
convertHms(double hours)
{
static char obuf[80] ;
int h,m ;
double s ;
h2hms(hours, &h,&m,&s) ;
snprintf(obuf, sizeof(obuf), "%d:%2.2d:%f", h,m,s) ;
return obuf ;
}
/**
* convert hours (or degrees) to hours:minutes:seconds
*/
void
h2hms(hours, h,m,s)
double hours ;
int *h,*m ;
double *s ;
{
*h = hours ;
*m = hours*60. - *h*60 ;
*s = hours*3600. - *h*3600 - *m*60 ;
}
void
printHms(double hours)
{
printf("%s\n", convertHms(hours)) ;
}
/**
* Convert spherical coordinates to rectangular
*/
void
polar2rect(lat, lon, R, X,Y,Z)
double lat,lon,R ;
double *X,*Y,*Z ;
{
lat *= RAD ;
lon *= RAD ;
*X = R*cos(lat)*cos(lon) ;
*Y = R*cos(lat)*sin(lon) ;
*Z = R*sin(lat) ;
}
/**
* Convert rectangular coordinates to spherical
*/
void
rect2polar(X,Y,Z, lat,lon,R)
double X,Y,Z ;
double *lat,*lon,*R ;
{
*R = sqrt(X*X+Y*Y+Z*Z) ;
if( *R > 0. ) {
*lat = asin(Z / *R) * DEG ;
*lon = atan2(Y,X) * DEG ;
}
else
*lat = *lon = 0. ;
}
/**
* given polar coordinates of two objects, find the bearing
* and distance of object2 relative to object1
*/
void
deltaPolar(lat1,lon1,r1, lat2,lon2,r2, lat3,lon3,r3)
double lat1,lon1,r1 ;
double lat2,lon2,r2 ;
double *lat3, *lon3, *r3 ;
{
double x1,y1,z1, x2,y2,z2, x3,y3,z3 ;
polar2rect(lat1, lon1, r1, &x1,&y1,&z1) ;
polar2rect(lat2, lon2, r2, &x2,&y2,&z2) ;
x3 = x2 - x1 ;
y3 = y2 - y1 ;
z3 = z2 - z1 ;
rect2polar(x3,y3,z3, lat3,lon3,r3) ;
}
/**
* Return a % 360
*/
double
limitAngle(double a)
{
if( a >= 360 ) {
int ia = a;
a -= ia;
return ia % 360 + a;
} else if( a < 0 ) {
return 360 - limitAngle(-a);
}
return a;
}
/**
* Return h % 24
*/
double
limitHour(double h)
{
if( h >= 24 ) {
int ih = h;
h -= ih;
return ih % 24 + h;
} else if( h < 0 ) {
return 24 - limitHour(-h);
}
return h;
}
/**
* Extract a float field from the given buffer
* @param buffer - buffer to extract the field from
* @param start - index of first character, 1-based
* @param end - index of last character, inclusive
*/
double
recFloat(const char *buffer, int start, int end)
{
double rval = 0;
char tmp[80];
recString(buffer, start, end, tmp);
sscanf(tmp, "%lf", &rval);
return rval;
}
/**
* Extract a long field from the given buffer
* @param buffer - buffer to extract the field from
* @param start - index of first character, 1-based
* @param end - index of last character, inclusive
*/
long
recLong(const char *buffer, int start, int end)
{
long rval = 0;
char tmp[80];
recString(buffer, start, end, tmp);
sscanf(tmp, "%ld", &rval);
return rval;
}
/**
* Extract a string from the given buffer
* @param buffer - buffer to extract the field from
* @param start - index of first character, 1-based
* @param end - index of last character, inclusive
* @param tmp - caller-supplied buffer to receive string
*
* Caller is responsible for making sure tmp[] is at least
* (end-start+1) characters.
*/
char *
recString(const char *buffer, int start, int end, char *tmp)
{
memcpy(tmp, buffer+start-1, end-start+1);
tmp[end-start+1] = '\0';
return tmp;
}