Finding whether a point lies inside a rectangle or not
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding whether a point lies inside a rectangle is a common problem in computer science and computational geometry. This problem can arise in various applications such as computer graphics, geographical information systems, collision detection in games, and more. Below is a detailed article explaining the techniques and logic used to ascertain if a given point is inside a rectangle.
Background Concepts
Before diving into the methodology, let's solidify some foundational concepts related to points and rectangles:
• Point: A point is defined in a 2D plane by two coordinates, usually denoted as . • Rectangle: A rectangle is defined by its sides being parallel to the coordinate axes. In most scenarios, a rectangle is described by two corner points — the top-left and bottom-right corners, or alternatively by a single corner and its width and height.
Coordinate System
In a typical Cartesian coordinate system used in computer graphics:
• The top-left corner of the window/screen is typically (0, 0). • The x-axis increases to the right. • The y-axis increases as you move down the screen.
Algorithm for Axis-Aligned Rectangle
For axis-aligned rectangles, where the edges of the rectangle are parallel to the x or y axes, determining if a point lies inside the rectangle is straightforward. The rectangle can be defined using two corners, typically the bottom-left and top-right points.
Steps:
- A point lies inside or on the edge of this rectangle if: • •
If both conditions are met, the point is inside the rectangle; otherwise, it lies outside.
Algorithm Implementation:
Below is a sample function written in Python to implement this logic:
• Vertices Representation: The rectangle is represented by its four vertices, say , , , , listed in a clockwise or counterclockwise direction.
• Boundary Conditions: It must be clarified whether points on the edge or corner should be considered 'inside'. • Degenerate Cases: Situations like zero-width or zero-height rectangles deserve special handling. • Floating Point Arithmetic: Precision issues can affect the result due to representation errors in computers.
Related reading
- Fingerprint matching/recognition algorithms/implementations
- First-Occurrence Parallel String Matching Algorithm
- First occurrence in a binary search
- fitting rectangles in the smallest possible area
- First appearance in Stern's Diatomic Sequence
- Fit multivariate gaussian distribution to a given dataset
- Fitting rectangles together in optimal fashion
- Flipboard’s layout algorithm

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.