马上注册,解锁更多高级玩法
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[JavaScript] 纯文本查看 复制代码 let 坐标点 = {
x: 1,
y: 0,
}
let 多边形区域 = [
{x: 0, y: 0},
{x: 0, y: 100},
{x: 100, y: 100},
{x: 100, y: 0},
]
logd(isPointInPolygon(坐标点, 多边形区域))
[JavaScript] 纯文本查看 复制代码 //判断点P在多边形内-射线法
function isPointInPolygon(point, polygon) {
let x = point.x,
y = point.y
let inside = false
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
let xi = polygon[i].x,
yi = polygon[i].y
let xj = polygon[j].x,
yj = polygon[j].y
let intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
if (intersect) inside = !inside
}
return inside
}
|