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.

Convex Hull | Set 1
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).

Convex Hull | Set 1 (Jarvis’s Algorithm or Wrapping)

 

Below is C++ implementation of above algorithm.

// A C++ program to find convex hull of a set of points. Refer
// for explanation of orientation()
[pastacode lang=”cpp” manual=”%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Astruct%20Point%0A%7B%0A%20%20%20%20int%20x%2C%20y%3B%0A%7D%3B%0A%20%0A%2F%2F%20To%20find%20orientation%20of%20ordered%20triplet%20(p%2C%20q%2C%20r).%0A%2F%2F%20The%20function%20returns%20following%20values%0A%2F%2F%200%20–%3E%20p%2C%20q%20and%20r%20are%20colinear%0A%2F%2F%201%20–%3E%20Clockwise%0A%2F%2F%202%20–%3E%20Counterclockwise%0Aint%20orientation(Point%20p%2C%20Point%20q%2C%20Point%20r)%0A%7B%0A%20%20%20%20int%20val%20%3D%20(q.y%20-%20p.y)%20*%20(r.x%20-%20q.x)%20-%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20(q.x%20-%20p.x)%20*%20(r.y%20-%20q.y)%3B%0A%20%0A%20%20%20%20if%20(val%20%3D%3D%200)%20return%200%3B%20%20%2F%2F%20colinear%0A%20%20%20%20return%20(val%20%3E%200)%3F%201%3A%202%3B%20%2F%2F%20clock%20or%20counterclock%20wise%0A%7D%0A%20%0A%2F%2F%20Prints%20convex%20hull%20of%20a%20set%20of%20n%20points.%0Avoid%20convexHull(Point%20points%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20There%20must%20be%20at%20least%203%20points%0A%20%20%20%20if%20(n%20%3C%203)%20return%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20Result%0A%20%20%20%20vector%3CPoint%3E%20hull%3B%0A%20%0A%20%20%20%20%2F%2F%20Find%20the%20leftmost%20point%0A%20%20%20%20int%20l%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(points%5Bi%5D.x%20%3C%20points%5Bl%5D.x)%0A%20%20%20%20%20%20%20%20%20%20%20%20l%20%3D%20i%3B%0A%20%0A%20%20%20%20%2F%2F%20Start%20from%20leftmost%20point%2C%20keep%20moving%20counterclockwise%0A%20%20%20%20%2F%2F%20until%20reach%20the%20start%20point%20again.%20%20This%20loop%20runs%20O(h)%0A%20%20%20%20%2F%2F%20times%20where%20h%20is%20number%20of%20points%20in%20result%20or%20output.%0A%20%20%20%20int%20p%20%3D%20l%2C%20q%3B%0A%20%20%20%20do%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Add%20current%20point%20to%20result%0A%20%20%20%20%20%20%20%20hull.push_back(points%5Bp%5D)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Search%20for%20a%20point%20’q’%20such%20that%20orientation(p%2C%20x%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20q)%20is%20counterclockwise%20for%20all%20points%20’x’.%20The%20idea%0A%20%20%20%20%20%20%20%20%2F%2F%20is%20to%20keep%20track%20of%20last%20visited%20most%20counterclock-%0A%20%20%20%20%20%20%20%20%2F%2F%20wise%20point%20in%20q.%20If%20any%20point%20’i’%20is%20more%20counterclock-%0A%20%20%20%20%20%20%20%20%2F%2F%20wise%20than%20q%2C%20then%20update%20q.%0A%20%20%20%20%20%20%20%20q%20%3D%20(p%2B1)%25n%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20i%20is%20more%20counterclockwise%20than%20current%20q%2C%20then%0A%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20update%20q%0A%20%20%20%20%20%20%20%20%20%20%20if%20(orientation(points%5Bp%5D%2C%20points%5Bi%5D%2C%20points%5Bq%5D)%20%3D%3D%202)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Now%20q%20is%20the%20most%20counterclockwise%20with%20respect%20to%20p%0A%20%20%20%20%20%20%20%20%2F%2F%20Set%20p%20as%20q%20for%20next%20iteration%2C%20so%20that%20q%20is%20added%20to%0A%20%20%20%20%20%20%20%20%2F%2F%20result%20’hull’%0A%20%20%20%20%20%20%20%20p%20%3D%20q%3B%0A%20%0A%20%20%20%20%7D%20while%20(p%20!%3D%20l)%3B%20%20%2F%2F%20While%20we%20don’t%20come%20to%20first%20point%0A%20%0A%20%20%20%20%2F%2F%20Print%20Result%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20hull.size()%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22(%22%20%3C%3C%20hull%5Bi%5D.x%20%3C%3C%20%22%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20hull%5Bi%5D.y%20%3C%3C%20%22)%5Cn%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20Point%20points%5B%5D%20%3D%20%7B%7B0%2C%203%7D%2C%20%7B2%2C%202%7D%2C%20%7B1%2C%201%7D%2C%20%7B2%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B3%2C%200%7D%2C%20%7B0%2C%200%7D%2C%20%7B3%2C%203%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(points)%2Fsizeof(points%5B0%5D)%3B%0A%20%20%20%20convexHull(points%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

 

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)

[ad type=”banner”]