Movement on a Sphere, Charted on a Spreadsheet
Date: 11/20/2010 at 11:21:37 From: Alan Subject: traversing a sphere given specific inputs I have a sphere to traverse. I start at a given latitude and longitude. I face a given angle relative to north. I travel forward a given distance, measured in a value relative to revolutions of the sphere. How do I determine my new position and orientation? For example, I start at a latitude halfway between the equator and the north pole, and a longitude of 0. I start facing northeast (pi * 0.25) and I travel forward half a revolution. I now need to determine my new latitude, longitude, and angle relative to north given the 4 values mentioned above. The radius of the sphere is irrelevant in this case. I don't know how to construct the equation. I initially used this approximation: end_latitude = start_latitude + (sin(angle) * distance) end_longitude = start_longitude + (cos(angle) * distance) But this only works well near the equator. It might be different in crucial ways, but my problem seems similar to this problem: http://mathforum.org/library/drmath/view/51816.html With this in mind, I used Doctor Rick's (hopefully better!) equation to create a speadsheet showing my approximation. The answers seem wrong, though; for example, traveling no distance results in the end position being different from the starting position.
Date: 11/23/2010 at 01:34:08 From: Doctor Vogler Subject: Re: traversing a sphere given specific inputs Hi Alan, Thanks for writing to Dr. Math. Have you already seen our section on latitude and longitude? http://mathforum.org/library/drmath/sets/select/dm_lat_long.html The formula that gives the distance between two points is found at http://mathforum.org/library/drmath/view/51722.html http://mathforum.org/library/drmath/view/51879.html The formula for the bearing can be found at http://mathforum.org/library/drmath/view/55417.html The result is this. We use these variables: lat1 = starting latitude lon1 = starting longitude lat2 = latitude at the destination lon2 = longitude at the destination d = distance divided by the radius of the sphere b = bearing (angle from north at the starting point) Then the distance (haversine) formula is cos(d) = cos(lat1)*cos(lat2)*cos(lon1 - lon2) + sin(lat1)*sin(lat2) The bearing formula is sin(lat2) - sin(lat1)*cos(d) cos(b) = ---------------------------- sin(d)*cos(lat1) So you can solve the second equation for sin(lat2) ... sin(lat2) = cos(b)*sin(d)*cos(lat1) + sin(lat1)*cos(d) ... and then solve the first equation for cos(lon1 - lon2): cos(d) - sin(lat1)*sin(lat2) cos(lon1 - lon2) = ---------------------------- cos(lat1)*cos(lat2) Take an inverse sine to determine lat2, and then take an inverse cosine to determine lon1 - lon2, and subtract from lon1 to get lon2. In your example, you have lat1 = 45 degrees = pi/4 radians lon1 = 0 b = 45 degrees = pi/4 radians d = pi radians (half a revolution, or pi*radius) Then cos(b) = sin(b) = cos(lat1) = sin(lat1) = sqrt(2)/2 sin(d) = 0 cos(d) = -1 So sin(lat2) = cos(b)*sin(d)*cos(lat1) + sin(lat1)*cos(d) = -sqrt(2)/2 This means that lat2 = -pi/4 (since latitudes are always between -pi and +pi, the inverse sine is unambiguous). Then we compute cos(d) - sin(lat1)*sin(lat2) cos(lon1 - lon2) = ---------------------------- = -1 cos(lat1)*cos(lat2) This means that lon1 - lon2 = pi, and since lon1 = 0, that means that lon2 = pi. In this case, since +pi and -pi are the same angle, the inverse cosine is also unambiguous. More often, though, you won't be able to tell the sign of the angle (lon1 - lon2) from this formula, because the distance is indifferent to the sign of (lon1 - lon2). I'll leave it to you to figure out how to use the bearing to determine the sign of (lon1 - lon2). Basically, you have to list a few cases when (lon1 - lon2) is between either -pi and 0, or 0 and +pi. If you have any questions about this or need more help, please write back and show me what you have been able to do, and I will try to offer further suggestions. - Doctor Vogler, The Math Forum http://mathforum.org/dr.math/
Date: 11/23/2010 at 20:18:41 From: Alan Subject: traversing a sphere given specific inputs Thanks. This mostly looks great. Your help so far is greatly appreciated. I have an Excel file now that shows your equation, the inital values, the final values, and some intermediate ones to indicate the path. They are plotted on a graph. A large dot marks the start of the path. If latitudes are between -pi and pi, then how is halfway between the equator and north pole pi/4? Shouldn't it be pi/2? Trying to start at pi/2 latitude produces strange results, so I used pi/4 in my example. Longitude appears to range between -pi and 0. Is this correct? It's not possible to go east at all as setting any easterly course seems to cause it to reflect. For example, 0.5 goes in the same direction as -0.5. Traveling one rotation causes the path to leave one side of the longitudes; but instead of returning onto the other, it reflects and returns to the start the wrong way.
Date: 11/23/2010 at 22:42:11 From: Doctor Vogler Subject: Re: traversing a sphere given specific inputs Hi Alan, Thanks for writing to Dr. Math. You are right: I should have said that latitudes range from -pi/2 (at the south pole) to +pi/2 (at the north pole), not from -pi to +pi. To you to figure out when (lon1 - lon2) is between -pi and 0, and when it's between 0 and +pi, start by taking (lon1 - lon2) to be between -pi and 0 if both d and b are between 0 and +pi or neither of them. Take (lon1 - lon2) to be between 0 and +pi if one of d and b is between 0 and +pi, and the other is not. - Doctor Vogler, The Math Forum http://mathforum.org/dr.math/
Date: 11/24/2010 at 12:01:46 From: Doctor Vogler Subject: Re: traversing a sphere given specific inputs Hi Alan, I should add to my previous response that, when I say that d (or b) is between 0 and pi, what I mean is that its sine is positive. Similarly, when I said "not between 0 and pi," it would have been more accurate to say that its sine is negative. When the sine is zero, then you have some more special cases. When d = pi, then you'll always get cos(lon1 - lon2) = -1, which is unambiguous. When b = pi or b = 0, then you have this issue where the longitude doesn't change until you go around the north/south pole. So sin(lat2) = sin(lat1 - d), And while you might take this to mean that lat2 = lat1 - d, this will sometimes break the rule that -pi/2 <= lat2 <= pi/2. It does so precisely when you have to change lon2 from lon1 to lon1 + pi. - Doctor Vogler, The Math Forum http://mathforum.org/dr.math/
Date: 11/24/2010 at 12:34:13 From: Alan Subject: traversing a sphere given specific inputs I forgot to mention before that, where you said d is "distance divided by the radius of the sphere," I used 1 as the radius of the sphere, as I am working in revolutions rather than units of surface distance, which would change with sphere size. I was unsure about this, however it seems to have worked fine. I implemented in my spreadsheet the part where if both d and b are between 0 and +pi, or neither of them, etc. I didn't mean to miss out this bit earlier. The maths in this is quite heavy going for me. I had to use Wolfram|Alpha to rearange the earlier equations as I wouldn't have known where to start in doing so myself with such complicated equations. The d and b fix seemed to work well and shows the correct results, fixing both problems. It was still buggy where the inital longitude was other than 0, but I figured out how to correct that myself. The correction is probably not the neatest, but it's not bad -- and it works. Provided I haven't missed something major, this all seems to be working. The last piece of information that I need is the final heading (the heading afterwards). How is this calculated? I am happy to provide the latest version of the spreadsheet to anyone else with a similar problem. This is the end of a long line of trying to find a solution, and I wouldn't want it to take anyone else so long.
Date: 11/24/2010 at 21:22:56 From: Doctor Vogler Subject: Re: traversing a sphere given specific inputs Hi Alan, If I understand you correctly, you are asking what the bearing has become after moving the distance d in a "straight" line (i.e., along a great circle), since in general the bearing changes as you move. For that question, you just consider going backwards. If you switch (lon1, lat1) with (lon2, lat2), then you are talking about going in the opposite direction, with the bearing calculated at the other point. So if you add (or subtract) pi to that bearing, then you will get the bearing moving away from your starting point. In other words, you have lat1 = starting latitude lon1 = starting longitude lat2 = latitude at the destination lon2 = longitude at the destination d = distance divided by the radius of the sphere b = bearing (angle from north at the starting point) b' = bearing at end (looking away from start, at ending point) The formulas I already gave you remain cos(d) = cos(lat1)*cos(lat2)*cos(lon1 - lon2) + sin(lat1)*sin(lat2) sin(lat2) = cos(b)*sin(d)*cos(lat1) + sin(lat1)*cos(d) The new one, gotten by swapping the two points and adding pi to the bearing, is sin(lat1) = -cos(b')*sin(d)*cos(lat2) + sin(lat2)*cos(d) Solving for b', sin(lat2)*cos(d) - sin(lat1) cos(b') = ---------------------------- sin(d)*cos(lat2) Then you take the inverse cosine. Of course, now you have the same issues about b' as before. The cosine is unaffected by adding pi to b', so you have to figure out whether b' is between 0 and +pi, or between -pi and 0. It seems to me that b' will be between 0 and pi (i.e., sin(b) > 0) if and only if b is, since it's just a question of whether you are walking east or walking west. (Of course, if you are going due north or due south, then you have special cases, as before.) - Doctor Vogler, The Math Forum http://mathforum.org/dr.math/
Date: 11/25/2010 at 08:48:17 From: Alan Subject: traversing a sphere given specific inputs I changed the part about d or b being between 0 and pi to instead ask if its sine (as in sine wave) is positive. (At first, I wasn't sure if you meant "sign" as in + and - or "sine" as in the trigonometric function.) This looks neater and performs the same. I also implemented the equation you gave me for the final bearing and included the compensation for eastwards and westwards traveling. From what I can tell, the values that produce errors are to do with performing arccos on certain numbers, and occur when the distance to travel is a multiple of pi. I will probably solve this by adding a very small number where the distance is a multiple of pi. Moving north or south doesn't work well for any output but is very simple, so when I come to code it soon, I will include a check and a simpler algorithm to use in those cases. Otherwise, results seem correct! This should all be correct now? If so, thanks again for the help!
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.