File size: 1,518 Bytes
8c93eed
7e40962
8c93eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Haversine-based geodesic interpolator
function generateGeodesicPoints(lat1, lon1, lat2, lon2, numPoints = 512) {
    /**

     * Generates a series of points along the geodesic path between two geographic coordinates, using Haversine function.

        * @returns {Array} An array of points, each represented as [latitude, longitude].

    */
    const toRad = deg => deg * Math.PI / 180;
    const toDeg = rad => rad * 180 / Math.PI;

    const1 = toRad(lat1);
    const1 = toRad(lon1);
    const2 = toRad(lat2);
    const2 = toRad(lon2);

    const 螖 = 2 * Math.asin(
        Math.sqrt(
            Math.sin((蠁2 - 蠁1) / 2) ** 2 +
            Math.cos(蠁1) * Math.cos(蠁2) *
            Math.sin((位2 - 位1) / 2) ** 2
        )
    );

    if (螖 === 0) return [[lat1, lon1]];

    const sin螖 = Math.sin(螖);

    const points = [];

    for (let i = 0; i <= numPoints; i++) {
        const f = i / numPoints;
        const A = Math.sin((1 - f) * 螖) / sin螖;
        const B = Math.sin(f * 螖) / sin螖;

        const x = A * Math.cos(蠁1) * Math.cos(位1) + B * Math.cos(蠁2) * Math.cos(位2);
        const y = A * Math.cos(蠁1) * Math.sin(位1) + B * Math.cos(蠁2) * Math.sin(位2);
        const z = A * Math.sin(蠁1) + B * Math.sin(蠁2);

        const 蠁i = Math.atan2(z, Math.sqrt(x * x + y * y));
        const 位i = Math.atan2(y, x);

        points.push([toDeg(蠁i), toDeg(位i)]);
    }

    return points;
}

export default generateGeodesicPoints;