Merge branch 'prefs_interface' into altosdroid
[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;
19
20 import java.lang.Math;
21
22 public class AltosGreatCircle {
23         public double   distance;
24         public double   bearing;
25
26         double sqr(double a) { return a * a; }
27
28         static final double rad = Math.PI / 180;
29         static final double earth_radius = 6371.2 * 1000;       /* in meters */
30
31         public static final int BEARING_LONG = 0;
32         public static final int BEARING_SHORT = 1;
33         public static final int BEARING_VOICE = 2;
34
35         public String bearing_words(int length) {
36                 String [][] bearing_string = {
37                         {
38                                 "North", "North North East", "North East", "East North East",
39                                 "East", "East South East", "South East", "South South East",
40                                 "South", "South South West", "South West", "West South West",
41                                 "West", "West North West", "North West", "North North West"
42                         }, {
43                                 "N", "NNE", "NE", "ENE",
44                                 "E", "ESE", "SE", "SSE",
45                                 "S", "SSW", "SW", "WSW",
46                                 "W", "WNW", "NW", "NNW"
47                         }, {
48                                 "north", "nor nor east", "north east", "east nor east",
49                                 "east", "east sow east", "south east", "sow sow east",
50                                 "south", "sow sow west", "south west", "west sow west",
51                                 "west", "west nor west", "north west", "nor nor west "
52                         }
53                 };
54                 return bearing_string[length][(int)((bearing / 90 * 8 + 1) / 2)%16];
55         }
56
57         public AltosGreatCircle (double start_lat, double start_lon,
58                                  double end_lat, double end_lon)
59         {
60                 double lat1 = rad * start_lat;
61                 double lon1 = rad * -start_lon;
62                 double lat2 = rad * end_lat;
63                 double lon2 = rad * -end_lon;
64
65                 double d_lon = lon2 - lon1;
66
67                 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
68                 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
69                                        sqr(Math.cos(lat1) * Math.sin(lat2) -
70                                            Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
71                 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
72                 double d = Math.atan2(vdn,vdd);
73                 double course;
74
75                 if (Math.cos(lat1) < 1e-20) {
76                         if (lat1 > 0)
77                                 course = Math.PI;
78                         else
79                                 course = -Math.PI;
80                 } else {
81                         if (d < 1e-10)
82                                 course = 0;
83                         else
84                                 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
85                                                    (Math.sin(d)*Math.cos(lat1)));
86                         if (Math.sin(lon2-lon1) > 0)
87                                 course = 2 * Math.PI-course;
88                 }
89                 distance = d * earth_radius;
90                 bearing = course * 180/Math.PI;
91         }
92
93         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
94                 this(start.lat, start.lon, end.lat, end.lon);
95         }
96
97         public AltosGreatCircle() {
98                 distance = 0;
99                 bearing = 0;
100         }
101 }