File: utilities.c

package info (click to toggle)
drawmap 1.3-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 404 kB
  • ctags: 399
  • sloc: ansic: 3,907; makefile: 61
file content (164 lines) | stat: -rw-r--r-- 3,150 bytes parent folder | download | duplicates (2)
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
/*
 * =========================================================================
 * utilities - A library of utility functions used by the drawmap program.
 * Copyright (c) 1997  Fred M. Erickson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * =========================================================================
 */


/*
 * Convert latitudes in degree/min/sec format into decimal degrees.
 *
 * We assume that there is no decimal point, or other punctuation,
 * and that the numeric latitude is in the DDMMSS format,
 * and that the latitude is immediately followed by 'N' or 'S'.
 */
double lat_conv(unsigned char *ptr)
{
	double lat;
	double min;
	double sec;

	lat = *ptr - '0';
	lat = lat * 10.0 + *(ptr + 1) - '0';
	min = *(ptr + 2) - '0';
	min = min * 10.0 + *(ptr + 3) - '0';
	sec = *(ptr + 4) - '0';
	sec = sec * 10.0 + *(ptr + 5) - '0';
	lat = lat + min / 60.0 + sec / 3600.0;
	if (*(ptr + 6) == 'S')  {
		lat = -lat;
	}
	return(lat);
}


/*
 * Convert longitudes in degree/min/sec format into decimal degrees.
 *
 * We assume that there is no decimal point, or other punctuation,
 * and that the numeric longitude is in the DDDMMSS format,
 * and that the longitude is immediately followed by 'W' or 'E'.
 */
double lon_conv(unsigned char *ptr)
{
	double lon;
	double min;
	double sec;

	lon = *ptr - '0';
	lon = lon * 10.0 + *(ptr + 1) - '0';
	lon = lon * 10.0 + *(ptr + 2) - '0';
	min = *(ptr + 3) - '0';
	min = min * 10.0 + *(ptr + 4) - '0';
	sec = *(ptr + 5) - '0';
	sec = sec * 10.0 + *(ptr + 6) - '0';
	lon = lon + min / 60.0 + sec / 3600.0;
	if (*(ptr + 7) == 'W')  {
		lon = -lon;
	}
	return(lon);
}




/* Round double values to long integers. */
long
round(double f)
{
	long i;
	double ff;

	i = (long)f;
	ff = (double)i;

	if (f < 0.0)  {
		if ((ff - f) >= 0.5)  {
			return(i - 1);
		}
	}
	else  {
		if ((f - ff) >= 0.5)  {
			return(i + 1);
		}
	}

	return(i);
}



/* Find the maximum of two long integers. */
long
max(long a, long b)
{
	if (a > b)  {
		return(a);
	}
	else  {
		return(b);
	}
}



/* Find the minimum of three doubles */
double
min3(double a, double b, double c)
{
	if (a < b)  {
		if (a < c)  {
			return(a);
		}
		else  {
			return(c);
		}
	}
	else  {
		if (b < c)  {
			return(b);
		}
		else  {
			return(c);
		}
	}
}



/* Find the maximum of three doubles */
double
max3(double a, double b, double c)
{
	if (a > b)  {
		if (a > c)  {
			return(a);
		}
		else  {
			return(c);
		}
	}
	else  {
		if (b > c)  {
			return(b);
		}
		else  {
			return(c);
		}
	}
}