Optimisations + lisibilité

This commit is contained in:
Sébastien Rivière
2025-06-18 02:21:32 +02:00
parent c6066bc234
commit 4fd73a35c8
9 changed files with 153 additions and 187 deletions

View File

@@ -1,3 +1,12 @@
/**
* Convert a angle from degree to radian
* @param {Number} deg angle in degree
* @returns angle in radian
*/
function degToRad(deg) {
return deg * (Math.PI / 180);
}
/**
* Compute the distance between two points givent their longitude and latitude
* @param {Object} pos1 The first position
@@ -5,13 +14,13 @@
* @returns the distance between the two positions in meters
* @see https://gist.github.com/miguelmota/10076960
*/
export function getDistanceFromLatLon({ lat: lat1, lng: lon1 }, { lat: lat2, lng: lon2 }) {
function getDistanceFromLatLon({ lat: lat1, lng: lon1 }, { lat: lat2, lng: lon2 }) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var dLat = degToRad(lat2 - lat1);
var dLon = degToRad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.cos(degToRad(lat1)) * Math.cos(degToRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
@@ -19,15 +28,6 @@ export function getDistanceFromLatLon({ lat: lat1, lng: lon1 }, { lat: lat2, lng
return d * 1000;
}
/**
* Convert a angle from degree to radian
* @param {Number} deg angle in degree
* @returns angle in radian
*/
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
/**
* Check if a GPS point is in a circle
* @param {Object} position The position to check, an object with lat and lng fields
@@ -37,4 +37,4 @@ function deg2rad(deg) {
*/
export function isInCircle(position, center, radius) {
return getDistanceFromLatLon(position, center) < radius;
}
}