Homeβ€Ί DSAβ€Ί Rotating Calipers β€” Diameter, Width, and Closest Pair on Convex Hulls

Rotating Calipers β€” Diameter, Width, and Closest Pair on Convex Hulls

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Geometry β†’ Topic 4 of 4
Learn the rotating calipers technique for computing polygon diameter, minimum width, and closest pair of convex hulls in O(n) after O(n log n) convex hull preprocessing.
πŸ”₯ Advanced β€” solid DSA foundation required
In this tutorial, you'll learn:
  • Rotating calipers: O(n) scan of antipodal vertex pairs after O(n log n) convex hull. Reduces many O(nΒ²) problems to O(n).
  • Each vertex is antipodal to O(1) others as we rotate 360Β° β€” the total antipodal pairs is O(n).
  • Applications: diameter, minimum width, minimum bounding rectangle, closest pair between hulls.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
Imagine holding two parallel rulers (calipers) against opposite sides of a convex shape and rotating them 360 degrees together. At each angle, the distance between them gives the width of the shape in that direction. The maximum width is the diameter, the minimum is the smallest enclosing strip width. Rotating calipers formalize this: O(n) scan of antipodal vertex pairs.

Rotating calipers, introduced by Michael Shamos in 1978, is an O(n) technique for computing geometric properties of convex polygons that would naively require O(nΒ²) comparisons. The key insight: many geometric properties depend only on antipodal vertex pairs β€” pairs of vertices with parallel supporting lines. As you rotate 360Β°, each vertex becomes antipodal to at most 2 other vertices, so the total number of antipodal pairs is O(n).

Polygon Diameter β€” Farthest Pair of Points

The diameter of a convex polygon is the maximum distance between any two vertices. Naively O(nΒ²) β€” but with rotating calipers, O(n) after the hull is built.

rotating_calipers.py Β· PYTHON
1234567891011121314151617181920212223
from math import dist

def polygon_diameter(hull: list[tuple]) -> float:
    """Maximum distance between any two hull vertices. O(n)."""
    n = len(hull)
    if n == 1: return 0
    if n == 2: return dist(hull[0], hull[1])
    def cross_len(o, a, b):
        return abs((a[0]-o[0])*(b[1]-o[1]) - (a[1]-o[1])*(b[0]-o[0]))
    max_dist = 0
    j = 1
    for i in range(n):
        # Advance j while cross product increases (antipodal tracking)
        while cross_len(hull[i], hull[(i+1)%n], hull[(j+1)%n]) > \
              cross_len(hull[i], hull[(i+1)%n], hull[j]):
            j = (j + 1) % n
        max_dist = max(max_dist,
                       dist(hull[i], hull[j]),
                       dist(hull[(i+1)%n], hull[j]))
    return max_dist

hull = [(0,0),(3,0),(3,4),(0,4)]  # 3x4 rectangle β€” diameter = 5
print(f'Diameter: {polygon_diameter(hull):.3f}')  # 5.0 (diagonal)
β–Ά Output
Diameter: 5.000

Applications

Rotating calipers enables O(n) solutions (after O(n log n) hull construction) for: - Diameter: Maximum distance between hull vertices - Minimum width: Minimum enclosing strip - Minimum enclosing rectangle: Smallest bounding rectangle - Closest pair between two convex hulls: O(n+m) - Intersection of convex hulls: O(n+m)

All of these would be O(nΒ²) or O(nΒ²m) without the rotating calipers insight.

πŸ”₯
Real ApplicationMinimum bounding rectangle (computed via rotating calipers) is used in package optimisation (smallest box for a 3D printed object), satellite image analysis (orientation of elongated features), and robotics (gripper alignment with objects).

🎯 Key Takeaways

  • Rotating calipers: O(n) scan of antipodal vertex pairs after O(n log n) convex hull. Reduces many O(nΒ²) problems to O(n).
  • Each vertex is antipodal to O(1) others as we rotate 360Β° β€” the total antipodal pairs is O(n).
  • Applications: diameter, minimum width, minimum bounding rectangle, closest pair between hulls.
  • Always build convex hull first β€” rotating calipers only works on convex polygons.

Interview Questions on This Topic

  • QWhat is an antipodal pair and why does rotating calipers only need to check O(n) pairs?
  • QHow would you find the minimum bounding rectangle of a point set?
  • QWhat is the time complexity of rotating calipers and why?

Frequently Asked Questions

Does rotating calipers work on non-convex polygons?

No β€” rotating calipers requires a convex polygon. For non-convex polygons, compute the convex hull first (O(n log n)), then apply rotating calipers to the hull. The hull has at most n vertices, so the total complexity remains O(n log n).

πŸ”₯
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousPoint in Polygon β€” Ray Casting Algorithm
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged