🟦 Convex Hull
Interactive convex hull visualizer: step through Graham scan, Jarvis march and Quickhull with live cross-product checks and complexity comparison.
About Convex Hull
The convex hull of a set of points is the smallest convex polygon containing all of them — equivalently, the shape formed by stretching a rubber band around the outermost points. Computing convex hulls efficiently is a fundamental problem in computational geometry with applications in collision detection (the convex hull of a rigid body is its simplest bounding volume), shape analysis, pathfinding, and robot motion planning. The optimal worst-case time complexity is O(n log n) for n input points, which is achievable by Graham scan (1972) and several other algorithms; for point sets where the hull has h vertices, output-sensitive algorithms such as Jarvis march achieve O(nh), which is faster when h is small.
This simulator implements and animates three classic algorithms side by side. Graham scan sorts all points by polar angle around the bottommost point, then traverses them with a stack, discarding any point that creates a right turn (non-left cross product). Jarvis march (gift wrapping) repeatedly selects the point that makes the smallest counterclockwise angle from the current edge. Quickhull recursively divides the point set using the furthest point above each edge, similar to quicksort's partition step. You can click to add points, drag to reposition them, and step through each algorithm to compare intermediate states and operation counts.
Frequently Asked Questions
What is the time complexity of Graham scan, Jarvis march, and Quickhull?
Graham scan runs in O(n log n) due to the initial angular sort; the stack traversal is O(n). Jarvis march (gift wrapping) runs in O(nh) where h is the number of hull vertices: in the worst case (all points on the hull) this is O(n²), but for typical point sets with h = O(log n), it is O(n log n). Quickhull has O(n log n) average-case time (like quicksort) but O(n²) worst case when all points lie on the hull and are processed individually. Chan's algorithm (1996) achieves the optimal O(n log h) in all cases.
How does the cross-product test determine left versus right turns?
For three points A, B, C, compute the 2D cross product (B − A) × (C − A) = (Bx−Ax)(Cy−Ay) − (By−Ay)(Cx−Ax). A positive value means C is to the left of the directed line A→B (counterclockwise turn), negative means right (clockwise, to be discarded in Graham scan), and zero means collinear. This O(1) orientation test is the fundamental primitive in all convex hull algorithms — and in many other computational geometry algorithms such as polygon triangulation and line-segment intersection.
What is the lower bound for convex hull computation?
The convex hull problem has an Ω(n log n) lower bound in the algebraic decision tree model, proven by a reduction from sorting: given n numbers x₁, …, xn, place points (xᵢ, xᵢ²) on a parabola — their convex hull is the entire set, returned in sorted order. Since sorting requires Ω(n log n) comparisons, any algorithm that solves both must also take Ω(n log n). This makes Graham scan and merge-hull algorithms asymptotically optimal for general point sets.
How is the convex hull used in collision detection?
In 2D game physics, the convex hull of a polygon is its minimal convex wrapper. Two convex polygons can be tested for intersection using the Separating Axis Theorem (SAT): if a line exists that separates the two hulls, they do not overlap — and only O(h₁ + h₂) candidate separating axes need to be tested (one per edge). The GJK (Gilbert-Johnson-Keerthi) algorithm extends this to 3D and handles curved objects by computing the Minkowski difference, achieving O(1) iterations in practice for simple shapes. Both SAT and GJK are fundamental tools in Unity, Bullet, and other physics engines.
What happens when points are collinear on the hull boundary?
Points that lie on a hull edge but are not vertices (they are collinear between two hull vertices) can be included or excluded depending on the algorithm variant. Standard Graham scan excludes collinear interior points (it removes them during the sort deduplication step), yielding the minimal vertex set. Some applications (e.g., polygon area computation) prefer including all boundary points. The choice affects h (hull size), runtime, and the behaviour of the cross-product orientation test (zero cross product must be handled carefully to avoid infinite loops in Jarvis march).
What is Chan's algorithm and why is it optimal?
Chan's algorithm (Timothy Chan, 1996) achieves O(n log h) time, where h is the number of hull vertices — this is optimal because outputting h vertices takes Ω(h) time and sorting n points takes Ω(n log n). Chan's approach guesses h in doubling phases (try h = 2, 4, 8, …), runs a mini-Jarvis march that stops after h steps using a precomputed Graham scan on n/h groups of points as inner oracles. When the guess equals the true h, the algorithm terminates with a correct hull. Each phase costs O(n log h); the doubling adds only a constant factor, giving O(n log h) total.
How is the convex hull used in linear programming?
In 2D linear programming, the feasible region defined by m inequality constraints is a convex polygon — a convex hull of the constraints' intersection points. The optimal solution of a linear programme always lies at a vertex of the feasible polytope. The simplex method traverses vertices of this polytope; interior-point methods traverse the interior. In higher dimensions (d variables, m constraints), computing the vertex enumeration of the feasible polytope is equivalent to computing a convex hull in d dimensions — a problem solved by double-description and beneath-beyond algorithms.
What is the 3D convex hull and what algorithms compute it?
In 3D, the convex hull of n points is a convex polyhedron with at most O(n) vertices, edges, and faces (by Euler's formula: V − E + F = 2, and for convex polyhedra F ≤ 2n − 4). Algorithms include the 3D Graham scan (incremental insertion), divide-and-conquer (O(n log n)), and the gift-wrapping variant (Jarvis march in 3D). The QuickHull3D algorithm by Barber, Dobkin and Huhdanpaa (1996, qhull library) is the practical standard — used in SciPy's ConvexHull, MATLAB, and game engine physics tools.
Can convex hull algorithms handle duplicate points?
Duplicate points (identical coordinates) must be handled explicitly; most implementations deduplicate the input before running the hull algorithm. In Graham scan, duplicate points would produce zero cross products causing ambiguity in the angular sort. In Jarvis march, selecting a duplicate as the next hull vertex could cause an infinite loop. A robust implementation either removes duplicates in O(n log n) preprocessing, or uses exact arithmetic with perturbation (symbolic perturbation / SOS — simulation of simplicity) to handle all degenerate configurations consistently.
What is the relationship between convex hull and Voronoi diagrams?
There is a classical duality: the 2D Voronoi diagram of n points is equivalent to the projection of the 3D convex hull of the same points lifted to the paraboloid z = x² + y². Specifically, lift each point (xᵢ, yᵢ) to (xᵢ, yᵢ, xᵢ² + yᵢ²), compute the 3D convex hull, then project the lower hull faces back to 2D — the result is the Delaunay triangulation, and its dual graph is the Voronoi diagram. This means any O(n log n) 3D convex hull algorithm immediately gives an O(n log n) Voronoi algorithm, matching the classical Fortune's sweep-line algorithm.
How is the convex hull applied in machine learning?
In support vector machines (SVMs), the maximum-margin classifier between two classes of points corresponds to finding the closest points on the convex hulls of the two classes — the "minimum enclosing ball" dual. The SVM's support vectors are exactly the hull points closest to the separating hyperplane. Convex hulls also appear in data depth (Tukey depth, the "onion peeling" algorithm), anomaly detection (data points outside the hull are outliers), and in multi-objective optimisation where the Pareto frontier is a portion of the convex hull of feasible objective vectors.
Compute the smallest convex polygon enclosing a point set with Graham scan, Jarvis march or Quickhull — animated step by step, with live cross-product turn detection and comparative operation counts.
3D · Three.js / WebGL renderer · 60 FPS target · runs fully client-side, no install