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 using Divide and Conquer Algorithm
- Deleting points from Convex Hull
- Convex Hull | Set 2 (Graham Scan)
- 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
- Stein's Algorithm for finding GCD
- Doolittle Algorithm : LU Decomposition
- Booth’s Multiplication Algorithm
- Bresenham's Algorithm for 3-D Line Drawing
- Computer Organization | Booth's Algorithm
- Pollard's Rho Algorithm for Prime Factorization
- LS3/NS3 sphere generation algorithm and its implementation