How Andrew’s algorithm builds a monotone chain

Short answer: Andrew’s monotone chain computes a planar convex hull by sorting points by increasing x, then building the lower hull and the upper hull as stacks that pop non-left turns. The sort is O(n log n) and each chain is linear, so the total matches the Graham scan without a polar-angle comparison. The two chains meet at the leftmost and rightmost points. Collinear points are kept or dropped by the same turn predicate the rest of the hull code uses.


What the problem actually asks

The Graham scan’s polar sort is the part that is easy to get wrong: ties, the anchor, and the wrap around 180 degrees. A left-to-right sort has a total order that floating-point x coordinates already approximate, with a y tie break. The lower hull, walking left to right, is a chain that only turns one way. The upper hull is the same walk with the opposite turn, or the same turn on a reversed pass.

Concatenating the chains includes the rightmost point twice if both walks push it, so one copy is dropped at the join. The leftmost point is the shared start.


What the algorithm guarantees

The concatenated chain is the convex hull in counterclockwise or clockwise order, depending on the turn convention. Time is O(n log n). Extra memory is the sorted array and two stacks. Duplicate points are removed after the sort or handled by the turn test. The algorithm assumes general position only in the sense that a robust cross product decides turns. It does not require integer coordinates, and an integer cross product avoids rounding when the coordinates themselves are integers.

The monotone chain is not faster than Graham’s asymptotically. It is simpler. Jarvis march is faster only when the hull has very few vertices.


The working steps

  1. Sort the points by increasing x, and by increasing y when x ties. Remove exact duplicates if the minimum vertex set is required.
  2. Build the lower hull from left to right. Push each point, and pop the middle point of the last three while those three make a right turn or a straight line that must be simplified.
  3. Build the upper hull from right to left, or left to right with the opposite turn test.
  4. Drop the first and last point of the upper hull if they duplicate the ends of the lower hull, and concatenate.
  5. A cross product of zero means collinear. Pop it for a minimum hull. Keep it only under an explicit convention.

The hull is planar. The sort key is x, not the polar angle. Mixing the two sorts breaks the monotone invariant and the stack can miss a hull vertex or keep an interior one. Output-sensitive wrapping is Jarvis. This algorithm always pays for the sort.


Approaches that look simpler and fail

Approach Why it fails when both chains keep both endpoints
Concatenate the chains without dropping the duplicated ends The leftmost and rightmost points appear twice. The polygon has zero-length edges, and a later area formula can still be right while a vertex walk is wrong.
Sort by polar angle and then run the two-chain pops The monotone invariant is an x-order invariant. Angle order is the Graham scan’s input, and the lower-upper split is no longer a left-to-right pass.
Use a floating-point slope to decide the turn The cross product is the predicate. A slope division fails on a vertical segment and loses the sign on near-horizontal ties.
Claim O(n·h) time The sort is O(n log n) regardless of h. The linear chains do not remove it.

Polar sorting is the Graham scan. Gift wrapping is Jarvis march. Andrew’s monotone chain is the x-sort plus two stacks.


A working example

This Python fragment builds the lower hull and the upper hull after sorting by x. It drops the duplicated endpoints. The hull of (0, 0), (1, 1), (2, 0), (1, -1) is the diamond. It prints the lower-left start and the vertex count, 0 4.

pts = [(0, 0), (1, 1), (2, 0), (1, -1)]

def cross(o, a, b):
    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])

def andrew(points):
    points = sorted(set(points))
    if len(points) <= 1:
        return points
    lower = []
    for p in points:
        while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
            lower.pop()
        lower.append(p)
    upper = []
    for p in reversed(points):
        while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
            upper.pop()
        upper.append(p)
    return lower[:-1] + upper[:-1]

hull = andrew(pts)
assert len(hull) == 4 and hull[0] == (0, 0)
print(hull[0][0], len(hull))

Where it is used

Andrew’s monotone chain is the usual 2D convex hull in mapping and collision code: sort by x, then build the lower and upper chains. Graham’s scan computes the same hull with a polar sort. A physics engine that only needs overlap uses GJK and does not build the hull.


When you can drop this algorithm

Drop a polar-angle implementation when writing a hull from scratch. Sort by x and build the two chains. Keep the collinear predicate identical to the one the rest of the geometry code uses.


Why this stays published

We publish this so a search for Andrew’s monotone chain returns the x-sort and the two stacks that join at the extreme points. Andrew’s monotone chain computes a planar convex hull in O(n log n) time by sorting on x and popping non-left turns on a lower chain and an upper chain.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *