I'm new to Python and trying to determine if a list of points is inside a rectangle. I think my issue might have to do with iterating on integers but I'm not sure how to resolve it.
However, I tried the following solution for a list of points and was unsuccessful:
def allIn(firstCorner=(0,0), secondCorner=(0,0), pointList=[]):
fx = firstCorner[0]
fy = firstCorner[1]
sx = secondCorner[0]
sy = secondCorner[1]
import numpy as np
for i in np.array(pointList):
if i[0] >= fx and i[0] <= sx and i[1] >= fy and i[1] <= sy:
return True
if i[0] <= fx and i[0] >= sx and i[1] <= fy and i[1] >= sy:
return True
if i[0] >= fx and i[0] <= sx and i[1] <= fy and i[1] >= sy:
return True
if i[0] <= fx and i[0] >= sx and i[1] >= fy and i[1] <= sy:
return True
if pointList == []:
return False
else:
return False
The code seems to go through without error but it's not providing the correct answers. For example, it says allIn((0,0), (5,5), [(1,1), (0,0), (5,6)]) is True when it should return False. I think it's only referencing the first point in pointList. i.e.
allIn((0,0),(5,5),[(0,-1), (0,0), (5,6)]) returns False, but allIn((0,0), (5,5), [(1,1), (0,0), (5,6)]) returns True.
It also doesn't give any answer at all for allIn((0,0), (5,5), []).
It's important to note that firstCorner and secondCorner are not necessarily the left and right corners. We assume that the first parameter does not always represent the left corner of the rectangle and the second parameter is not always the right corner. The function should work correctly either way.
I was trying to avoid using libraries for this question (the assignment mentioned not to), but even with a proposed shapely.geometry solution, I still can't figure out how to iterate over the pointList rather than just a single point. Thank you in advance for any assistance you might be able to provide - I really appreciate it!
So, you only
returnwithin the loop if you find an outsider. Otherwise, you have to run through the ENTIRE list before you can returnTrue. This works. The final "not not" thing just converts the present or absence of the list toTrueorFalse.Output:
Another alternative is to write a function to check a single point, and then use the
alloranyfunction to handle the list part.