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_9;
20 import java.lang.Math;
23 public class AltosGreatCircle implements Cloneable, Serializable {
24 public double distance;
25 public double bearing;
27 public double elevation;
29 double sqr(double a) { return a * a; }
31 public static final double rad = Math.PI / 180;
32 public static final double earth_radius = 6371.2 * 1000; /* in meters */
34 public static final int BEARING_LONG = AltosConvert.BEARING_LONG;
35 public static final int BEARING_SHORT = AltosConvert.BEARING_SHORT;
36 public static final int BEARING_VOICE = AltosConvert.BEARING_VOICE;
38 public String bearing_words(int length) {
39 return AltosConvert.bearing_to_words(length, bearing);
42 public AltosGreatCircle (double start_lat, double start_lon, double start_alt,
43 double end_lat, double end_lon, double end_alt) {
44 double lat1 = rad * start_lat;
45 double lon1 = rad * -start_lon;
46 double lat2 = rad * end_lat;
47 double lon2 = rad * -end_lon;
49 double d_lon = lon2 - lon1;
51 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
52 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
53 sqr(Math.cos(lat1) * Math.sin(lat2) -
54 Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
55 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
56 double d = Math.atan2(vdn,vdd);
59 if (Math.cos(lat1) < 1e-20) {
68 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
69 (Math.sin(d)*Math.cos(lat1)));
70 if (Math.sin(lon2-lon1) > 0)
71 course = 2 * Math.PI-course;
73 distance = d * earth_radius;
74 bearing = course * 180/Math.PI;
76 double height_diff = end_alt - start_alt;
77 range = Math.sqrt(distance * distance + height_diff * height_diff);
78 elevation = Math.atan2(height_diff, distance) * 180 / Math.PI;
81 public AltosGreatCircle clone() {
82 AltosGreatCircle n = new AltosGreatCircle();
84 n.distance = distance;
87 n.elevation = elevation;
91 public AltosGreatCircle (double start_lat, double start_lon,
92 double end_lat, double end_lon) {
93 this(start_lat, start_lon, 0, end_lat, end_lon, 0);
96 public AltosGreatCircle(AltosGPS start, AltosGPS end) {
97 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
100 public AltosGreatCircle() {