find if 4 points on a plane form a 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
To decide whether four planar points form a rectangle, you need a test that works regardless of the order in which the points are given. The cleanest approach is to avoid slope formulas and square roots, then use distance relationships that are true for every rectangle.
Use Squared Distances Instead of Side Ordering
If the points really are the four corners of a rectangle, the six pairwise distances have a predictable pattern:
- two copies of one side length
- two copies of the other side length
- two equal diagonals
A square is just a special case where the two side lengths are the same, so you may end up with four equal smaller distances and two equal larger ones.
Using squared distances avoids floating-point noise and avoids calling sqrt() unnecessarily.
The last condition is the Pythagorean relationship in squared form. It guarantees that the diagonal matches the two side lengths of a right-angled shape.
Why This Works
For any rectangle with side lengths a and b, each diagonal has length squared a^2 + b^2. Among the six pairwise distances from four points, exactly four of them are edges and two are diagonals. Once the distances fall into the right repeated pattern and satisfy the Pythagorean relation, you have a rectangle regardless of the input order.
This method is usually simpler than trying to guess the polygon order first.
Dot Product Is Useful After You Know the Corner Order
If you already know which points are adjacent, checking a right angle with the dot product is straightforward:
The dot-product method is perfect for validating a candidate ordering, but it is not enough by itself when the four points arrive in random order. That is why the pairwise-distance approach is a better first pass in code interviews and geometry utilities.
Degenerate Cases Matter
A robust implementation should reject these cases:
- duplicate points
- four collinear points
- a self-crossing set that does not produce the rectangle distance pattern
The distances[0] == 0 check rejects duplicates. Collinear points fail because they do not produce the repeated diagonal pattern and do not satisfy the Pythagorean condition in the required way.
Common Pitfalls
- Assuming the points are already listed in clockwise or counterclockwise order.
- Using floating-point distances and then comparing for exact equality.
- Checking only that opposite sides are equal, which also matches some non-rectangular quadrilaterals.
- Forgetting to reject duplicate points.
- Using slope formulas, then running into division-by-zero edge cases for vertical lines.
Summary
- The most robust order-independent test uses the six pairwise squared distances.
- A rectangle produces two equal diagonals and four side distances with the right repetition pattern.
- The squared side lengths must satisfy the Pythagorean relation with the squared diagonal.
- Dot products are useful when you already know which edges meet at a corner.
- Using squared distances avoids unnecessary floating-point problems.
Related reading
- Find if a point is inside a convex hull for a set of points without computing the hull itself
- Find if any set is covered by member sets
- Find if point lies on line segment
- Find k-th minimum sum of every possible subset
- Find K nearest Points to Point P in 2-dimensional plane
- Find largest rectangle containing only zeros in an N×N binary matrix
- Find maximum possible time HHMM by permuting four given digits
- Find minimal Ai2 Bi2 when A and B are sorted

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.