You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.isPointInPolygon = isPointInPolygon;
|
|
// 多边形的射线检测,参考:https://blog.csdn.net/WilliamSun0122/article/details/77994526
|
|
var tolerance = 1e-6;
|
|
// 三态函数,判断两个double在eps精度下的大小关系
|
|
function dcmp(x) {
|
|
if (Math.abs(x) < tolerance) {
|
|
return 0;
|
|
}
|
|
return x < 0 ? -1 : 1;
|
|
}
|
|
// 判断点Q是否在p1和p2的线段上
|
|
function onSegment(p1, p2, q) {
|
|
if ((q[0] - p1[0]) * (p2[1] - p1[1]) === (p2[0] - p1[0]) * (q[1] - p1[1]) &&
|
|
Math.min(p1[0], p2[0]) <= q[0] &&
|
|
q[0] <= Math.max(p1[0], p2[0]) &&
|
|
Math.min(p1[1], p2[1]) <= q[1] &&
|
|
q[1] <= Math.max(p1[1], p2[1])) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
// 判断点P在多边形内-射线法
|
|
function isPointInPolygon(points, x, y) {
|
|
var isHit = false;
|
|
var n = points.length;
|
|
if (n <= 2) {
|
|
// svg 中点小于 3 个时,不显示,也无法被拾取
|
|
return false;
|
|
}
|
|
for (var i = 0; i < n; i++) {
|
|
var p1 = points[i];
|
|
var p2 = points[(i + 1) % n];
|
|
if (onSegment(p1, p2, [x, y])) {
|
|
// 点在多边形一条边上
|
|
return true;
|
|
}
|
|
// 前一个判断min(p1[1],p2[1])<P.y<=max(p1[1],p2[1])
|
|
// 后一个判断被测点 在 射线与边交点 的左边
|
|
if (dcmp(p1[1] - y) > 0 !== dcmp(p2[1] - y) > 0 &&
|
|
dcmp(x - ((y - p1[1]) * (p1[0] - p2[0])) / (p1[1] - p2[1]) - p1[0]) < 0) {
|
|
isHit = !isHit;
|
|
}
|
|
}
|
|
return isHit;
|
|
}
|
|
//# sourceMappingURL=is-point-in-polygon.js.map
|