Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.
We strongly recommend to see the following post first.
How to check if two given line segments intersect?
The idea of Jarvis’s Algorithm is simple, we start from the leftmost point (or point with minimum x coordinate value) and we keep wrapping points in counterclockwise direction. The big question is, given a point p as current point, how to find the next point in output? The idea is to use orientation() here. Next point is selected as the point that beats all other points at counterclockwise orientation, i.e., next point is q if for any other point r, we have “orientation(p, r, q) = counterclockwise”. Following is the detailed algorithm.
1) Initialize p as leftmost point.
2) Do following while we don’t come back to the first (or leftmost) point.
…..a) The next point q is the point such that the triplet (p, q, r) is counterclockwise for any other point r.
…..b) next[p] = q (Store q as next of p in the output convex hull).
…..c) p = q (Set p as q for next iteration).
Below is the implementation of above algorithm.
Output: The output is points of the convex hull.
(0, 3) (0, 0) (3, 0) (3, 3)
Time Complexity: For every point on the hull we examine all the other points to determine the next point. Time complexity is ?(m * n) where n is number of input points and m is number of output or hull points (m <= n). In worst case, time complexity is O(n 2). The worst case occurs when all the points are on the hull (m = n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Dynamic Convex hull | Adding Points to an Existing Convex Hull
- Quickhull Algorithm for Convex Hull
- Convex Hull | Monotone chain algorithm
- Convex Hull using Divide and Conquer Algorithm
- Convex Hull | Set 2 (Graham Scan)
- Perimeter of Convex hull for a given set of points
- Deleting points from Convex Hull
- Tangents between two Convex Polygons
- Find number of diagonals in n sided convex polygon
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Booth’s Multiplication Algorithm
- Doolittle Algorithm : LU Decomposition
- Stein's Algorithm for finding GCD
- Pollard's Rho Algorithm for Prime Factorization
- Midpoint ellipse drawing algorithm
Improved By : princi singh