File size: 1,613 Bytes
d9d886e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Credits to https://stackoverflow.com/a/48764436/6156700
const isRound = (num, decimalPlaces) => {
  const p = Math.pow(10, decimalPlaces)
  return Math.round(num * p) / p === num
}

const round = (num, decimalPlaces) => {
  if (isRound(num, decimalPlaces || 0)) {
    return num
  }
  const p = Math.pow(10, decimalPlaces || 0)
  const n = (num * p) * (1 + Number.EPSILON)
  return Math.round(n) / p
}

const toDegree = (rad) => {
  const pi = 3.141592653589793
  return rad * (180 / pi)
}

// Added from Adhyasta-GCS-Webview/src/helper/mathHelper.js
const calculateDistance = (lat1, lon1, alt1, lat2, lon2, alt2) => {
  const R = 6371 // Radius of the Earth in kilometers
  const lat1Rad = (lat1 * Math.PI) / 180
  const lon1Rad = (lon1 * Math.PI) / 180
  const lat2Rad = (lat2 * Math.PI) / 180
  const lon2Rad = (lon2 * Math.PI) / 180

  const dLat = lat2Rad - lat1Rad
  const dLon = lon2Rad - lon1Rad
  // const dAlt = alt2 - alt1 // Altitude difference

  const a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(lat1Rad) *
          Math.cos(lat2Rad) *
          Math.sin(dLon / 2) *
          Math.sin(dLon / 2)

  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))

  const distanceInKilometers = R * c
  const distanceInMeters = distanceInKilometers * 1000

  // Calculate the 3D distance, considering altitude
  // const altitudeDifference = Math.abs(alt2 - alt1);
  // const altitudeCorrection = Math.sqrt(
  //   Math.pow(distanceInMeters, 2) + Math.pow(altitudeDifference, 2)
  // );

  return distanceInMeters
}

module.exports = {
  round,
  toDegree,
  calculateDistance
}