How to check if a point is inside a rectangle in Python

1 Answer

0 votes
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Rectangle:
    def __init__(self, top_left, bottom_right):
        self.top_left = top_left
        self.bottom_right = bottom_right

def is_point_inside_rectangle(p, rect):
    return (rect.top_left.x <= p.x <= rect.bottom_right.x and
            rect.top_left.y <= p.y <= rect.bottom_right.y)

rect = Rectangle(Point(0.0, 0.0), Point(7.0, 7.0))
p = Point(3.0, 2.0)

if is_point_inside_rectangle(p, rect):
    print("The point is inside the rectangle.")
else:
    print("The point is outside the rectangle.")



'''
run:

The point is inside the rectangle.

'''

 



answered Jun 22, 2025 by avibootz
...