How Jarvis march wraps a convex hull

Short answer: Jarvis march, or gift wrapping, builds a planar convex hull by starting at the leftmost point and repeatedly choosing the next hull vertex as the point with the smallest polar angle from the current vertex. Each step scans every point, so the time is O(n·h), where h is the number of hull vertices. The worst case is O(n²) when every point is on the hull. The best practical case is a hull of a few vertices on a large cloud, where the linear dependence on h beats an n log n sort.


What the problem actually asks

The hull is a cycle. From a known hull edge, the next edge is the one that leaves every other point to the left. That edge ends at the point of minimum turning angle. No global sort is required. The price is a scan of the cloud at every hull vertex.

The same left-turn predicate used by the Graham scan decides the angle comparison: point c is a better next vertex than point b if a→b→c is a right turn, meaning b is inside the turn toward c. Ties, collinear points, choose the farther point so the march does not stop early on an edge.


What the algorithm guarantees

The march returns the hull vertices in order and closes when it returns to the start. Time is Θ(n·h) orientation tests. Extra memory is the output polygon. The algorithm is output-sensitive. It does not become O(n log n) by a trick inside the same loop. Chan’s algorithm combines a mini-hull sort with gift wrapping to reach O(n log h), which is the asymptotically nice output-sensitive bound. Jarvis is the plain wrap.

A cloud with h = 3 pays a small constant times n. A convex polygon fed back as input pays n² / something. Choosing the algorithm because “hulls are small” is a bet about h, and the bet should be conscious.


The working steps

  1. Choose the leftmost point as the start. It is on the hull. Set the current vertex to the start.
  2. Scan every other point and keep the one that is smallest by polar angle from the current vertex, using the cross-product turn test. On a tie, keep the farther point.
  3. Append that point as the next hull vertex and make it current.
  4. Stop when the next vertex would be the start. The appended list is the hull in boundary order.
  5. If a collinear triple should contribute only its endpoints, the farther-point tie break already skips the middle point.

The O(n·h) bound is tight for this loop. An n log n algorithm, Graham or Andrew, is faster when h is a large fraction of n. Chan’s algorithm is the theoretical improvement that is still output-sensitive. Jarvis does not compute a 3D hull. The 3D analog is a different gift wrapping.


Approaches that look simpler and fail

Approach Why it fails when the next point is the nearest rather than the most external
Choose the closest point to the current vertex Distance does not decide the supporting edge. A near interior point is chosen over a far hull point, and the chain cuts into the cloud.
Quote O(n log n) for the march There is no sort. The bound is O(n·h), which is quadratic when h is Θ(n).
Stop after a fixed number of steps The hull can have any number of vertices up to n. The closing condition is the return to the start.
Break collinear ties by the nearer point The march can place a middle boundary point and then fail to see the true endpoint, or it can stall. The farther point continues the same supporting line to its end.

An angular sort and a stack are the Graham scan. A coordinate sort and two chains are Andrew’s monotone chain. Chan’s algorithm is the O(n log h) refinement of wrapping. Jarvis march is the direct O(n·h) wrap.


A working example

This Python fragment wraps the hull by repeatedly choosing the point most counter-clockwise from the current endpoint. On the diamond it also reports 4 hull vertices.

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 jarvis(points):
    start = min(points)
    hull = []
    p = start
    while True:
        hull.append(p)
        nxt = points[0]
        for q in points[1:]:
            if nxt == p or cross(p, nxt, q) < 0:
                nxt = q
        p = nxt
        if p == start:
            break
    return hull

assert len(jarvis(pts)) == 4
print(len(jarvis(pts)))

Where it is used

Jarvis’ march (gift wrapping) builds a convex hull by walking the next tangent, which is attractive when the hull has very few points. Mapping code with a large hull uses Andrew’s monotone chain. The hull is the same polygon.


When you can drop this algorithm

Drop gift wrapping when h may be large. Sort and scan with Andrew’s algorithm. Keep Jarvis when the hull is known to have a handful of vertices and a full sort would dominate, or when the wrapping view is the one being taught.


Why this stays published

We publish this so a search for Jarvis march returns the O(n·h) wrap and the turn test that selects the next hull vertex. Jarvis march builds a planar convex hull in O(n·h) time by walking from each hull vertex to the point of smallest turning angle.

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

Leave a Reply

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