已知两条线
已知四个点的经纬度 (假设相交的情况)该如何计算他们相交点的经纬度?
https://blog.csdn.net/hf872914334/article/details/111590098
/*
* 线段与线段的交点
* 解线性方程组, 求线段AB与线段CD的交点坐标,如果没有交点,返回null
*/
function intersects(coords1, coords2) {
let x1 = coords1[0][0];
let y1 = coords1[0][1];
let x2 = coords1[1][0];
let y2 = coords1[1][1];
let x3 = coords2[0][0];
let y3 = coords2[0][1];
let x4 = coords2[1][0];
let y4 = coords2[1][1];
//斜率交叉相乘 k1 = (y4 - y3) / (x4 - x3) k2 = (y2 - y1) / (x2 - x1)
//k1 k2 同乘 (x4 - x3) * (x2 - x1) 并相减得到denom
let denom = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1));
// 如果分母为0 则平行或共线, 不相交
if (denom === 0) {
return null;
}
let numeA = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3));
let numeB = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3));
let uA = numeA / denom;
let uB = numeB / denom;
// 交点在线段1上,且交点也在线段2上
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
let x = x1 + (uA * (x2 - x1));
let y = y1 + (uA * (y2 - y1));
return [x, y];
}
return null;
}
intersects([[0,0],[1,1]], [[3,0],[2,1]]) //null
intersects([[0,0],[1,1]], [[3,0],[0,1]]) //[0.75, 0.75]
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
这个算起来很麻烦
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
/*
**函数 取点1点2连线与点3点4连线交点(点1经度,点1纬度,点2经度,点2纬度,点3经度,点3纬度,点4经度,点4纬度,)
**返回 交点经纬度
*/
function getPoint(jd1, wd1, jd2, wd2, jd3, wd3, jd4, wd4) {
var j1, j2, j3, j4, w1, w2, w3, w4, x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4, a1, a2, b1, b2, c1, c2;
j1 = jd1 * Math.PI / 180;
j2 = jd2 * Math.PI / 180;
j3 = jd3 * Math.PI / 180;
j4 = jd4 * Math.PI / 180;
w1 = wd1 * Math.PI / 180;
w2 = wd2 * Math.PI / 180;
w3 = wd3 * Math.PI / 180;
w4 = wd4 * Math.PI / 180;
x1 = Math.cos(j1) * Math.cos(w1);
x2 = Math.cos(j2) * Math.cos(w2);
x3 = Math.cos(j3) * Math.cos(w3);
x4 = Math.cos(j4) * Math.cos(w4);
y1 = Math.sin(j1) * Math.cos(w1);
y2 = Math.sin(j2) * Math.cos(w2);
y3 = Math.sin(j3) * Math.cos(w3);
y4 = Math.sin(j4) * Math.cos(w4);
z1 = Math.sin(w1);
z2 = Math.sin(w2);
z3 = Math.sin(w3);
z4 = Math.sin(w4);
a1 = y1 * z2 - y2 * z1;
a2 = y3 * z4 - y4 * z3;
b1 = z1 * x2 - z2 * x1;
b2 = z3 * x4 - z4 * x3;
c1 = x1 * y2 - x2 * y1;
c2 = x3 * y4 - x4 * y3;
var dx5, y5, z5;
x5 = b1 * c2 - b2 * c1;
y5 = c1 * a2 - c2 * a1;
z5 = a1 * b2 - a2 * b1;
var da, db;
da = Math.atan2(y5, x5);
db = Math.asin(z5 / Math.pow(Math.pow(x5, 2) + Math.pow(y5, 2) + Math.pow(z5, 2), 0.5));
if (Math.abs(da - j1) > Math.PI || Math.abs(da - j2) > Math.PI || Math.abs(da - j3) > Math.PI || Math.abs(da - j4) >
Math.PI || Math.abs(db - w1) > Math.PI || Math.abs(db - w2) > Math.PI || Math.abs(db - w3) > Math.PI || Math.abs(
db - w4) > Math.PI) {
if (da > 0) {
da -= Math.PI;
} else {
da += Math.PI;
}
db = -db;
}
return {
jd: da * 180 / Math.PI,
wd: db * 180 / Math.PI
};
}
console.log(getPoint(0, 0, 0, 90, 20, 10, -20, 10));
</script>
</body>
</html>