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, q, r) = 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Dynamic Convex hull | Adding Points to an Existing Convex Hull
- Quickhull Algorithm for Convex Hull
- Convex Hull using Divide and Conquer Algorithm
- Convex Hull | Monotone chain 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
- Check whether two convex regular polygon have same center or not
- Check if the given point lies inside given N points of a Convex Polygon
- Check if given polygon is a convex polygon or not
- Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Line Clipping | Set 2 (Cyrus Beck Algorithm)
- Minimum enclosing circle | Set 2 - Welzl's algorithm
- Closest Pair of Points using Divide and Conquer algorithm
- Euclid's Algorithm when % and / operations are costly