mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 18:34:02 +00:00
19 lines
709 B
JavaScript
19 lines
709 B
JavaScript
/**
|
|
* Haversine great-circle distance between two WGS-84 points.
|
|
*
|
|
* @param {number} lat1 - Latitude of point A (degrees).
|
|
* @param {number} lon1 - Longitude of point A (degrees).
|
|
* @param {number} lat2 - Latitude of point B (degrees).
|
|
* @param {number} lon2 - Longitude of point B (degrees).
|
|
* @returns {number} Distance in kilometers.
|
|
*/
|
|
export function haversineKm(lat1, lon1, lat2, lon2) {
|
|
const toRad = (value) => (value * Math.PI) / 180;
|
|
const dLat = toRad(lat2 - lat1);
|
|
const dLon = toRad(lon2 - lon1);
|
|
const a =
|
|
Math.sin(dLat / 2) ** 2 +
|
|
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
|
|
return 6371 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
}
|