5

How do you draw the curve representing the shortest distance between 2 points on a flat map of the Earth?

Of course, the line would not be a straight line because the Earth is curved. (For example, the shortest distance between 2 airports is curved.)

EDIT: THanks for all the answers guys - sorry I was slow to choose solution :/

helloworlder
  • 1,383
  • 13
  • 18
  • Do you want to model Earth as a simple sphere or as its true shape (flattened on the poles)? It's pretty easy if you simplify it to a sphere. – Michael Myers Dec 08 '09 at 16:31
  • What projection are you wanting to use? Or, could you clarify the question some? – retracile Dec 08 '09 at 16:33
  • Duplicate: http://stackoverflow.com/questions/23569/calculating-distance-between-2-cities – ire_and_curses Dec 08 '09 at 16:39
  • And some more: http://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates, http://stackoverflow.com/questions/1420045/how-to-find-distance-from-the-latitude-and-longitude-of-two-locations – ire_and_curses Dec 08 '09 at 16:41
  • @ire - I disagree that this is a dup. Those ones just want the distance, this one wants to "draw the curve", which implies to me calculating the intermediate points in order to plot them on a graphic. – Paul Tomblin Dec 08 '09 at 16:47
  • 2
    There's obviously a lot of confusion about what you are asking. Drawing the shortest path and calculating the shortest distance are two very different problems. Which do you want help with? – Donnie DeBoer Dec 08 '09 at 17:33
  • Thanks everyone! Really helpful and detailed answers - awesome – helloworlder Dec 16 '09 at 03:43

2 Answers2

12

I get this sort of information from the Aviation Formulary.

In this case:

Distance between points

The great circle distance d between two points with coordinates {lat1,lon1} and {lat2,lon2} is given by:

d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))

A mathematically equivalent formula, which is less subject to rounding error for short distances is:

d=2*asin(sqrt((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))

And

Intermediate points on a great circle

In previous sections we have found intermediate points on a great circle given either the crossing latitude or longitude. Here we find points (lat,lon) a given fraction of the distance (d) between them. Suppose the starting point is (lat1,lon1) and the final point (lat2,lon2) and we want the point a fraction f along the great circle route. f=0 is point 1. f=1 is point 2. The two points cannot be antipodal ( i.e. lat1+lat2=0 and abs(lon1-lon2)=pi) because then the route is undefined. The intermediate latitude and longitude is then given by:

    A=sin((1-f)*d)/sin(d)
    B=sin(f*d)/sin(d)
    x = A*cos(lat1)*cos(lon1) +  B*cos(lat2)*cos(lon2)
    y = A*cos(lat1)*sin(lon1) +  B*cos(lat2)*sin(lon2)
    z = A*sin(lat1)           +  B*sin(lat2)
    lat=atan2(z,sqrt(x^2+y^2))
    lon=atan2(y,x)
Pang
  • 9,564
  • 146
  • 81
  • 122
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • Paul, you said, "which is less subject to rounding error for short distances". How short a distance is this good for? I need something that will be accurate for 100M to 1000M distances. – KevinDTimm Dec 08 '09 at 16:37
  • @kevin - I was quoting the Aviation Formulary. I have no idea what he considered short, but I always use the "less subject to rounding error" version for my flight planner. – Paul Tomblin Dec 08 '09 at 16:45
  • Thanks! The intermediate points on the great cirlce is exactly what I needed. Btw , sorry for not being too clear on the question - my knowlege in this area is currently vague at best. – helloworlder Dec 16 '09 at 03:50
2

To draw the 3D shortest path between two points on Earth's surface onto a 2D map of Earth's surface, you have to know how the 3D surface of Earth was projected onto the 2D map in question. If you know the projection used, you just need to apply it to the 3D shortest path to project it onto the 2D map. If you don't know the exact projection used, but have access to it through some sort of interface (ie. input 3D surface coords -> output 2D map coords), you could sample points along the 3D surface path, generate their corresponding map points through said interface, and then approximate the projected path with line segments/bezier curves/etc. through the projected sample points.

Donnie DeBoer
  • 2,517
  • 15
  • 14