introduce some base math functions
This commit is contained in:
9
src/math/angle.ts
Normal file
9
src/math/angle.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export class Angle {
|
||||||
|
static rad2deg(radian: number): number {
|
||||||
|
return radian * (180.0 / Math.PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static deg2rad(degree: number): number {
|
||||||
|
return degree * (Math.PI / 180.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/math/index.ts
Normal file
4
src/math/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import { Angle } from './angle';
|
||||||
|
import { WGS84 } from './wgs84';
|
||||||
|
|
||||||
|
export { Angle, WGS84 };
|
||||||
25
src/math/wgs84.ts
Normal file
25
src/math/wgs84.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Angle } from './angle';
|
||||||
|
|
||||||
|
export class WGS84 {
|
||||||
|
/// @brief Calculates the Haversine distance
|
||||||
|
/// @param coordinate0 The first coordinate
|
||||||
|
/// @param coordinate1 The second coordinate
|
||||||
|
/// @return The haversine distance in metres
|
||||||
|
static distance(
|
||||||
|
coordinate0: { lat: number; lon: number },
|
||||||
|
coordinate1: { lat: number; lon: number },
|
||||||
|
): number {
|
||||||
|
const lat0 = Angle.deg2rad(coordinate0.lat);
|
||||||
|
const lon0 = Angle.deg2rad(coordinate0.lon);
|
||||||
|
const lat1 = Angle.deg2rad(coordinate1.lat);
|
||||||
|
const lon1 = Angle.deg2rad(coordinate1.lon);
|
||||||
|
const earthRadius = 6371000;
|
||||||
|
|
||||||
|
const a =
|
||||||
|
0.5 -
|
||||||
|
Math.cos(lat1 - lat0) * 0.5 +
|
||||||
|
Math.cos(lat0) * Math.cos(lat1) * (1 - Math.cos(lon1 - lon0) * 0.5);
|
||||||
|
|
||||||
|
return 2 * earthRadius * Math.asin(Math.sqrt(a));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user