判断点线面是否交叠包含
# 传值方式
// 点: [x,y]
// 线: [[x,y],[x,y]]
// 面: [[x,y],[x,y],[x,y]...]
1
2
3
2
3
# 1 判断相交 判断两多边形线段是否相交
function isSegmentsIntersectant(segA, segB) {//线线
const abc = (segA[0][0] - segB[0][0]) * (segA[1][1] - segB[0][1]) - (segA[0][1] - segB[0][1]) * (segA[1][0] - segB[0][0]);
const abd = (segA[0][0] - segB[1][0]) * (segA[1][1] - segB[1][1]) - (segA[0][1] - segB[1][1]) * (segA[1][0] - segB[1][0]);
if (abc * abd >= 0) {
return false;
}
const cda = (segB[0][0] - segA[0][0]) * (segB[1][1] - segA[0][1]) - (segB[0][1] - segA[0][1]) * (segB[1][0] - segA[0][0]);
const cdb = cda + abc - abd;
console.log("线段是否相交:", !(cda * cdb >= 0));
return !(cda * cdb >= 0);
}
function isPolygonsIntersectant(plyA, plyB) {//面面
for (let i = 0, il = plyA.length; i < il; i++) {
for (let j = 0, jl = plyB.length; j < jl; j++) {
const segA = [plyA[i], plyA[i === il - 1 ? 0 : i + 1]];
const segB = [plyB[j], plyB[j === jl - 1 ? 0 : j + 1]];
if (isSegmentsIntersectant(segA, segB)) {
console.log("边界相交:");
return true;
}
}
}
console.log("边界不相交:");
return false;
}
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
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
# 2 判断包含 判断点是否在另一平面图中
function pointInPolygon(point, vs) {
const x = point[0], y = point[1];
let inside = false;
for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
const xi = vs[i][0], yi = vs[i][1];
const xj = vs[j][0], yj = vs[j][1];
const intersect = ((yi > y) !== (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) {
inside = !inside;
}
}
console.log(inside);
return inside;
}
//判断两多变形是否存在点与区域的包含关系(A的点在B的区域内或B的点在A的区域内)
function isPointInPolygonBidirectional(plyA, plyB) {//面面
let [a, b] = [false, false];
a = plyA.some(item => pointInPolygon(item, plyB));
if (!a) {
b = plyB.some(item => pointInPolygon(item, plyA));
}
console.log("包含关系:", a || b);
return a || b;
}
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
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
# 3、判断多边形是否重合
function isPolygonsOverlap(plyA, plyB) {
return isPolygonsIntersectant(plyA, plyB) || isPointInPolygonBidirectional(plyA, plyB);
}
1
2
3
2
3
# 4、调用方式
const plyA = [[0, 0], [5, 0], [5, 5], [0, 5]],
plyB = [[138, 363], [202, 299], [266, 363]] ;//不相交
const isOver = isPolygonsOverlap(plyA, plyB);
1
2
3
4
2
3
4
上次更新: 2021/07/22, 09:07:42