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.
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.
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.
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:
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:
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.
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
- Testing for repeated characters in a string
- Tetris-ing an array
- Tetris Piece Rotation Algorithm
- Text clustering within a log file
- tf.self_adjoint_eig fails for covariance matrix
- The amortized complexity of stdnext_permutation?
- Text, string-based chord recognition algorithms?
- tf.distribute.MirroredStrategy implementation with sessions not with Keras?

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 courseTrack 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.