eGift

Gift ideas and thoughtful gifting how-to

Wrapping

Gift Wrapping Algorithm: How It Works

· 6 min read · Wrapping

In this article
  1. Background and Motivation
  2. How the Algorithm Works
  3. Time Complexity and Performance
  4. Choosing the Right Convex Hull Algorithm
  5. Common Pitfalls and Edge Cases
  6. Applications and Real-World Examples
  7. Comparison with Other Algorithms
  8. Frequently Asked Questions

The gift wrapping algorithm is a computational geometry method for finding the convex hull of a set of points—the smallest convex polygon that contains all of them. It works by repeatedly selecting the point that makes the smallest angle with the current hull edge, effectively "wrapping" a string around the points. This algorithm is also known as the Jarvis march, named after R. A. Jarvis who published it in 1973.

Background and Motivation

Convex hulls are fundamental in many fields, including computer graphics (collision detection), pattern recognition, image processing, and geographic information systems (GIS). The gift wrapping algorithm is one of the simplest to understand conceptually, making it a common teaching tool. Its name comes from the visual analogy: imagine wrapping a string around a set of nails on a board—the string traces the convex hull. The algorithm starts at the leftmost point (or any known hull vertex) and then repeatedly finds the next point that requires the smallest clockwise turn, continuing until it returns to the start.

How the Algorithm Works

The algorithm proceeds step by step:

  1. Find the starting point: Choose the point with the smallest x-coordinate (if ties, the smallest y-coordinate). This point is guaranteed to be on the hull.
  2. Set the current point: Mark this as the current hull vertex.
  3. Find the next point: For every other point, compute the angle between the vector from the current point to the candidate and a reference vector (e.g., the positive x-axis). Select the point that gives the smallest angle (i.e., the most clockwise turn).
  4. Repeat: Move to that point, add it to the hull, and repeat step 3 until you return to the starting point.

The angle comparison can be done without actually computing trigonometric functions, using the cross product of vectors. For three points A, B, C, the cross product (B-A) × (C-A) tells whether C is to the left, right, or collinear with the directed line from A to B. To find the next point, you keep the candidate that is most clockwise relative to the current edge.

Time Complexity and Performance

The gift wrapping algorithm has a worst-case time complexity of O(n·h), where n is the number of input points and h is the number of points on the hull. In the worst case, when all points lie on the hull (e.g., points on a circle), h = n, so the complexity becomes O(n²). This makes it inefficient for large datasets compared to other algorithms like QuickHull (O(n log n) average) or Andrew's monotone chain (O(n log n) worst-case). However, if the hull is small (h is much smaller than n), the algorithm can be very fast in practice. For example, if you have 1,000 points but only 10 are on the hull, the algorithm runs in about 10,000 operations, which is faster than sorting-based algorithms that always take at least O(n log n).

Choosing the Right Convex Hull Algorithm

When deciding whether to use gift wrapping, consider the expected size of the hull and the number of points:

  • Gift wrapping (Jarvis march): Best when the hull is expected to be small (e.g., points clustered near a circle's perimeter but with few extreme points). Simple to implement and easy to debug.
  • Andrew's monotone chain: Better for general use because it is O(n log n) regardless of hull size. It sorts points by x-coordinate and builds the hull in two passes.
  • QuickHull: Similar to quicksort, it recursively partitions points. Average case is O(n log n), but worst-case is O(n²) (e.g., points on a circle).
  • Graham scan: Also O(n log n) due to sorting by polar angle. Works well but requires careful handling of collinear points.

For most applications, Andrew's monotone chain is the recommended default because of its predictable performance and ease of implementation. However, gift wrapping is still valuable for educational purposes and for cases where the hull is known to be small, such as in real-time collision detection with few objects.

Common Pitfalls and Edge Cases

Several issues can arise when implementing gift wrapping:

  • Collinear points: If multiple points lie on the same hull edge, the algorithm may pick any of them. To get the extreme endpoints, you can choose the point that is farthest along the edge when angles are equal.
  • Duplicate points: Remove duplicates before running the algorithm to avoid infinite loops or incorrect hulls.
  • All points collinear: The convex hull is a line segment. The algorithm should handle this by returning the two endpoints.
  • Precision issues: Using floating-point arithmetic can cause errors when comparing angles. Use integer arithmetic or a tolerance for cross-product comparisons.
  • Starting point selection: The leftmost point is safe, but if you choose a point that is not on the hull (e.g., an interior point), the algorithm will fail. Always verify the starting point is extreme.

Applications and Real-World Examples

Gift wrapping is used in various practical scenarios:

  • Collision detection: In games, the convex hull of an object can be used as a simplified collision shape. If the hull is small, gift wrapping can be used to compute it quickly.
  • Image processing: Finding the boundary of a shape in a binary image can be done by computing the convex hull of the foreground pixels.
  • Robotics: Path planning around obstacles often uses convex hulls to represent obstacles.
  • Statistics: In outlier detection, the convex hull of a dataset can help identify points that lie outside the main cluster.

For example, consider a set of GPS coordinates of delivery locations. The convex hull gives the minimal polygon that encloses all locations, which can be used to define a service area. If the number of locations is large (e.g., thousands), you would use a faster algorithm, but for a small set (e.g., 20 locations), gift wrapping is perfectly adequate.

Comparison with Other Algorithms

Here is a quick comparison of convex hull algorithms:

  • Gift wrapping: O(n·h), simple, good for small hulls.
  • Graham scan: O(n log n), sorts by angle, requires a stack.
  • Andrew's monotone chain: O(n log n), sorts by x, builds upper and lower hulls.
  • QuickHull: O(n log n) average, O(n²) worst-case, recursive.

In practice, Andrew's monotone chain is often preferred because it is easy to implement and has a stable O(n log n) complexity. Gift wrapping is still taught because it provides insight into the geometric intuition behind convex hulls.

Frequently Asked Questions

What is the difference between gift wrapping and Graham scan?

Gift wrapping builds the hull by repeatedly finding the next point with the smallest angle relative to the current hull edge, without sorting all points first. Graham scan first sorts all points by polar angle around a pivot, then scans them to build the hull. Gift wrapping is O(n·h), while Graham scan is O(n log n) due to sorting.

Can gift wrapping handle points with the same x-coordinate?

Yes, but you need a tie-breaking rule. For the starting point, choose the one with the smallest y-coordinate among those with the smallest x. For subsequent points, if two points have the same angle, choose the one that is farther away to ensure you get the extreme hull vertex.

Is gift wrapping suitable for 3D convex hulls?

The basic idea can be extended to 3D, but it becomes more complex. The algorithm would wrap a plane around the points, and you would need to find the next face that makes the smallest angle with the current face. However, for 3D, algorithms like the incremental method or QuickHull are more commonly used.

What is the best algorithm for a large number of points?

For large datasets, Andrew's monotone chain or QuickHull are better choices because they run in O(n log n) time. Gift wrapping can become O(n²) when the hull is large, which is impractical for millions of points.

Can gift wrapping be used for non-convex shapes?

No, the gift wrapping algorithm specifically computes the convex hull, which is always convex. For non-convex shapes, you would need a different algorithm, such as the concave hull or alpha shapes.