1 // polar.cxx -- routines to deal with polar math and transformations
3 // Written by Curtis Olson, started June 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
29 #include <simgear/constants.h>
31 #include "polar3d.hxx"
34 * Calculate new lon/lat given starting lon/lat, and offset radial, and
35 * distance. NOTE: starting point is specifed in radians, distance is
36 * specified in meters (and converted internally to radians)
37 * ... assumes a spherical world.
38 * @param orig specified in polar coordinates
39 * @param course offset radial
40 * @param dist offset distance
41 * @return destination point in polar coordinates
43 Point3D calc_gc_lon_lat( const Point3D& orig, double course,
47 // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
49 // lon=lon1 // endpoint a pole
51 // lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
54 // printf("calc_lon_lat() offset.theta = %.2f offset.dist = %.2f\n",
55 // offset.theta, offset.dist);
57 dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
59 result.sety( asin( sin(orig.y()) * cos(dist) +
60 cos(orig.y()) * sin(dist) * cos(course) ) );
62 if ( cos(result.y()) < SG_EPSILON ) {
63 result.setx( orig.x() ); // endpoint a pole
66 fmod(orig.x() - asin( sin(course) * sin(dist) /
68 + SGD_PI, SGD_2PI) - SGD_PI );
75 * Calculate course/dist given two spherical points.
76 * @param start starting point
77 * @param dest ending point
78 * @param course resulting course
79 * @param dist resulting distance
81 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
82 double *course, double *dist )
89 // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
90 // cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
91 double cos_start_y = cos( start.y() );
92 double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
93 double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
94 double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
95 cos_start_y * cos(dest.y()) * tmp2 * tmp2));
97 *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
101 cos(dest.y())*sin(dest.x()-start.x()),
102 cos(start.y())*sin(dest.y())-
103 sin(start.y())*cos(dest.y())*cos(dest.x()-start.x()));
105 *course = SGD_2PI-c1;
109 // We obtain the initial course, tc1, (at point 1) from point 1 to
110 // point 2 by the following. The formula fails if the initial
111 // point is a pole. We can special case this with:
113 // IF (cos(lat1) < EPS) // EPS a small number ~ machine precision
115 // tc1= pi // starting from N pole
117 // tc1= 0 // starting from S pole
121 // For starting points other than the poles:
123 // IF sin(lon2-lon1)<0
124 // tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
126 // tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
129 // if ( cos(start.y()) < SG_EPSILON ) {
130 // doing it this way saves a transcendental call
131 double sin_start_y = sin( start.y() );
132 if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
133 // EPS a small number ~ machine precision
134 if ( start.y() > 0 ) {
135 *course = SGD_PI; // starting from N pole
137 *course = 0; // starting from S pole
140 // For starting points other than the poles:
141 // double tmp3 = sin(d)*cos_start_y);
142 // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
143 // double tmp5 = acos(tmp4/tmp3);
145 // Doing this way gaurentees that the temps are
146 // not stored into memory
147 double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
148 (sin(d) * cos_start_y) );
150 // if ( sin( dest.x() - start.x() ) < 0 ) {
151 // the sin of the negative angle is just the opposite sign
152 // of the sin of the angle so tmp2 will have the opposite
153 // sign of sin( dest.x() - start.x() )
157 *course = SGD_2PI - tmp5;
166 * Calculate course/dist given two spherical points.
167 * @param start starting point
168 * @param dest ending point
169 * @param course resulting course
170 * @param dist resulting distance
172 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
173 double *course, double *dist ) {
174 // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
175 // cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
176 double tmp1 = sin( (start.y() - dest.y()) / 2 );
177 double tmp2 = sin( (start.x() - dest.x()) / 2 );
178 double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
179 cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
180 // We obtain the initial course, tc1, (at point 1) from point 1 to
181 // point 2 by the following. The formula fails if the initial
182 // point is a pole. We can special case this with:
184 // IF (cos(lat1) < EPS) // EPS a small number ~ machine precision
186 // tc1= pi // starting from N pole
188 // tc1= 0 // starting from S pole
192 // For starting points other than the poles:
194 // IF sin(lon2-lon1)<0
195 // tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
197 // tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
202 if ( cos(start.y()) < SG_EPSILON ) {
203 // EPS a small number ~ machine precision
204 if ( start.y() > 0 ) {
205 tc1 = SGD_PI; // starting from N pole
207 tc1 = 0; // starting from S pole
211 // For starting points other than the poles:
213 double tmp3 = sin(d)*cos(start.y());
214 double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
215 double tmp5 = acos(tmp4/tmp3);
216 if ( sin( dest.x() - start.x() ) < 0 ) {
219 tc1 = SGD_2PI - tmp5;
223 *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;