Jun-03-2022, 12:31 AM
(This post was last modified: Jun-03-2022, 12:31 AM by deanhystad.)
If lower left is not always the lower left corner, make it so.
def point_in_rectangle(lower_left, upper_right, point): """Return True if point is in rectangle defined by corners lowerLeft and upper right. """ llx = min(lower_left[0], upper_right[0]) lly = min(lower_left[1], upper_right[1]) urx = max(lower_left[0], upper_right[0]) ury = max(lower_left[1], upper_right[1]) x, y = point return llx <= x <= urx and lly <= y <= uryBut if you do that or not, use the Python way of comparing against a range, not the C way.
Do: x1 <= test1 <= x2 Not test1 >= x1 and test1 <= x2If you really want to do a bunch of comparisons, do them using any():
def point_in_rectangle(lower_left, upper_right, point): """Return True if point is in rectangle defined by corners lowerLeft and upper right. """ x1, y1 = lower_left x2, y2 = upper_right x, y = point return any(( x1 <= x <= x2 and y1 <= y <= y2, x1 <= x <= x2 and y1 >= y >= y2, x1 >= x >= x2 and y1 <= y <= y2, x1 >= x >= x2 and y1 >= y >= y2)) print(point_in_rectangle((1, 2), (3, 4), (1.5, 3.2)))See how easy it is to verify the logic when they are all aligned vertically like this?