矩形中的点全部

时间:2019-05-29 19:26:46

标签: python-3.x numpy

如果点列表落在矩形内,我试图获取所有点列表以填充布尔值true。

我试图在Jupyterlab中运行以下代码。但我不断收到以下错误:

  

TypeError:“ tuple”和“ int”的实例之间不支持“> =”

def allIn(firstCorner=(0,0), secondCorner=(0,0), pointList=[]):
   fc1,sc1=firstCorner[0],firstCorner[1]
   fc2,sc2=secondCorner[0],secondCorner[1]

   fc,sc=pointList[0],pointList[1]
   if (fc >= fc1 and fc <= fc2 and sc >= sc1 and sc <= sc2) :
       return True
   elif(fc >= fc2 and fc <= fc1 and sc >= sc2 and sc <= sc1):
       return True
   else:
       return False

print(allIn((0,0), (5,5), [(1,1), (0,0), (5,5)]))

我希望输出为allIn((0,0), (5,5), [(1,1), (0,0), (5,5)])应该返回True,但是allIn((0,0), (5,5), [(1,1), (0,0), (5,6)])应该返回FalseallIn((0,0), (5,5), [])的空列表应返回False

2 个答案:

答案 0 :(得分:0)

您的pointsList是一个元组列表。您已设置

fc,sc=pointList[0],pointList[1]

所以fc和sc是元组。

if (fc >= fc1 and fc <= fc2 and sc >= sc1 and sc <= sc2) :

您正在将fc(一个元组)与fc1(一个int)进行比较,这将引发TypeError。为了进行正确的比较,请查看pointList [0] [0],pointList [0] [1],pointList [1] [0]等。

答案 1 :(得分:0)

查看点列表中的各个点。

def allIn(firstCorner=(0,0), secondCorner=(0,0), pointList=[]):
   fc1,sc1=firstCorner[0],firstCorner[1]
   fc2,sc2=secondCorner[0],secondCorner[1]
   inside = False
   for point in pointList:
       fc,sc=point[0],point[1]
       if (fc >= fc1 and fc <= fc2 and sc >= sc1 and sc <= sc2) :
           inside = True
       elif(fc >= fc2 and fc <= fc1 and sc >= sc2 and sc <= sc1):
           inside = True
       else:
           return False
   return inside