4f9345179ab3ae77adfd42427532768cefdd3754
[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_7;
19
20 import java.lang.Math;
21 import java.io.*;
22
23 public class AltosGreatCircle implements Cloneable, Serializable {
24         public double   distance;
25         public double   bearing;
26         public double   range;
27         public double   elevation;
28
29         double sqr(double a) { return a * a; }
30
31         public static final double rad = Math.PI / 180;
32         public static final double earth_radius = 6371.2 * 1000;        /* in meters */
33
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;
37
38         public String bearing_words(int length) {
39                 return AltosConvert.bearing_to_words(length, bearing);
40         }
41
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;
48
49                 double d_lon = lon2 - lon1;
50
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);
57                 double course;
58
59                 if (Math.cos(lat1) < 1e-20) {
60                         if (lat1 > 0)
61                                 course = Math.PI;
62                         else
63                                 course = -Math.PI;
64                 } else {
65                         if (d < 1e-10)
66                                 course = 0;
67                         else
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;
72                 }
73                 distance = d * earth_radius;
74                 bearing = course * 180/Math.PI;
75
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;
79         }
80
81         public AltosGreatCircle clone() {
82                 AltosGreatCircle n = new AltosGreatCircle();
83
84                 n.distance = distance;
85                 n.bearing = bearing;
86                 n.range = range;
87                 n.elevation = elevation;
88                 return n;
89         }
90
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);
94         }
95
96         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
97                 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
98         }
99
100         public AltosGreatCircle() {
101                 distance = 0;
102                 bearing = 0;
103                 range = 0;
104                 elevation = 0;
105         }
106 }