Просмотр исходного кода

introduce some base math functions

Sven Czarnian 2 лет назад
Родитель
Сommit
b65f1d7561
3 измененных файлов с 38 добавлено и 0 удалено
  1. 9 0
      src/math/angle.ts
  2. 4 0
      src/math/index.ts
  3. 25 0
      src/math/wgs84.ts

+ 9 - 0
src/math/angle.ts

@@ -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 - 0
src/math/index.ts

@@ -0,0 +1,4 @@
+import { Angle } from './angle';
+import { WGS84 } from './wgs84';
+
+export { Angle, WGS84 };

+ 25 - 0
src/math/wgs84.ts

@@ -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));
+  }
+}