[comp.mail.uucp] latitude/longitude

msb@sq.uucp (Mark Brader) (07/29/88)

A little while ago someone posted the following formula (in C) which
allegedly converts the latitudes and longitudes of two places to the
great circle distance between them:

>  distance = acos(cos(lat2-lat1) * cos((lon2-lon1)*cos(lat1)));

A moment's thought will show that this is obviously wrong, although it
does give the right answers for some degenerate cases.

Here is why it is obviously wrong.  Since the numbering of the points 1
and 2 is arbitrary, we should be able to exchange them in the formula.
That would give:

>  distance = acos(cos(lat1-lat2) * cos((lon1-lon2)*cos(lat2)));

Since cos(-x) = cos(x), this is the same as:

>  distance = acos(cos(lat2-lat1) * cos((lon2-lon1)*cos(lat2)));

But this is obviously different from the original formula unless lat1 and
lat2 have the same cosine.  So the formula is wrong.

Here is the correct formula.  The derivation is not particularly interesting,
but if anyone is curious, I will type it in and email it to them.

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

As the other poster said,

>  All quantities are in radians.

That is, if you are starting with latitudes and longitudes in degrees,
you first multiply them by pi/180 before computing.  To convert the
answer to a linear distance, multiply by the radius of the earth.

>  To convert the distance to kilometers, multiply by 6367.

Well, this isn't quite right.  See, if you want 4 significant figures,
you have to allow for the earth not being quite spherical, and then
the above formula isn't really quite right.  The radius of the earth
actually varies from about 3950 (6357 km) at the poles to 3963 miles
(6378 km) at the equator.  (These numbers are from memory, but within 3 km.)
If you can get by with 3 significant figures, pick an appropriate approx-
imation of the radius for the latitudes you're using, and multiply by that.

Mark Brader			"What a strange field.  Studying beings instead
SoftQuad Inc., Toronto		 of mathematics.  Could lead to recursive
utzoo!sq!msb, msb@sq.com	 problems in logic."	-- Robert L. Forward
						(The Flight of the Dragonfly)