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
- Convex Hull using Divide and Conquer Algorithm
- Quickhull Algorithm for Convex Hull
- Area of a polygon with given n ordered vertices
- Chinese Remainder Theorem | Set 1 (Introduction)
- Orientation of 3 ordered points
- Segmented Sieve
- Euclidean algorithms (Basic and Extended)
- Closest Pair of Points | O(nlogn) Implementation
- Given n line segments, find if any two segments intersect
- Convex Hull | Set 2 (Graham Scan)
- How to check if a given point lies inside or outside a polygon?
- How to check if two given line segments intersect?
- Closest Pair of Points using Divide and Conquer algorithm
- Sieve of Eratosthenes
- Check whether a given point lies inside a triangle or not