29 lines
772 B
TypeScript
29 lines
772 B
TypeScript
import { Coordinates } from '@mt/common-types';
|
|
|
|
const EARTH_RADIUS = 6372795; // meters
|
|
|
|
export function getDistance(a: Coordinates, b: Coordinates): number {
|
|
const { PI, sin, cos, pow, sqrt, atan2 } = Math;
|
|
|
|
const aRad = {
|
|
lat: (a.lat * PI) / 180,
|
|
lon: (a.lon * PI) / 180,
|
|
};
|
|
|
|
const bRad = {
|
|
lat: (b.lat * PI) / 180,
|
|
lon: (b.lon * PI) / 180,
|
|
};
|
|
|
|
const delta = bRad.lon - aRad.lon;
|
|
|
|
// вычисления длины большого круга
|
|
const y = sqrt(
|
|
pow(cos(bRad.lat) * sin(delta), 2) +
|
|
pow(cos(aRad.lat) * sin(bRad.lat) - sin(aRad.lat) * cos(bRad.lat) * cos(delta), 2)
|
|
);
|
|
const x = sin(aRad.lat) * sin(bRad.lat) + cos(aRad.lat) * cos(bRad.lat) * cos(delta);
|
|
|
|
return +(atan2(y, x) * EARTH_RADIUS).toFixed(2);
|
|
}
|