Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Learn Convex Hull Algorithms: Orientation Tests, Graham Scan, Monotone Chain and Robust Geometry

Wait, What?

Most convex-hull bugs are not really sorting bugs. They are orientation bugs.

The convex hull is the smallest convex boundary containing a set of points. It is often introduced through the rubber-band metaphor, but professional understanding begins with a sharper primitive: given three points, do they make a left turn, a right turn, or lie on one line? That orientation test controls which points survive on the boundary.

Quick Answer

Learn convex hulls through orientation predicate → geometric invariant → sorted order → stack trace → collinearity policy → Graham scan → monotone chain → robustness and complexity.

1. Learn the Orientation Test Before the Hull

For points A, B and C, the sign of the 2D cross-product determinant tells whether C lies to the left or right of the directed line AB. Draw several triples and predict the sign before calculating it. Include nearly collinear examples. If this predicate is conceptually weak, every hull algorithm built on top of it becomes fragile.

2. What the Hull Must Preserve

As you walk around a convex hull in one consistent direction, the boundary must keep turning the same way. Interior dents violate convexity. That gives the learner the core invariant: when a newly considered point creates the wrong turn, the previous boundary candidate cannot remain on the final hull.

3. Graham Scan: Sort by Angle, Then Repair

Choose a pivot, commonly the lowest point, then order the remaining points around it by polar angle. Scan through that order with a stack. Whenever the last two stack points together with the new point fail the required turn test, pop the middle candidate. Continue until the turn invariant is restored.

4. Why the Pop Is Correct

The learner should never memorise “while cross product is non-positive, pop” without understanding the geometry. If three consecutive boundary candidates make an inward turn, the middle point lies inside or on the straight boundary determined by its neighbours under the chosen collinearity convention. Keeping it would violate the convex boundary rule.

5. Monotone Chain: Sort Lexicographically

Andrew’s monotone-chain method sorts points by x-coordinate and then y-coordinate. It builds a lower hull from left to right and an upper hull from right to left using the same turn test. The method is popular because the implementation is compact and the reasoning clean: two monotone boundary chains meet at the extremes.

6. Collinear Points Are a Contract Choice

Do you want only the extreme endpoints of a straight hull edge, or every boundary point lying on that edge? The inequality in the turn test changes the answer. This is not a minor implementation detail. It is part of the output specification and must be stated before testing correctness.

7. Complexity Comes Mostly From Sorting

For Graham scan and monotone chain, sorting takes O(n log n). The scan itself is linear because each point can be pushed and popped only a bounded number of times. This is an excellent place to teach amortized counting without calling it mysterious: a point removed from the stack does not keep returning indefinitely.

8. Real Geometry Needs Robust Predicates

Floating-point arithmetic can misclassify orientation when points are nearly collinear. Jonathan Shewchuk’s work on adaptive-precision geometric predicates is a classic reminder that mathematically correct formulas can still fail in finite-precision computation. Professional implementations must distinguish the abstract algorithm from numerical reliability.

9. Compare Output-Sensitive Alternatives

Gift wrapping can run in O(nh), where h is the number of hull vertices, making it conceptually attractive when the hull is small. More advanced algorithms achieve other output-sensitive bounds. The learner should first master orientation and one O(n log n) method before exploring asymptotic refinements.

Common Failure States

  • Using the wrong sign convention for orientation.
  • Sorting points but mishandling duplicate or collinear cases.
  • Popping without stating the convexity invariant.
  • Mixing clockwise and counter-clockwise conventions midway through the code.
  • Assuming exact arithmetic when coordinates are floating point.
  • Comparing hull algorithms without considering output size or numerical requirements.

Practice Ladder

  • Classify ten point triples by orientation.
  • Trace one Graham-scan stack by hand.
  • Build lower and upper monotone chains separately.
  • Change the collinearity convention and observe the output difference.
  • Test duplicates and nearly collinear inputs.
  • Compare O(n log n) scanning with O(nh) gift wrapping on different point sets.
  • Explain why robust predicates can matter even when the high-level algorithm is correct.

Learning Hall Boundary

This article owns foundational convex-hull construction and orientation reasoning. It connects to the existing Delaunay-triangulation article through shared geometric predicates but does not replace triangulation, spatial indexing or broader computational geometry.

Professional rule: understand convex hull algorithms when you can derive every stack pop from the orientation invariant, state your collinearity policy and explain why numerical predicates can change a theoretically correct result in practice.