{"ScriptPreparationCode":" var TO_RAD = Math.PI / 180;\r\n \r\n function toRad(deg) {\r\n return deg * TO_RAD;\r\n }\r\n \r\n var coords = new Array();\r\n \r\n function randomLat() {\r\n return Math.random() * 180 - 90;\r\n }\r\n \r\n function randomLong() {\r\n return Math.random() * 360 - 180;\r\n }\r\n \r\n for (var i = 0; i \u003C 5000; i\u002B\u002B) {\r\n coords.push([randomLat(), randomLong(), randomLat(), randomLong()]);\r\n }\r\n \r\n function compute(coord, f) {\r\n return f(coord[0], coord[1], coord[2], coord[3]);\r\n }\r\n \r\n function test(f) {\r\n for (var i = 0; i \u003C coords.length; i\u002B\u002B) {\r\n var coord = coords[i];\r\n compute(coord, f);\r\n }\r\n }\r\n \r\n var R = 6371009; // m\r\n var R2 = 6371009 * 2; // m\r\n \r\n function distance1(lat1, lon1, lat2, lon2) {\r\n var R = 6371009; // m\r\n var dLat = toRad(lat2 - lat1);\r\n var dLon = toRad(lon2 - lon1);\r\n \r\n var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) \u002B Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);\r\n var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\r\n return R * c;\r\n }\r\n \r\n function distance2(lat1, lon1, lat2, lon2) {\r\n var aLat = toRad(lat1);\r\n var bLat = toRad(lat2);\r\n var dLat2 = (bLat - aLat) / 2;\r\n var dLon2 = toRad(lon2 - lon1) / 2;\r\n var x = Math.sin(dLat2) * Math.sin(dLat2) \u002B Math.cos(aLat) * Math.cos(bLat) * Math.sin(dLon2) * Math.sin(dLon2);\r\n return R * 2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x));\r\n }\r\n \r\n function distance3(lat1, lon1, lat2, lon2) {\r\n var aLat = toRad(lat1);\r\n var bLat = toRad(lat2);\r\n var dLat2 = (bLat - aLat) * 0.5;\r\n var dLon2 = toRad(lon2 - lon1) * 0.5;\r\n var sindLat = Math.sin(dLat2);\r\n var sindLon = Math.sin(dLon2);\r\n var x = sindLat * sindLat \u002B Math.cos(aLat) * Math.cos(bLat) * sindLon * sindLon;\r\n return R2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x));\r\n }\r\n \r\nfunction getDistance(lat1, lon1, lat2, lon2) {\r\n const TO_RAD2 = Math.PI / 180;\r\n\r\n lat1 = lat1 * TO_RAD2;\r\n lat2 = lat2 * TO_RAD2;\r\n var sinDeltaLat = (lat2 - lat1) * 0.5;\r\n var sinDeltaLon = (lon2 - lon1) * TO_RAD2 * 0.5;\r\n\r\n var a = sinDeltaLat * sinDeltaLat \u002B Math.cos(lat1) * Math.cos(lat2) * sinDeltaLon * sinDeltaLon;\r\n\r\n // Return distance in meters\r\n return 12742000 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\r\n}\r\n function distance4(lat1, lon1, lat2, lon2) {\r\n var aLat = lat1 * TO_RAD;\r\n var bLat = lat2 * TO_RAD;\r\n var dLat2 = (bLat - aLat) * 0.5;\r\n var dLon2 = (lon2 - lon1) * TO_RAD * 0.5;\r\n var sindLat = Math.sin(dLat2);\r\n var sindLon = Math.sin(dLon2);\r\n var x = sindLat * sindLat \u002B Math.cos(aLat) * Math.cos(bLat) * sindLon * sindLon;\r\n return R2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x));\r\n }\r\n \r\n function distance5(lat1, lon1, lat2, lon2) {\r\n var R = 6371009; // m\r\n var dLat = toRad(lat2 - lat1);\r\n var dLon = toRad(lon2 - lon1);\r\n \r\n var a = Math.pow(Math.sin(dLat / 2),2) \u002B Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.pow(Math.sin(dLon / 2), 2);\r\n var c = 2 * Math.asin(Math.sqrt(a));\r\n return R * c;\r\n }","TestCases":[{"Name":"Distance1","Code":"test(distance1)","IsDeferred":false},{"Name":"Distance2","Code":"test(distance2)","IsDeferred":false},{"Name":"Distance3","Code":"test(distance3)","IsDeferred":false},{"Name":"Distance4","Code":"test(distance4)","IsDeferred":false},{"Name":"Distance5","Code":"test(distance5)","IsDeferred":false},{"Name":"Distance6","Code":"test(getDistance)","IsDeferred":false}]}