a2f128074c89ebe29b446f7af027683762acf9b4
[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_11;
19
20 import java.lang.Math;
21 import java.io.*;
22
23 public class AltosGreatCircle implements Cloneable {
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                 if (Double.isNaN(course) || Double.isInfinite(course))
75                         bearing = 0;
76                 else
77                         bearing = course * 180/Math.PI;
78
79                 double height_diff = end_alt - start_alt;
80                 range = Math.sqrt(distance * distance + height_diff * height_diff);
81                 elevation = Math.atan2(height_diff, distance) * 180 / Math.PI;
82         }
83
84         public AltosGreatCircle clone() {
85                 AltosGreatCircle n = new AltosGreatCircle();
86
87                 n.distance = distance;
88                 n.bearing = bearing;
89                 n.range = range;
90                 n.elevation = elevation;
91                 return n;
92         }
93
94         public AltosGreatCircle (double start_lat, double start_lon,
95                                  double end_lat, double end_lon) {
96                 this(start_lat, start_lon, 0, end_lat, end_lon, 0);
97         }
98
99         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
100                 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
101         }
102
103         public AltosGreatCircle() {
104                 distance = 0;
105                 bearing = 0;
106                 range = 0;
107                 elevation = 0;
108         }
109 }