07c02c16c34c17d665335ff3c657482a289a4a62
[fw/altos] / ao-tools / altosui / 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 altosui;
19
20 import altosui.AltosGPS;
21
22 import java.lang.Math;
23
24 public class AltosGreatCircle {
25         double  distance;
26         double  bearing;
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 AltosGreatCircle (double start_lat, double start_lon,
34                                  double end_lat, double end_lon)
35         {
36                 double lat1 = rad * start_lat;
37                 double lon1 = rad * -start_lon;
38                 double lat2 = rad * end_lat;
39                 double lon2 = rad * -end_lon;
40
41                 double d_lon = lon2 - lon1;
42
43                 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
44                 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
45                                        sqr(Math.cos(lat1) * Math.sin(lat2) -
46                                            Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
47                 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
48                 double d = Math.atan2(vdn,vdd);
49                 double course;
50
51                 if (Math.cos(lat1) < 1e-20) {
52                         if (lat1 > 0)
53                                 course = Math.PI;
54                         else
55                                 course = -Math.PI;
56                 } else {
57                         if (d < 1e-10)
58                                 course = 0;
59                         else
60                                 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
61                                                    (Math.sin(d)*Math.cos(lat1)));
62                         if (Math.sin(lon2-lon1) > 0)
63                                 course = 2 * Math.PI-course;
64                 }
65                 distance = d * earth_radius;
66                 bearing = course * 180/Math.PI;
67         }
68
69         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
70                 this(start.lat, start.lon, end.lat, end.lon);
71         }
72
73         public AltosGreatCircle() {
74                 distance = 0;
75                 bearing = 0;
76         }
77 }