geometry
point-in-rectangle
computational-geometry
algorithms
spatial-analysis

Test if point is in some rectangle

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Testing whether a point lies inside a rectangle is easy if the rectangle is axis-aligned and slightly more interesting if the rectangle can be rotated. The correct algorithm depends on how the rectangle is represented. In the simplest case, you only need boundary comparisons. In the rotated case, you usually transform the point into the rectangle’s local coordinate system or use dot products against the rectangle’s edges.

Axis-Aligned Rectangle

If the rectangle edges are parallel to the axes and the rectangle is defined by its minimum and maximum coordinates, the test is direct.

python
1def point_in_rect(px, py, x_min, y_min, x_max, y_max):
2    return x_min <= px <= x_max and y_min <= py <= y_max
3
4print(point_in_rect(3, 3, 1, 1, 5, 4))   # True
5print(point_in_rect(6, 3, 1, 1, 5, 4))   # False

This is the standard answer in UI hit testing, grid problems, and many collision-detection tasks.

Normalize the Rectangle First

Sometimes input corners arrive in arbitrary order. In that case, normalize first so the logic still works.

python
1def point_in_rect(px, py, x1, y1, x2, y2):
2    x_min, x_max = min(x1, x2), max(x1, x2)
3    y_min, y_max = min(y1, y2), max(y1, y2)
4    return x_min <= px <= x_max and y_min <= py <= y_max
5
6print(point_in_rect(3, 3, 5, 4, 1, 1))   # True

This avoids bugs caused by assuming one corner is always bottom-left and the other is always top-right.

Boundary Included or Excluded

You also need to decide whether points on the edge count as “inside.” The code above includes boundaries because it uses <=.

If edge points should be treated as outside:

python
def point_strictly_inside(px, py, x_min, y_min, x_max, y_max):
    return x_min < px < x_max and y_min < py < y_max

This sounds minor, but it affects geometry algorithms, clipping, and UI hit testing in subtle ways.

Rotated Rectangle

If the rectangle can be rotated, plain min and max coordinate checks are not enough. A common strategy is to translate the point into the rectangle’s center-based coordinates and project it onto the rectangle’s local axes.

Here is one version using the rectangle center, half-width, half-height, and rotation angle:

python
1import math
2
3def point_in_rotated_rect(px, py, cx, cy, half_w, half_h, angle_radians):
4    dx = px - cx
5    dy = py - cy
6
7    cos_a = math.cos(-angle_radians)
8    sin_a = math.sin(-angle_radians)
9
10    local_x = dx * cos_a - dy * sin_a
11    local_y = dx * sin_a + dy * cos_a
12
13    return -half_w <= local_x <= half_w and -half_h <= local_y <= half_h
14
15print(point_in_rotated_rect(1, 0, 0, 0, 2, 1, math.radians(30)))

The idea is to rotate the point in the opposite direction so the rectangle becomes axis-aligned in local space.

Using Dot Products

Another way to reason about the rotated case is with edge vectors. If you know one rectangle corner and its two edge directions, you can project the point onto those edges and check whether both projections lie within the edge lengths.

That approach is especially useful in game engines and geometric libraries where vectors are already the core abstraction.

Performance Considerations

For axis-aligned rectangles, the test is constant-time and extremely cheap. For rotated rectangles, the math is still constant-time, but it involves trigonometric functions if you recompute the rotation every call.

If you test many points against the same rotated rectangle, precompute:

  • sine and cosine
  • rectangle center
  • half extents

That removes repeated work and keeps the check efficient.

Floating-Point Tolerance

With floating-point geometry, boundary cases can be sensitive to tiny rounding errors. If you are testing points that come from calculations rather than exact integers, consider using a tolerance.

python
def point_in_rect_eps(px, py, x_min, y_min, x_max, y_max, eps=1e-9):
    return (x_min - eps <= px <= x_max + eps and
            y_min - eps <= py <= y_max + eps)

This can prevent edge points from flickering between inside and outside due to tiny arithmetic noise.

Practical Use Cases

This test appears in many domains:

  • mouse or touch hit testing
  • map bounding boxes
  • collision checks in games
  • crop or selection tools
  • spatial indexing prefilters

In many systems, axis-aligned rectangle checks are used as cheap first-pass filters before more detailed geometry tests.

Common Pitfalls

The biggest mistake is assuming the rectangle corners are already ordered when they may not be. Another is forgetting to define whether edges count as inside or outside. Developers also sometimes use the simple axis-aligned check on rotated rectangles, which gives wrong results. Finally, floating-point boundary cases can behave inconsistently if you compare values with no tolerance in a numerically noisy pipeline.

Summary

  • For axis-aligned rectangles, compare the point coordinates against rectangle bounds.
  • Normalize corner order if the input may be arbitrary.
  • Decide explicitly whether rectangle edges count as inside.
  • For rotated rectangles, transform the point into local rectangle coordinates or use vector projections.
  • Add a small tolerance if floating-point boundary behavior matters.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.