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 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, ( assuming the sin and cos functions use radians, as is normally the case ) In radians we'll use capital letters ( X1, Y1 ) and ( X2, Y2)

Then the straight line distance through the earth 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 over 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. Note: 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. Pnelnik 11:39, 21 October 2008 (UTC)