Advertisement
giovani-rubim

Earth math [JS]

Oct 5th, 2022
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { sqrt, sin, cos, asin, acos, PI } = Math;
  2. const sqr = (x) => x*x;
  3. const toRad = (deg) => deg/180*PI;
  4. const toDeg = (rad) => rad/PI*180;
  5. const AVG_RAD = 6371;
  6. const toEuclidean = ([ lat, lon ]) => applyLon(applyLat([ 0, 0, 1 ], lat), lon);
  7. const euclideanDist = ([ ax, ay, az ], [ bx, by, bz ]) => sqrt(
  8.     sqr(bx - ax) + sqr(by - ay) + sqr(bz - az)
  9. );
  10. const chordToGCR = (chord) => asin(chord/2)*2;
  11. const sphericalDist = (a, b) => chordToGCR(euclideanDist(
  12.     toEuclidean(a),
  13.     toEuclidean(b),
  14. ));
  15. const applyLat = ([ x, y, z ], lat) => [
  16.     x,
  17.     cos(lat)*y + sin(lat)*z,
  18.     cos(lat)*z - sin(lat)*y,
  19. ];
  20. const applyLon = ([ x, y, z ], lon) => [
  21.     cos(lon)*x + sin(lon)*z,
  22.     y,
  23.     cos(lon)*z - sin(lon)*x,
  24. ];
  25. const haversine = ([ aLat, aLon ], [ bLat, bLon ]) => acos(
  26.     cos(aLat)*cos(bLat)*cos(aLon - bLon) + sin(aLat)*sin(bLat)
  27. );
  28. const calcAzimuth = (a, b) => {
  29.     const [ lat, lon ] = a;
  30.     const [ x, y ] = applyLat(applyLon(toEuclidean(b), -lon), -lat);
  31.     const radius = sqrt(sqr(x) + sqr(y));
  32.     if (radius == 0) return 0;
  33.     const temp = acos(y/radius);
  34.     return x >= 0 ? temp : PI*2 - temp;
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement