【发布时间】:2018-10-01 21:59:38
【问题描述】:
我有一个点列表,我需要找出这些点是否存在于由左上角和右下角定义的矩形中。如果所有点都属于矩形,则结果为 True,否则结果为 False。
这是我的代码,但我在某处弄错了。
def Functn1(topLeft=(0,0), bottomRight=(0,0), pointList=[]):
x1 = topLeft[0]
y1 = topLeft[1]
x2 = bottomRight[0]
y2 = bottomRight[1]
xa = pointList[i][0]
ya = pointList[i][0]
xb = pointList[i][1]
yb = pointList[i][1]
lengthofList = len(pointList)
for i in range(lengthofList):
if ((xa > x1 and xb < x2) and (ya > y1 and yb < y2)):
#if ((x[i][0] > x1 and x[i][1] < x2) and (y[i][0] > y1 and y[i][1] < y2)):
#Also tried using this code but keep getting the same error
return True
else:
return False
Functn1((0,0), (5,5), [(1,1), (0,0), (5,6)]) #should get False
我收到此错误:
<ipython-input-153-669eeffdb011> in allIn(topLeft, bottomRight, pointList)
20
21 for i in range(lengthofList):
---> 22 if ((x[i][0] > x1 and x[i][1] < x2) and (y[i][0] > y1 and y[i][1] < y2)):
23 return True
24 else:
TypeError: 'int' object is not subscriptable
【问题讨论】:
-
你能澄清你的问题吗,我不太明白你想检查
pointList元组之一是否等于bottomRight和topLeft? -
为什么它应该返回
False?(1, 1)不是(0, 0)和(5, 5)内的一个点吗? -
嗨@Azhy。首先,矩形仅由 2 个点形成。在这种情况下为 (0,0) 和 (5,5)。 (1,1) 和 (0,0) 将导致此矩形为 True,但 (5,6) 在矩形之外。由于所有点都不在矩形中,我应该得到一个错误。
标签: python python-3.x coordinates rectangles