Code: Select all
EnableExplicit
#Earth_Radius = 6371.000785
Procedure.d Haversine(PlanetRadius.d, Lat1.d, Lon1.d, Lat2.d, Lon2.d)
Protected Sin_dLat.d, Sin_dLon.d, a.d
Sin_dLat = Sin(Radian(Lat2 - Lat1) / 2)
Sin_dLon = Sin(Radian(Lon2 - Lon1) / 2)
a = Sin_dLat * Sin_dLat + Sin_dLon * Sin_dLon * Cos(Radian(Lat1)) * Cos(Radian(Lat2))
ProcedureReturn PlanetRadius * 2 * ATan2(Sqr(1 - a), Sqr(a))
EndProcedure
Procedure.d Haversine2(PlanetRadius.d, Lat1.d, Lon1.d, Lat2.d, Lon2.d)
ProcedureReturn ATan2(Sqr(1 - Sin(Radian(Lat2 - Lat1) / 2) * Sin(Radian(Lat2 - Lat1) / 2) + Sin(Radian(Lon2 - Lon1) / 2) * Sin(Radian(Lon2 - Lon1) / 2) * Cos(Radian(Lat1)) * Cos(Radian(Lat2))), Sqr(Sin(Radian(Lat2 - Lat1) / 2) * Sin(Radian(Lat2 - Lat1) / 2) + Sin(Radian(Lon2 - Lon1) / 2) * Sin(Radian(Lon2 - Lon1) / 2) * Cos(Radian(Lat1)) * Cos(Radian(Lat2)))) * PlanetRadius * 2
EndProcedure
Debug Haversine(#Earth_Radius, 51.476852, -0.000500, 55.950558, -3.185556)
Debug Haversine2(#Earth_Radius, 51.476852, -0.000500, 55.950558, -3.185556)
Debug "Difference = " + StrD(Haversine(#Earth_Radius, 51.476852, -0.000500, 55.950558, -3.185556) - Haversine2(#Earth_Radius, 51.476852, -0.000500, 55.950558, -3.185556), 17)