Check if a point lies on or inside a rectangle in Python

When working with geometric problems in Python, checking if a point lies inside a rectangle is a fundamental task. Given a rectangle defined by its bottom-left and top-right corner points, we can determine if any point (x, y) falls within its boundaries.

Problem Statement

Given a rectangle represented by two points: bottom-left corner (x1, y1) and top-right corner (x2, y2), we need to check whether a given point (x, y) lies inside or on the rectangle.

For example, if bottom_left = (1, 1), top_right = (8, 5), and point = (5, 4), the output should be True since the point lies inside the rectangle.

(1,1) (8,5) (5,4) Point Inside Rectangle Bottom-left: (1,1) Top-right: (8,5) Point: (5,4) ? Inside

Basic Approach

A point lies inside a rectangle if its x-coordinate is between the x-coordinates of the rectangle's corners, and its y-coordinate is between the y-coordinates of the rectangle's corners ?

def is_point_inside_rectangle(bottom_left, top_right, point):
    """Check if a point lies inside a rectangle"""
    x, y = point
    x1, y1 = bottom_left  # Bottom-left corner
    x2, y2 = top_right    # Top-right corner
    
    return (x1 < x < x2) and (y1 < y < y2)

# Test with the example
bottom_left = (1, 1)
top_right = (8, 5)
point = (5, 4)

result = is_point_inside_rectangle(bottom_left, top_right, point)
print(f"Point {point} is inside rectangle: {result}")
Point (5, 4) is inside rectangle: True

Including Points on the Boundary

If we want to include points that lie exactly on the rectangle's boundary, we use less-than-or-equal-to operators ?

def is_point_on_or_inside_rectangle(bottom_left, top_right, point):
    """Check if a point lies on or inside a rectangle"""
    x, y = point
    x1, y1 = bottom_left
    x2, y2 = top_right
    
    return (x1 <= x <= x2) and (y1 <= y <= y2)

# Test with boundary points
test_points = [(1, 1), (8, 5), (1, 3), (5, 1), (5, 4)]

for point in test_points:
    result = is_point_on_or_inside_rectangle(bottom_left, top_right, point)
    print(f"Point {point}: {'Inside/On boundary' if result else 'Outside'}")
Point (1, 1): Inside/On boundary
Point (8, 5): Inside/On boundary
Point (1, 3): Inside/On boundary
Point (5, 1): Inside/On boundary
Point (5, 4): Inside/On boundary

Complete Solution with Multiple Test Cases

def check_point_in_rectangle(bottom_left, top_right, point, include_boundary=True):
    """
    Check if a point lies inside or on a rectangle
    
    Args:
        bottom_left: Tuple (x1, y1) - bottom-left corner
        top_right: Tuple (x2, y2) - top-right corner  
        point: Tuple (x, y) - point to check
        include_boundary: Boolean - whether to include boundary points
    
    Returns:
        Boolean - True if point is inside (and optionally on) rectangle
    """
    x, y = point
    x1, y1 = bottom_left
    x2, y2 = top_right
    
    if include_boundary:
        return (x1 <= x <= x2) and (y1 <= y <= y2)
    else:
        return (x1 < x < x2) and (y1 < y < y2)

# Test cases
rectangle = [(1, 1), (8, 5)]  # [bottom_left, top_right]
test_cases = [
    (5, 4),   # Inside
    (1, 1),   # Bottom-left corner
    (8, 5),   # Top-right corner
    (0, 3),   # Outside (left)
    (9, 3),   # Outside (right)
    (5, 0),   # Outside (below)
    (5, 6)    # Outside (above)
]

print("Point\t\tInside (strict)\tInside/On boundary")
print("-" * 50)

for point in test_cases:
    strict = check_point_in_rectangle(rectangle[0], rectangle[1], point, False)
    inclusive = check_point_in_rectangle(rectangle[0], rectangle[1], point, True)
    print(f"{point}\t\t{strict}\t\t{inclusive}")
Point		Inside (strict)	Inside/On boundary
--------------------------------------------------
(5, 4)		True		True
(1, 1)		False		True
(8, 5)		False		True
(0, 3)		False		False
(9, 3)		False		False
(5, 0)		False		False
(5, 6)		False		False

Conclusion

Checking if a point lies inside a rectangle involves comparing coordinates with boundary conditions. Use strict inequalities (<, >) for points strictly inside, or inclusive inequalities (<=, >=) to include boundary points. This approach works efficiently for axis-aligned rectangles in 2D coordinate systems.

Updated on: 2026-03-25T14:06:37+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements