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 Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, USA.
27 #include <simgear/constants.h>
29 #include "polar3d.hxx"
32 * Calculate new lon/lat given starting lon/lat, and offset radial, and
33 * distance. NOTE: starting point is specifed in radians, distance is
34 * specified in meters (and converted internally to radians)
35 * ... assumes a spherical world.
36 * @param orig specified in polar coordinates
37 * @param course offset radial
38 * @param dist offset distance
39 * @return destination point in polar coordinates
41 Point3D calc_gc_lon_lat( const Point3D& orig, double course,
45 // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
47 // lon=lon1 // endpoint a pole
49 // lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
52 // printf("calc_lon_lat() offset.theta = %.2f offset.dist = %.2f\n",
53 // offset.theta, offset.dist);
55 dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
57 result.sety( asin( sin(orig.y()) * cos(dist) +
58 cos(orig.y()) * sin(dist) * cos(course) ) );
60 if ( cos(result.y()) < SG_EPSILON ) {
61 result.setx( orig.x() ); // endpoint a pole
64 fmod(orig.x() - asin( sin(course) * sin(dist) /
66 + SGD_PI, SGD_2PI) - SGD_PI );
73 * Calculate course/dist given two spherical points.
74 * @param start starting point
75 * @param dest ending point
76 * @param course resulting course
77 * @param dist resulting distance
79 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
80 double *course, double *dist )
87 // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
88 // cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
89 double cos_start_y = cos( start.y() );
90 double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
91 double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
92 double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
93 cos_start_y * cos(dest.y()) * tmp2 * tmp2));
95 *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
99 cos(dest.y())*sin(dest.x()-start.x()),
100 cos(start.y())*sin(dest.y())-
101 sin(start.y())*cos(dest.y())*cos(dest.x()-start.x()));
103 *course = SGD_2PI-c1;
107 // We obtain the initial course, tc1, (at point 1) from point 1 to
108 // point 2 by the following. The formula fails if the initial
109 // point is a pole. We can special case this with:
111 // IF (cos(lat1) < EPS) // EPS a small number ~ machine precision
113 // tc1= pi // starting from N pole
115 // tc1= 0 // starting from S pole
119 // For starting points other than the poles:
121 // IF sin(lon2-lon1)<0
122 // tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
124 // tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
127 // if ( cos(start.y()) < SG_EPSILON ) {
128 // doing it this way saves a transcendental call
129 double sin_start_y = sin( start.y() );
130 if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
131 // EPS a small number ~ machine precision
132 if ( start.y() > 0 ) {
133 *course = SGD_PI; // starting from N pole
135 *course = 0; // starting from S pole
138 // For starting points other than the poles:
139 // double tmp3 = sin(d)*cos_start_y);
140 // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
141 // double tmp5 = acos(tmp4/tmp3);
143 // Doing this way gaurentees that the temps are
144 // not stored into memory
145 double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
146 (sin(d) * cos_start_y) );
148 // if ( sin( dest.x() - start.x() ) < 0 ) {
149 // the sin of the negative angle is just the opposite sign
150 // of the sin of the angle so tmp2 will have the opposite
151 // sign of sin( dest.x() - start.x() )
155 *course = SGD_2PI - tmp5;
164 * Calculate course/dist given two spherical points.
165 * @param start starting point
166 * @param dest ending point
167 * @param course resulting course
168 * @param dist resulting distance
170 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
171 double *course, double *dist ) {
172 // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
173 // cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
174 double tmp1 = sin( (start.y() - dest.y()) / 2 );
175 double tmp2 = sin( (start.x() - dest.x()) / 2 );
176 double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
177 cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
178 // We obtain the initial course, tc1, (at point 1) from point 1 to
179 // point 2 by the following. The formula fails if the initial
180 // point is a pole. We can special case this with:
182 // IF (cos(lat1) < EPS) // EPS a small number ~ machine precision
184 // tc1= pi // starting from N pole
186 // tc1= 0 // starting from S pole
190 // For starting points other than the poles:
192 // IF sin(lon2-lon1)<0
193 // tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
195 // tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
200 if ( cos(start.y()) < SG_EPSILON ) {
201 // EPS a small number ~ machine precision
202 if ( start.y() > 0 ) {
203 tc1 = SGD_PI; // starting from N pole
205 tc1 = 0; // starting from S pole
209 // For starting points other than the poles:
211 double tmp3 = sin(d)*cos(start.y());
212 double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
213 double tmp5 = acos(tmp4/tmp3);
214 if ( sin( dest.x() - start.x() ) < 0 ) {
217 tc1 = SGD_2PI - tmp5;
221 *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;