参考这里,稍微做了一些改动。
def isInterArea(testPoint, AreaPoint):
# testPoint为待测点[x,y]或者任意凸多边形[x1, y1, ...]
# AreaPoint为按顺时针顺序的4个点[x1,y1,x2,y2,x3,y3,x4,y4]
if len(testPoint) > 2:
for i in range(0, len(testPoint), 2):
if not isInterArea(testPoint[i:i+2], AreaPoint):
return False
return True
LBPoint = AreaPoint[0:2]
LTPoint = AreaPoint[2:4]
RTPoint = AreaPoint[4:6]
RBPoint = AreaPoint[6:8]
a = (LTPoint[0]-LBPoint[0])*(testPoint[1]-LBPoint[1])-(LTPoint[1]-LBPoint[1])*(testPoint[0]-LBPoint[0])
b = (RTPoint[0]-LTPoint[0])*(testPoint[1]-LTPoint[1])-(RTPoint[1]-LTPoint[1])*(testPoint[0]-LTPoint[0])
c = (RBPoint[0]-RTPoint[0])*(testPoint[1]-RTPoint[1])-(RBPoint[1]-RTPoint[1])*(testPoint[0]-RTPoint[0])
d = (LBPoint[0]-RBPoint[0])*(testPoint[1]-RBPoint[1])-(LBPoint[1]-RBPoint[1])*(testPoint[0]-RBPoint[0])
# print(a,b,c,d)
if (a>=0 and b>=0 and c>=0 and d>=0) or (a<=0 and b<=0 and c<=0 and d<=0):
return True
else:
return False