telegps: Add 'Info' tab
[fw/altos] / altoslib / AltosGreatCircle.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_4;
19
20 import java.lang.Math;
21
22 public class AltosGreatCircle implements Cloneable {
23         public double   distance;
24         public double   bearing;
25         public double   range;
26         public double   elevation;
27
28         double sqr(double a) { return a * a; }
29
30         static final double rad = Math.PI / 180;
31         static final double earth_radius = 6371.2 * 1000;       /* in meters */
32
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;
36
37         public String bearing_words(int length) {
38                 return AltosConvert.bearing_to_words(length, bearing);
39         }
40
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;
47
48                 double d_lon = lon2 - lon1;
49
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);
56                 double course;
57
58                 if (Math.cos(lat1) < 1e-20) {
59                         if (lat1 > 0)
60                                 course = Math.PI;
61                         else
62                                 course = -Math.PI;
63                 } else {
64                         if (d < 1e-10)
65                                 course = 0;
66                         else
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;
71                 }
72                 distance = d * earth_radius;
73                 bearing = course * 180/Math.PI;
74
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;
78         }
79
80         public AltosGreatCircle clone() {
81                 AltosGreatCircle n = new AltosGreatCircle();
82
83                 n.distance = distance;
84                 n.bearing = bearing;
85                 n.range = range;
86                 n.elevation = elevation;
87                 return n;
88         }
89
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);
93         }
94
95         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
96                 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
97         }
98
99         public AltosGreatCircle() {
100                 distance = 0;
101                 bearing = 0;
102                 range = 0;
103                 elevation = 0;
104         }
105 }