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.
18 package org.altusmetrum.altoslib_4;
20 import java.lang.Math;
22 public class AltosGreatCircle implements Cloneable {
23 public double distance;
24 public double bearing;
26 public double elevation;
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 static final int BEARING_LONG = AltosConvert.BEARING_LONG;
34 public static final int BEARING_SHORT = AltosConvert.BEARING_SHORT;
35 public static final int BEARING_VOICE = AltosConvert.BEARING_VOICE;
37 public String bearing_words(int length) {
38 return AltosConvert.bearing_to_words(length, bearing);
41 public AltosGreatCircle (double start_lat, double start_lon, double start_alt,
42 double end_lat, double end_lon, double end_alt) {
43 double lat1 = rad * start_lat;
44 double lon1 = rad * -start_lon;
45 double lat2 = rad * end_lat;
46 double lon2 = rad * -end_lon;
48 double d_lon = lon2 - lon1;
50 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
51 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
52 sqr(Math.cos(lat1) * Math.sin(lat2) -
53 Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
54 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
55 double d = Math.atan2(vdn,vdd);
58 if (Math.cos(lat1) < 1e-20) {
67 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
68 (Math.sin(d)*Math.cos(lat1)));
69 if (Math.sin(lon2-lon1) > 0)
70 course = 2 * Math.PI-course;
72 distance = d * earth_radius;
73 bearing = course * 180/Math.PI;
75 double height_diff = end_alt - start_alt;
76 range = Math.sqrt(distance * distance + height_diff * height_diff);
77 elevation = Math.atan2(height_diff, distance) * 180 / Math.PI;
80 public AltosGreatCircle clone() {
81 AltosGreatCircle n = new AltosGreatCircle();
83 n.distance = distance;
86 n.elevation = elevation;
90 public AltosGreatCircle (double start_lat, double start_lon,
91 double end_lat, double end_lon) {
92 this(start_lat, start_lon, 0, end_lat, end_lon, 0);
95 public AltosGreatCircle(AltosGPS start, AltosGPS end) {
96 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
99 public AltosGreatCircle() {