geometry
rectangles
plane geometry
coordinate geometry
mathematical problem-solving

find if 4 points on a plane form a rectangle?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

python
1from itertools import combinations
2
3Point = tuple[int, int]
4
5def squared_distance(a: Point, b: Point) -> int:
6    dx = a[0] - b[0]
7    dy = a[1] - b[1]
8    return dx * dx + dy * dy
9
10def forms_rectangle(points: list[Point]) -> bool:
11    if len(points) != 4 or len(set(points)) != 4:
12        return False
13
14    distances = sorted(
15        squared_distance(a, b)
16        for a, b in combinations(points, 2)
17    )
18
19    if distances[0] == 0:
20        return False
21
22    return (
23        distances[0] == distances[1]
24        and distances[2] == distances[3]
25        and distances[4] == distances[5]
26        and distances[0] + distances[2] == distances[4]
27    )
28
29points = [(0, 0), (3, 0), (3, 2), (0, 2)]
30print(forms_rectangle(points))

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:

python
1def dot(u: tuple[int, int], v: tuple[int, int]) -> int:
2    return u[0] * v[0] + u[1] * v[1]
3
4A = (0, 0)
5B = (3, 0)
6D = (0, 2)
7
8AB = (B[0] - A[0], B[1] - A[1])
9AD = (D[0] - A[0], D[1] - A[1])
10
11print(dot(AB, AD) == 0)

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.

Course illustration
Course illustration

All Rights Reserved.