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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_14;
21 import java.lang.Math;
24 public class AltosGreatCircle implements Cloneable {
25 public double distance;
26 public double bearing;
28 public double elevation;
30 double sqr(double a) { return a * a; }
32 public static final double rad = Math.PI / 180;
33 public static final double earth_radius = 6371.2 * 1000; /* in meters */
35 public static final int BEARING_LONG = AltosConvert.BEARING_LONG;
36 public static final int BEARING_SHORT = AltosConvert.BEARING_SHORT;
37 public static final int BEARING_VOICE = AltosConvert.BEARING_VOICE;
39 public String bearing_words(int length) {
40 return AltosConvert.bearing_to_words(length, bearing);
43 public AltosGreatCircle (double start_lat, double start_lon, double start_alt,
44 double end_lat, double end_lon, double end_alt) {
45 double lat1 = rad * start_lat;
46 double lon1 = rad * -start_lon;
47 double lat2 = rad * end_lat;
48 double lon2 = rad * -end_lon;
50 double d_lon = lon2 - lon1;
52 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
53 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
54 sqr(Math.cos(lat1) * Math.sin(lat2) -
55 Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
56 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
57 double d = Math.atan2(vdn,vdd);
60 if (Math.cos(lat1) < 1e-20) {
69 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
70 (Math.sin(d)*Math.cos(lat1)));
71 if (Math.sin(lon2-lon1) > 0)
72 course = 2 * Math.PI-course;
74 distance = d * earth_radius;
75 if (Double.isNaN(course) || Double.isInfinite(course))
78 bearing = course * 180/Math.PI;
80 double height_diff = end_alt - start_alt;
81 range = Math.sqrt(distance * distance + height_diff * height_diff);
82 elevation = Math.atan2(height_diff, distance) * 180 / Math.PI;
85 public AltosGreatCircle clone() {
86 AltosGreatCircle n = new AltosGreatCircle();
88 n.distance = distance;
91 n.elevation = elevation;
95 public AltosGreatCircle (double start_lat, double start_lon,
96 double end_lat, double end_lon) {
97 this(start_lat, start_lon, 0, end_lat, end_lon, 0);
100 public AltosGreatCircle(AltosGPS start, AltosGPS end) {
101 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
104 public AltosGreatCircle() {