JavaScript实现,判断一个点是否在多边形内

//定义点的结构体
function point(){
 this.x=0;
 this.y=0;
}
 
//计算一个点是否在多边形里,参数:点,多边形数组 
function PointInPoly(pt, poly) { 
    for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) 
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) 
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x) 
        && (c = !c); 
    return c; 
}
 
 
 
//调用
 
var pt=new point();
pt.x=34.32812720562833;
pt.y=108.93115997314453;
//这里的下x,y坐标是多边形的顺时针的拐角节点来排序的
var _poly=[{x:1.1,y:1.1},{x:3,y:1},{x:6,y:4},{x:2,y:10},{x:1.1,y:1.1}];
 
 
var xx=PointInPoly(pt,_poly);
alert(xx);

原文链接:https://blog.csdn.net/tapist_Fps/article/details/121918541

 

posted @ 2023-07-19 15:30  襄阳老兵  阅读(179)  评论(0编辑  收藏  举报