Home > Mobile >  Python: check if all points within a rectangle
Python: check if all points within a rectangle

Time:02-21

I wrote below function to check if the list of points is within a rectangle, and it should return True only if all points in the list are in the rectangle.

def allIn(firstCorner=(0,0), secondCorner=(0,0), pointList=[]):
    if len(pointList) == 0:
        return False
    for i in range(0, len(pointList)):
        if (secondCorner[0]>=pointList[i][0]>=firstCorner[0] and secondCorner[1]>=pointList[i][1]>=firstCorner[1]):
            return True
        elif (secondCorner[0]>=pointList[i][0]>=firstCorner[0] and firstCorner[1]>=pointList[i][1]>=secondCorner[1]):
            return True
        elif (firstCorner[0]>=pointList[i][0]>=secondCorner[0] and secondCorner[1]>=pointList[i][1]>=firstCorner[1]):
            return True
        elif (firstCorner[0]>=pointList[i][0]>=secondCorner[0] and firstCorner[1]>=pointList[i][1]>=secondCorner[1]):
            return True
    return False

The coordinates of firstCorner and secondCorner just defines one of the corners of the rectangle, do not always represent the top left corner of the rectangle and the bottom right corner.

However, the result always comes back as True. The only time it will return False when the coordinate which is not in the rectangle is the first element in the list.

For example: firstCorner = (0,0), secondCorner = (5,5), pointList=[(1,1), (0,0), (5,6)] (which (1,1) and (0,0) are in the rectangle while (5,6) is not)

print(allIn((0,0), (5,5), [(1,1), (0,0), (5,6)])) always returns True, same result for print(allIn((0,0), (5,5), [(1,1), (5,6), (0,0)])). It only returns False when I put the list as print(allIn((0,0), (5,5), [(5,6), (1,1), (0,0)]))

Could anybody help me to find out which part is wrong?

CodePudding user response:

it seems that you put "return False" in "for" statement?(place 1 in below code ?)

def allIn(firstCorner=(0,0), secondCorner=(0,0), pointList=[]):
    if len(pointList) == 0:
        return False
    for i in range(0, len(pointList)):
        if xx
            return True
        elif xx
            return True
        elif xx
            return True
        elif xx
            return True
        return False  # place 1,here ? cause what you say 
    return False  # or here ? 

additionly, if the real code is same as that in your question, it also be wrong, because the code realize "or" logic ,i.e. if exist any one point in rectangle, it will return True . here is my code:

def allIn(firstCorner=(0, 0), secondCorner=(0, 0), pointList=[]):
    ret=[]
    if len(pointList) == 0:
        return False
    for i in range(0, len(pointList)):
        if (secondCorner[0] >= pointList[i][0] >= firstCorner[0] and secondCorner[1] >= pointList[i][1] >= firstCorner[
            1]):
            ret.append(1)
        elif (secondCorner[0] >= pointList[i][0] >= firstCorner[0] and firstCorner[1] >= pointList[i][1] >=
              secondCorner[1]):
            ret.append(1)
        elif (firstCorner[0] >= pointList[i][0] >= secondCorner[0] and secondCorner[1] >= pointList[i][1] >=
              firstCorner[1]):
            ret.append(1)
        elif (firstCorner[0] >= pointList[i][0] >= secondCorner[0] and firstCorner[1] >= pointList[i][1] >=
              secondCorner[1]):
            ret.append(1)
        else:
            ret.append(0)

    print("ret:",ret)
    return all(ret) 
  • Related