JavaScript实现,判断一个点是否在多边形内(简直nice,希望能帮到你)

时间:2021-07-02 16:35:19   收藏:0   阅读:0
//定义点的结构体
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);

简直nice,解决了困扰我好久的地图区域内容显示

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!