]> git.gag.com Git - fw/altos/blob - altoslib/AltosGreatCircle.java
altos/stmf0: Fixup for SPI mode support
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.lang.Math;
22 import java.io.*;
23
24 public class AltosGreatCircle implements Cloneable {
25         public double   distance;
26         public double   bearing;
27         public double   range;
28         public double   elevation;
29
30         double sqr(double a) { return a * a; }
31
32         public static final double rad = Math.PI / 180;
33         public static final double earth_radius = 6371.2 * 1000;        /* in meters */
34
35         public static final int BEARING_LONG = AltosConvert.BEARING_LONG;
36         public static final int BEARING_SHORT = AltosConvert.BEARING_SHORT;
37         public static final int BEARING_VOICE = AltosConvert.BEARING_VOICE;
38
39         public String bearing_words(int length) {
40                 return AltosConvert.bearing_to_words(length, bearing);
41         }
42
43         public AltosGreatCircle (double start_lat, double start_lon, double start_alt,
44                                  double end_lat, double end_lon, double end_alt) {
45                 double lat1 = rad * start_lat;
46                 double lon1 = rad * -start_lon;
47                 double lat2 = rad * end_lat;
48                 double lon2 = rad * -end_lon;
49
50                 double d_lon = lon2 - lon1;
51
52                 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
53                 double vdn = Math.sqrt(sqr(Math.cos(lat2) * Math.sin(d_lon)) +
54                                        sqr(Math.cos(lat1) * Math.sin(lat2) -
55                                            Math.sin(lat1) * Math.cos(lat2) * Math.cos(d_lon)));
56                 double vdd = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(d_lon);
57                 double d = Math.atan2(vdn,vdd);
58                 double course;
59
60                 if (Math.cos(lat1) < 1e-20) {
61                         if (lat1 > 0)
62                                 course = Math.PI;
63                         else
64                                 course = -Math.PI;
65                 } else {
66                         if (d < 1e-10)
67                                 course = 0;
68                         else
69                                 course = Math.acos((Math.sin(lat2)-Math.sin(lat1)*Math.cos(d)) /
70                                                    (Math.sin(d)*Math.cos(lat1)));
71                         if (Math.sin(lon2-lon1) > 0)
72                                 course = 2 * Math.PI-course;
73                 }
74                 distance = d * earth_radius;
75                 if (Double.isNaN(course) || Double.isInfinite(course))
76                         bearing = 0;
77                 else
78                         bearing = course * 180/Math.PI;
79
80                 double height_diff = end_alt - start_alt;
81                 range = Math.sqrt(distance * distance + height_diff * height_diff);
82                 elevation = Math.atan2(height_diff, distance) * 180 / Math.PI;
83         }
84
85         public AltosGreatCircle clone() {
86                 AltosGreatCircle n = new AltosGreatCircle();
87
88                 n.distance = distance;
89                 n.bearing = bearing;
90                 n.range = range;
91                 n.elevation = elevation;
92                 return n;
93         }
94
95         public AltosGreatCircle (double start_lat, double start_lon,
96                                  double end_lat, double end_lon) {
97                 this(start_lat, start_lon, 0, end_lat, end_lon, 0);
98         }
99
100         public AltosGreatCircle(AltosGPS start, AltosGPS end) {
101                 this(start.lat, start.lon, start.alt, end.lat, end.lon, end.alt);
102         }
103
104         public AltosGreatCircle() {
105                 distance = 0;
106                 bearing = 0;
107                 range = 0;
108                 elevation = 0;
109         }
110 }