Mathwords logoMathwords

Gift Wrapping Algorithm — Definition, Formula & Example

The gift wrapping algorithm, also called the Jarvis march, finds the convex hull of a set of points. It starts at an outermost point and repeatedly swings a line around the set until the line touches the next point, as if wrapping paper around the points.

Let SS be a finite set of nn points in the plane. Let p0p_0 be the point with the least xx-coordinate, with ties broken by the least yy-coordinate. Given the current hull vertex pip_i, choose pi+1∈Sp_{i+1} \in S so that no point of SS lies strictly to the right of the directed line pi→pi+1p_i \to p_{i+1}. If several points qualify, take the farthest one from pip_i. Stop when pi+1=p0p_{i+1} = p_0. The sequence p0,p1,…p_0, p_1, \dots lists the hull vertices counterclockwise. The algorithm runs in O(nh)O(nh) time, where hh is the number of hull vertices, so the worst case is O(n2)O(n^2).

Key Formula

cross⁡(p,q,r)=(qx−px)(ry−py)−(qy−py)(rx−px)\operatorname{cross}(p, q, r) = (q_x - p_x)(r_y - p_y) - (q_y - p_y)(r_x - p_x)
Where:
  • pp = The current hull vertex
  • qq = The current candidate for the next hull vertex
  • rr = Any other point being tested
  • cross⁡(p,q,r)\operatorname{cross}(p,q,r) = Positive if r is left of the line from p to q, negative if r is right of it, and 0 if the three points are collinear

Worked Example

Problem: The points are A(0, 0), B(6, 2), C(4, 1), D(3, 5), E(1, 3), and F(3, 2). Use the gift wrapping algorithm to find the starting vertex and the hull vertex that comes after it, going counterclockwise.
Step 1: Find the starting point. A has the least x-coordinate and it is the only point with that value, so A is a hull vertex.
p0=A(0,0)p_0 = A(0, 0)
Step 2: Pick any candidate, say B. Test C against the line A to B. The result is negative, so C lies to the right. That means B cannot be next, and C replaces B as the candidate.
cross⁡(A,B,C)=(6)(1)−(2)(4)=−2<0\operatorname{cross}(A, B, C) = (6)(1) - (2)(4) = -2 < 0
Step 3: Test every other point against the new line A to C. Each result is positive, so no point lies to the right. C is confirmed as the next hull vertex.
cross⁡(A,C,B)=2,cross⁡(A,C,D)=17,cross⁡(A,C,E)=11,cross⁡(A,C,F)=5\operatorname{cross}(A,C,B) = 2,\quad \operatorname{cross}(A,C,D) = 17,\quad \operatorname{cross}(A,C,E) = 11,\quad \operatorname{cross}(A,C,F) = 5
Answer: The hull starts at A(0, 0), and its first edge goes from A to C(4, 1). If you repeat the process from each new vertex, you get the full hull A, C, B, D, E. The point F(3, 2) is inside the hull.

Why It Matters

Gift wrapping is a standard first convex hull algorithm in computational geometry and algorithms courses. Its O(nh)O(nh) cost depends on the size of the output, so it beats Graham scan's O(nlog⁡n)O(n \log n) when the hull has fewer than about log⁡n\log n vertices. Convex hulls appear in collision detection for games and robotics and in GIS boundary mapping.

Common Mistakes

Mistake: Ignoring the case where the cross product equals 0, which means a tested point is collinear with the current edge.
Correction: If you keep the nearer collinear point, the hull can include extra points that sit in the middle of an edge. In some implementations, the loop never closes. When the cross product is 0, keep the point farther from pip_i.

Related Terms

  • Algorithm — General idea of a step-by-step procedure this method follows
  • Greedy Algorithm — Strategy of picking the locally extreme point at each step
  • Analytic Geometry — Supplies the coordinates behind the cross product test
  • Euclidean Geometry — Plane setting where convexity and hull edges are defined
  • Geometry — Parent subject containing convex shapes and polygons
  • Convex Hull — The shape this algorithm constructs
  • Cross Product — Used to test which way each turn bends
  • Counterclockwise — Direction of turn checked when choosing the next point