72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
import { CABLE_CONFIG, CONFIG } from "./constants.js";
|
|
import { earthSurfacePoint } from "./earth-surface.js";
|
|
|
|
export function cableSurfacePoint(latitude, longitude) {
|
|
return earthSurfacePoint(latitude, longitude, CONFIG.earthRadius + CABLE_CONFIG.line.altitudeOffset);
|
|
}
|
|
|
|
export function calculateGreatCirclePoints(
|
|
lat1,
|
|
lon1,
|
|
lat2,
|
|
lon2,
|
|
radius,
|
|
segments = 50,
|
|
) {
|
|
const points = [];
|
|
const phi1 = (lat1 * Math.PI) / 180;
|
|
const lambda1 = (lon1 * Math.PI) / 180;
|
|
const phi2 = (lat2 * Math.PI) / 180;
|
|
const lambda2 = (lon2 * Math.PI) / 180;
|
|
|
|
const dLambda = Math.min(
|
|
Math.abs(lambda2 - lambda1),
|
|
2 * Math.PI - Math.abs(lambda2 - lambda1),
|
|
);
|
|
const cosDelta =
|
|
Math.sin(phi1) * Math.sin(phi2) +
|
|
Math.cos(phi1) * Math.cos(phi2) * Math.cos(dLambda);
|
|
|
|
let delta = Math.acos(Math.max(-1, Math.min(1, cosDelta)));
|
|
|
|
if (delta < CABLE_CONFIG.line.nearPointThreshold) {
|
|
const p1 = earthSurfacePoint(lat1, lon1, radius);
|
|
const p2 = earthSurfacePoint(lat2, lon2, radius);
|
|
return [p1, p2];
|
|
}
|
|
|
|
for (let i = 0; i <= segments; i++) {
|
|
const t = i / segments;
|
|
const sinDelta = Math.sin(delta);
|
|
const A = Math.sin((1 - t) * delta) / sinDelta;
|
|
const B = Math.sin(t * delta) / sinDelta;
|
|
|
|
const x1 = Math.cos(phi1) * Math.cos(lambda1);
|
|
const y1 = Math.cos(phi1) * Math.sin(lambda1);
|
|
const z1 = Math.sin(phi1);
|
|
|
|
const x2 = Math.cos(phi2) * Math.cos(lambda2);
|
|
const y2 = Math.cos(phi2) * Math.sin(lambda2);
|
|
const z2 = Math.sin(phi2);
|
|
|
|
let x = A * x1 + B * x2;
|
|
let y = A * y1 + B * y2;
|
|
let z = A * z1 + B * z2;
|
|
|
|
const norm = Math.sqrt(x * x + y * y + z * z);
|
|
x = (x / norm) * radius;
|
|
y = (y / norm) * radius;
|
|
z = (z / norm) * radius;
|
|
|
|
const lat = (Math.asin(z / radius) * 180) / Math.PI;
|
|
let lon = (Math.atan2(y, x) * 180) / Math.PI;
|
|
|
|
if (lon > 180) lon -= 360;
|
|
if (lon < -180) lon += 360;
|
|
|
|
points.push(earthSurfacePoint(lat, lon, radius));
|
|
}
|
|
|
|
return points;
|
|
}
|