已知点pt(x,y),多边形poly,判断点pt是否在多边形内部
方法:射线法
参考http://alienryderflex.com/polygon/

js实现:


function isPointInPoly(poly, pt){
  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;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注