2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 import altosui.AltosGPS;
22 import java.lang.Math;
24 public class AltosGreatCircle {
28 double sqr(double a) { return a * a; }
30 static final double rad = Math.PI / 180;
31 static final double earth_radius = 6371.2 * 1000; /* in meters */
33 public AltosGreatCircle (double start_lat, double start_lon,
34 double end_lat, double end_lon)
36 double lat1 = rad * start_lat;
37 double lon1 = rad * -start_lon;
38 double lat2 = rad * end_lat;
39 double lon2 = rad * -end_lon;
41 double d_lon = lon2 - lon1;
43 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
44 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
45 sqr(Math.cos(lat1) * Math.sin(lat2) -
46 Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
47 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
48 double d = Math.atan2(vdn,vdd);
51 if (Math.cos(lat1) < 1e-20) {
60 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
61 (Math.sin(d)*Math.cos(lat1)));
62 if (Math.sin(lon2-lon1) > 0)
63 course = 2 * Math.PI-course;
65 distance = d * earth_radius;
66 bearing = course * 180/Math.PI;
69 public AltosGreatCircle(AltosGPS start, AltosGPS end) {
70 this(start.lat, start.lon, end.lat, end.lon);
73 public AltosGreatCircle() {