Extension talk:MathFunctions

Adding a distance calculator function
Perhaps we could add a distance calculator, which would have as inputs the longitude and latitude of two points and would output the distance along the earth's surface. It would be straight-forward to code and I think very useful. It would be helpful if it were compatible with the results returned by the geocode function. For example suppose we have Point1:        ( x1, y1) where x1 is the degrees north ( if it is south then it will be negative ) y1 is the degrees east ( if it is west, then it will be negative ) and Point2:    ( x2, y2 )

We first convert them to radians, ( x degrees = x * pi / 180 radians). We assume that the sin and cos functions, which we use below, use radians, as is the case with most implementations of those functions. We'll use capital letters for the points that have been converted to radians: ( X1, Y1 ) and ( X2, Y2)

Then the straight line distance through the earth between the two points is: D_through = R * sqrt { (cos(X1)*sin(Y1) - cos(X2)*sin(Y2))^2 + (cos(X1)*cos(Y1) - cos(X2)*cos(Y2))^2 + ( sin(X1) - sin(X2))^2 } and the distance along the surface D_surface = 2 * R * asin ( D_through / [ 2 * R ] ) where the radius of the earth R = [20,000 / pi] km ( i.e. 10,000 km from the equator to pole ). For those who have not yet got used to modern units such as km, which have only been in common usage since 1793, we could possibly also allow a conversion a to miles (1.609344 km = 1 mile ). That would require another parameter specifying the units required. Notes:
 * The mathematical functions are available in the Math Functions Extension
 * To derive the equation above, convert polar coordinates to cartesian coordinates and then use Pythagorous.
 * We're ignoring the fact that the earth is not a perfect sphere, i.e. slightly flattened at the poles.
 * We also ignore altitude.
 * So the distance quoted is the distance along the surface of the earth at sea-level.

The code in C++ might look something like:
 * This looks interesting. I might implement it somehow in the code. 16:39, 21 October 2008 (UTC)
 * Well, I've implemented it and now I'm testing it. I could post it here tomorrow. 18:23, 21 October 2008 (UTC)

That's fast Pnelnik 11:20, 22 October 2008 (UTC)