@orca-fe/x-map
Version:
68 lines (67 loc) • 2.29 kB
JavaScript
export const getCPoints = (p1, p2, alpha) => {
const [x1, y1] = p1;
const [x2, y2] = p2;
const centerX = (x1 + x2) / 2;
const centerY = (y1 + y2) / 2;
const distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
const a = distance / alpha;
const atan = Math.atan((y2 - y1) / (x2 - x1));
let deg = 0.5;
if (x2 - x1 < 0)
deg = 1.5;
return {
radii: a,
distance,
center: [centerX, centerY],
cp: [centerX + a * Math.cos(Math.PI * deg + atan), centerY + a * Math.sin(Math.PI * deg + atan)],
};
};
export const calcQuadBezierCurve = (a, c, b, d) => {
const e = 1 - d;
return e * e * a + 2 * e * d * c + d * d * b;
};
/**
* 计算曲线某一刻坐标位置
* @param p1 起始点
* @param p2 中间点
* @param p3 结束点
* @param alpha 百分比
* @returns {{x, y: *}}
*/
export const calcQuadBezierCurvePoint = (p1, p2, p3, alpha) => [
calcQuadBezierCurve(p1[0], p2[0], p3[0], alpha),
calcQuadBezierCurve(p1[1], p2[1], p3[1], alpha),
];
export const calcBezierCPHeight = (minHeight, maxHeight, num, allowNegative) => {
/* 需要生成高度位置的数量 */
const n = allowNegative ? (num + 1) / 2 : num;
const res = [];
const min = Math.min(minHeight, maxHeight);
const step = (Math.max(minHeight, maxHeight) - min) / (n + 1);
for (let i = 1; i <= num; i += 1) {
res.push(min + step * i);
if (allowNegative)
res.push((min + step * i) * -1);
}
return res;
};
/**
* caculate the great circle distance
* 计算地球面上两个点之间的距离
*/
export const getGreatCircleDistance = (p1, p2) => {
const [lng1, lat1] = p1;
const [lng2, lat2] = p2;
const EARTH_RADIUS = 6378137.0; // 单位M
function getRad(d) {
return (d * Math.PI) / 180.0;
}
const radLat1 = getRad(lat1);
const radLat2 = getRad(lat2);
const a = radLat1 - radLat2;
const b = getRad(lng1) - getRad(lng2);
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s *= EARTH_RADIUS;
s = Math.round(s * 10000) / 10000.0;
return s;
};