Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating shortest path between 2 points on a flat map of the Earth

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 :/

like image 721
helloworlder Avatar asked Dec 08 '09 16:12

helloworlder


1 Answers

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)
like image 75
Paul Tomblin Avatar answered Sep 21 '22 17:09

Paul Tomblin