Bearing Between Two PointsDate: 12/19/2001 at 20:32:39 From: Doug Subject: Latitude/Longitude calculations I have found numerous solutions for finding the distance between two Lat/Long points on the earth (including the Haversine Formula), but I can't seem to find a reference that shows how to also calculate the direction between those points. At first blush it seemed easy but there appears to be something I'm missing. Is there an easy way to calculate the "heading" (relative to North = 0)between two coordinates? Thanks. Doug Date: 12/20/2001 at 16:37:08 From: Doctor Rick Subject: Re: Latitude/Longitude calculations Hi, Doug. My favorite web reference for navigational formulas is this: Aviation Formulary V1.33, by Ed Williams http://williams.best.vwh.net/avform.htm The algorithm it gives for bearing (or course) between two points is this: tc1=mod(atan2(sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)), 2*pi) The formula gives the *initial* heading for a great-circle route from point A to point B. The heading will change in the course of the trip. The quantities in the formula have these meanings: lon1 = longitude of point A lat1 = latitude of point A lon2 = longitude of point B lat2 = latitude of point B tc1 = direction of point B from point A (angle east of north) pi = 3.141596... NOTE: The introduction to the Aviation Formulary page says this: "For the convenience of North Americans I will take North latitudes and West longitudes as positive and South and East negative. The longitude is the opposite of the usual mathematical convention." The algorithm as I show it above has been modified to use the usual convention that east longitudes are positive. In the formula, atan2(y,x) is the arctangent, or inverse tangent, of y/x, with the additional feature of deciding which quadrant the angle belongs in, based on the signs of x and y. This is common in computer programming languages. Also, mod(a,2*pi) is the remainder you get when you divide a by 2*pi; that is, subtract the largest multiple of 2*pi less than a from a, and that's the answer. I'll restate the algorithm in a form that makes the quadrant decision explicit (and also covers the cases for dlat and dlon equal to zero), in case you don't have access to an atan2 function. dlat = lat2 - lat1 dlon = lon2 - lon1 y = sin(lon2-lon1)*cos(lat2) x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1) if y > 0 then if x > 0 then tc1 = arctan(y/x) if x < 0 then tc1 = 180 - arctan(-y/x) if x = 0 then tc1 = 90 if y < 0 then if x > 0 then tc1 = -arctan(-y/x) if x < 0 then tc1 = arctan(y/x)-180 if x = 0 then tc1 = 270 if y = 0 then if x > 0 then tc1 = 0 if x < 0 then tc1 = 180 if x = 0 then [the 2 points are the same] Thanks to Kalpana who wrote in and helped me correct the original version of this algorithm. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/ Date: 12/20/2001 at 22:47:05 From: Doug Subject: Latitude/Longitude calculations Thanks very much Dr. Rick. I will try the code you've suggested and let you know the result. For your information, it's to be used to calculate current speed and direction from an Acoustic Doppler Current Profiler. Normally the instrument utilizes the ship's gyro to determine direction, but it failed, and I need to use GPS positions at 2-minute intervals to determine the ship's direction. The current speed and direction relative to the ship can then be rotated to real- world coordinates... Doug Date: 06/15/2002 at 12:18:04 From: Hurle Subject: Your formula for bearing I know how to derive the formula sin(lat2) - sin(lat1)*cos(d) Bearing = acos ---------------------------- sin(d) * cos(lat1) But how did you derive the alternative formula (which does not require the pre-computation of the distance, d) that appears in your archive? Thank you. Hurle of Phoenix (actually "Surprise") Arizona Date: 06/19/2002 at 08:45:23 From: Doctor Rick Subject: Re: Your formula for bearing Hi, Hurle. I've wanted to get a derivation of the bearing formula into the Archives, but until now our correspondents have been content to get the formula without knowing why it works. Thanks for asking, and motivating me to do the work! Let's define three unit vectors, each in the direction of the line from the center of the earth to a point on the surface: N in the direction of the north pole, A in the direction of the initial point, and B in the direction of the final point on the course. Then the bearing we seek is the angle between the plane containing N and A, and the plane containing A and B. Thus it equals the angle between vectors perpendicular to these planes, namely, NxA and BxA. Let point A have latitude lat1 and longitude 0 (we can rotate our coordinate system so this is true), and let point B have latitude lat2 and longitude dlon (the difference between the actual longitudes of A and B). Then we can calculate NxA and BxA: N = (0, 0, 1) A = (cos(lat1), 0, sin(lat1)) B = (cos(lat2)*cos(dlon), cos(lat2)*sin(dlon), sin(lat2)) NxA = (0, cos(lat1), 0) BxA = (sin(lat1)*cos(lat2)*sin(dlon), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlon), -cos(lat1)*cos(lat2)*sin(dlon)) The usual way to find the angle would be to take the dot product of these vectors, divide by the product of the magnitudes of the vectors, and take the arccosine. But that would be quite a mess. Instead we can take advantage of the fact that NxA is parallel to the y axis. The tangent of the angle between BxA and the y axis is the component of BxA in the x-z plane (the square root of the sum of the squares of the x and z components) divided by the y component: tan(theta) = sqrt((sin(lat1)*cos(lat2)*sin(dlon))^2 + (-cos(lat1)*cos(lat2)*sin(dlon))^2) / (cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlon)) The numerator simplifies to sqrt(cos(lat2)^2 * sin(dlon)^2) * sqrt(sin(lat1)^2 + cos(lat1)^2) = cos(lat2)*sin(dlon) Thus tan(theta) = cos(lat2)*sin(dlon) / (cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlon)) That's the formula. If we take the arctan of both sides, we get a value in the range from -pi/2 to pi/2 radians. The problem is, this doesn't distinguish between opposite directions, NE vs. SW for instance. But the function atan2(y,x) returns the arctan of y/x with adjustments for the signs of x and y so that the angle returned is the angle of the cartesian point (x,y) in polar coordinates -- just what we need here. The only problem is that it returns a value in the range (-pi, pi]. The function mod(..., 2*pi) moves negative angles up to the range (pi, 2*pi). To get a final answer in degrees (0 to 360), you must multiply by 180/pi. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/ Date: 06/19/2002 at 19:32:37 From: Hurle Subject: Your formula for bearing Thank you very much. I have not found this derivation anywhere else -- you are a wizard, I don't care what the other Math Doctors say about you! Why is this used over the simpler version? Date: 06/19/2002 at 20:44:33 From: Doctor Rick Subject: Re: Your formula for bearing Hi, Hurle. I believe you're referring to the first version given here: Aviation Formulary V1.33, by Ed Williams http://williams.best.vwh.net/avform.htm#Crs That version is only simpler if you don't count the calculations you had to do to find the distance between the points, which that version uses. Thus that version is better if you need to find the distance as well as the bearing, but if you only needed the bearing, then the formula I derived would be simpler. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/ Date: 06/20/2002 at 09:25:01 From: Hurle Subject: Thank you (Your formula for bearing) You're right. Thanks once again. Hurle Date: 01/31/2006 at 13:52:18 From: Bruce Subject: spherical trigonometrical bearings for a dumb pilot! Hello! I am a rather unlearned (when compared to others on this site) pilot from South Africa. I am trying to write an Excel sheet that calculates great circle distances and bearings in the context of a logcard. The distance part I got right (as backed up by my helicopter's GPS system), while bearings I just cannot hack. As I am in the Southern and Eastern Hemisphere, that might be causing the problem. Also, I don't know what distance to insert in the formulas (i.e. nm, degrees, etc) and whether to use degrees or radians. I keep getting answers like 708 or 1.257777 which don't equate to bearings. Please help me with an idiot-proof formula, if possible relating to Excel, that I can try out. Thank you in advance!!! Date: 01/31/2006 at 20:04:12 From: Doctor Rick Subject: Re: spherical trigonometrical bearings for a dumb pilot! Hi, Bruce. The main formula discussed on this page does not require input of the distance between points. (There is a link to a different formula, that does require that input, near the bottom of the page.) tc1 = mod(atan2(sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)), 2*pi) A spreadsheet version of this formula is: BearingRad: =MOD(ATAN2(COS(LatA)*SIN(LatB)-SIN(LatA)*COS(LatB) *COS(LongB-LongA), SIN(LongB-LongA)*COS(LatB)), 2*PI()) where I have defined cells containing formulas as follows: LatA: =lat1*pi()/180 LongA: =lon1*pi()/180 LatB: =lat2*pi()/180 LongB: =lon2*pi()/180 and lat1, lon2, lat2, and lon2 are cells containing the coordinates in decimal degrees. It appears that your coordinates are in degrees and decimal minutes, so you'll need to convert them to decimal degrees using formulas like this one: lat1: =lat1deg+lat1min/60*SIGN(lat1deg) The BearingRad formula above gives the bearing in radians. You'll need to convert that to degrees east of north with a formula like this: Bearing: =BearingRad*180/PI() I took these formulas from my own spreadsheet for calculating bearings. If this isn't enough to straighten out your problems, let me know what you do and what you get, so I can check it out. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/ Date: 02/05/2006 at 10:38:17 From: Bruce Subject: Thank you (spherical trigonometrical bearings for a dumb pilot!) Thank you so much Doctor Rick! You are a legend - you really put it so well and in terms that I, the maths wannabe, could understand. Keep up the good work and know you have a loyal fan from the southern hemisphere!!! Bruce |
Search the Dr. Math Library: |
[Privacy Policy] [Terms of Use]
Ask Dr. Math^{TM}
© 1994-2015 The Math Forum
http://mathforum.org/dr.math/