-1

I have a list of points and I need to find if the points exist in a rectangle defined by the top-left and bottom-right corners. If all the points belong in the rectangle then the result is True, otherwise the result is False.

Here is my code but I am getting it wrong somewhere.

 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

I am getting this error:

<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
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
DigiMon
  • 17
  • 1
  • 3
  • can you clarify your question, i don't understand well do you want to check if one of the `pointList` tuples equals `bottomRight` and `topLeft`? – Azhy Oct 01 '18 at 22:21
  • And why it should return `False`? isn't `(1, 1)` a point inside `(0, 0)` and `(5, 5)`? – Azhy Oct 01 '18 at 22:24
  • Hi @Azhy. First the rectangle is formed from the 2 points only. (0,0) and (5,5) in this case. (1,1) and (0,0) will result in True for this rectangle but (5,6) is outside the rectangle. Since all points are not in the rectangle I should get a false. – DigiMon Oct 01 '18 at 22:37

1 Answers1

2

Based on my understanding of your question, these are some of the mistakes you made in your code:

  1. You used the i variable before setting or initializing it like:-.

    xa = pointList[i][0]
    ya = pointList[i][0]
    xb = pointList[i][1]
    yb = pointList[i][1]
    

    I think you wanted to use it as an iterating variable in the for loop but you used it outside the loop.

  2. You created four variables for each point in the pointList variable which I think are for the point's x, y, width, height although a point has no any width or height.

  3. Your loop is invalid because it returns True or False after looping through the first item in the list, and it won't search for the other points to know if they are inside or outside the rectangle.


So I created a copy of your code with some changes based on what you want to and to also understand it easily:

def Functn1(topLeft=(0,0), bottomRight=(0,0), pointList=[]):

    x = topLeft[0]     #Rectangle x
    y = topLeft[1]     #Rectangle y
    w = bottomRight[0] #Rectangle width(which you created with name of x2)
    h = bottomRight[1] #Rectangle height(which you created with name of y2)

    for i in range(len(pointList)):

        p_x = pointList[i][0] #Current point x
        p_y = pointList[i][1] #Current point y

        if not ((p_x >= x and p_x < w) and (p_y >= y and p_y < h)): #Return False if the point wasn't in the rectangle (because you said theyre all must be inside the rectangle)
            return False

    return True #If non of them was outside the rectangle (Or it wasn't returned False) so return True

But if you worked or read about graphics you should know the (0, 0) in your example is inside the rectangle and (5, 5) is outside the rectangle, because if the size of something was 5 pixels so the last pixel's value is 4 not 5, and I wrote the code like that and if you want to change it so you can easily change the < operator to <= and also the > to >= to include the last point of the rectangle.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Azhy
  • 704
  • 3
  • 16
  • Hi @Azhy. Thank you so much for the quick turnaround. It worked Great! – DigiMon Oct 01 '18 at 23:35
  • @GinoMempin Yes you are right, I forgot to do that. but by the way why the `len()` function returns this error? I don't understand isn't that function return an `integer` value? – Azhy Oct 02 '18 at 20:42
  • 1
    Sorry, I think I greatly misunderstood the codes and the error. The TypeError was not caused by the `range` or the `len` functions. It's something else, and your code already fixed it. So just ignore my previous comments. Your answer's good as it is, +1. – Gino Mempin Oct 03 '18 at 23:50