点在多边形内部判断

引用https://blog.csdn.net/hjh2005/article/details/9246967

添加位于顶点和多边形边线上的判断

# 点在多边形内部判断

def pointInPolygon(polypoints, point):

oddNodes = False

polySides = len(polypoints)

x, y = point[0], point[1]

j = polySides - 1

for i in range(polySides):

pointi = polypoints[i]

Xi, Yi = pointi[0], pointi[1]

pointj = polypoints[j]

Xj, Yj = pointj[0], pointj[1]

# 位于多边形顶点

if Xi==x and Yi==y :return True

if Xj==x and Yj==y :return True

# 位于相邻顶点的边上

dij= math.sqrt((Xj-Xi)**2+(Yj-Yi)**2)

dip= math.sqrt((Xj-x)**2+(Yj-y)**2)

djp= math.sqrt((Xi-x)**2+(Yi-y)**2)

if dij==(dip+djp):  return True

# 射线与多边形边相交奇数个点则在内部

if ((Yi < y and Yj >= y) or

(Yj < y and Yi >= y)) and (Xi <= x or Xj <= x):

if (Xi + (y - Yi) / Yj - Yi) * (Xj - Xi) < x:

oddNodes = not oddNodes

j = i

return oddNodes

(0)

相关推荐