问题遇到的现象和发生背景
此程序的目的是判断一个点是否在多边形图形内。
最终我希望达到输入坐标,然后程序判断true or false的效果,但是运行的时候出现了问题,求解惑。
问题相关代码,请勿粘贴截图
def is_in_poly(p, poly):
px, py = p
is_in = False
for i, corner in enumerate(poly):
next_i = i + 1 if i + 1 < len(poly) else 0
x1, y1 = corner
x2, y2 = poly[next_i]
if (x1 == px and y1 == py) or (x2 == px and y2 == py):
is_in = True
break
if min(y1, y2) < py <= max(y1, y2):
x = x1 + (py - y1) * (x2 - x1) / (y2 - y1)
if x == px:
is_in = True
break
elif x > px:
is_in = not is_in
return is_in
if __name__ == '__main__':
print('Please enter the point coordinates:')
p=input()
point=p
poly = [[1.5, 2.4], [7.1, 3.6], [3.1, 8.5], [4.2, 5.3]]
print(is_in_poly(point, poly))
运行结果及报错内容
Please enter the point coordinates:
[3.0,3.0]
Traceback (most recent call last):
File "E:/python practice/report/report.py", line 27, in <module>
print(is_in_poly(point, poly))
File "E:/python practice/report/report.py", line 3, in is_in_poly
px, py = p
ValueError: too many values to unpack (expected 2)
我的解答思路和尝试过的方法
我尝插入px,py=坐标的代码,但是也失败了。
我想要达到的结果
我希望达到输入坐标,然后程序判断true or false的效果