mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
71b7b209dc
TravelElements ramped how far a leg bows by a local km helper and roadRoute refused long legs by a second copy of it. Both now call legKm from legKm.ts, the same formula with the same clamp.
15 lines
685 B
TypeScript
15 lines
685 B
TypeScript
/**
|
|
* Kilometres between two coordinates, along the great circle.
|
|
*
|
|
* Shared by the map element, which ramps how far a leg bows by it, and by the
|
|
* road fetch, which refuses to route a leg past its ceiling. The clamp keeps
|
|
* `asin` defined when rounding pushes two near antipodal points a hair past 1.
|
|
*/
|
|
export function legKm(a: { lat: number; lng: number }, b: { lat: number; lng: number }): number {
|
|
const r = (d: number) => (d * Math.PI) / 180;
|
|
const dLat = r(b.lat - a.lat);
|
|
const dLng = r(b.lng - a.lng);
|
|
const h = Math.sin(dLat / 2) ** 2 + Math.cos(r(a.lat)) * Math.cos(r(b.lat)) * Math.sin(dLng / 2) ** 2;
|
|
return 12742 * Math.asin(Math.min(1, Math.sqrt(h)));
|
|
}
|