File: circle.5c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (32 lines) | stat: -rw-r--r-- 871 bytes parent folder | download | duplicates (4)
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
 * Takes degrees lat/long pairs,
 * Returns miles + compass degrees
 * Bart Massey 2000
 * From Javascript by Jeff Waldock 1997
 * http://jeff.sci.shu.ac.uk/jw/javascript/js2.html
 * 
 * XXX Appears to need some angle reductions: the bearing
 * may be off by a multiple of 90 degrees.
 */

typedef struct {
  real lat, lon;
} loc;

typedef struct {
  real dist, bearing;
} course;

course great_circle (loc start, loc end) {
	real rad = pi / 180;
	/* real earth_radius = 6371.2 km ; */
	real earth_radius = 3958.9; /* miles */
	real a = (90 - start.lat) * rad;
	real b = (90 - end.lat) * rad;
	real phi = (end.lon - start.lon) * rad;
	real cosr = cos(a) * cos(b) + sin(a) * sin(b) * cos(phi);
	real r = acos(cosr);
	real rdist = earth_radius * r;
	real sinth = sin(phi) * sin(b) / sin(r);
	real th = asin(sinth) / rad;
	return (course){ .dist = rdist, .bearing = th };
}