{"id":25897,"date":"2017-10-25T21:20:30","date_gmt":"2017-10-25T15:50:30","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25897"},"modified":"2017-10-25T21:20:30","modified_gmt":"2017-10-25T15:50:30","slug":"convex-hull-set-1-jarviss-algorithm-wrapping","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/convex-hull-set-1-jarviss-algorithm-wrapping\/","title":{"rendered":"Convex Hull | Set 1 (Jarvis\u2019s Algorithm or Wrapping)"},"content":{"rendered":"<p>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.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-25900\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/convexHull1.png\" alt=\"Convex Hull | Set 1 \" width=\"325\" height=\"101\" \/><br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/check-if-two-given-line-segments-intersect\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to check if two given line segments intersect?<\/a><\/p>\n<p>The idea of Jarvis\u2019s 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 <a href=\"http:\/\/www.geeksforgeeks.org\/orientation-3-ordered-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">orientation()<\/a> 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 \u201corientation(p, r, q) = counterclockwise\u201d. Following is the detailed algorithm.<\/p>\n<p><strong>1)<\/strong> Initialize p as leftmost point.<br \/>\n<strong>2)<\/strong> Do following while we don\u2019t come back to the first (or leftmost) point.<br \/>\n\u2026..<strong>a)<\/strong> The next point q is the point such that the triplet (p, q, r) is counterclockwise for any other point r.<br \/>\n\u2026..<strong>b)<\/strong> next[p] = q (Store q as next of p in the output convex hull).<br \/>\n\u2026..<strong>c)<\/strong> p = q (Set p as q for next iteration).<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25906\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/convexHull1-1.png\" alt=\"Convex Hull | Set 1 (Jarvis\u2019s Algorithm or Wrapping)\" width=\"624\" height=\"328\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/convexHull1-1.png 624w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/convexHull1-1-300x158.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/p>\n<p>\u00a0<\/p>\n<p>Below is C++ implementation of above algorithm.<\/p>\n<div>\n<div id=\"highlighter_708258\" class=\"syntaxhighlighter nogutter c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c comments\">\/\/ A C++ program to find convex hull of a set of points. Refer<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c comments\">\/\/ <a href=\"http:\/\/www.geeksforgeeks.org\/orientation-3-ordered-points\/\">http:\/\/www.geeksforgeeks.org\/orientation-3-ordered-points\/<\/a><\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c comments\">\/\/ for explanation of orientation()<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%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\u2013%3E%20p%2C%20q%20and%20r%20are%20colinear%0A%2F%2F%201%20\u2013%3E%20Clockwise%0A%2F%2F%202%20\u2013%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\u2019q\u2019%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\u2019x\u2019.%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\u2019i\u2019%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\u2019hull\u2019%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\u2019t%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p><strong>Output:<\/strong> The output is points of the convex hull.<\/p>\n<pre>(0, 3)\r\n(0, 0)\r\n(3, 0)\r\n(3, 3)<\/pre>\n<p><strong>Time Complexity:<\/strong> 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 <sup>2<\/sup>). The worst case occurs when all the points are on the hull (m = n)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Convex Hull | Set 1 (Jarvis\u2019s Algorithm or Wrapping) &#8211; Geometric Algorithms &#8211; The idea of Jarvis\u2019s Algorithm is simple, we start from the leftmost point.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73905,83572],"tags":[72929,76001,76002,76004,76005,76003,76000,76006],"class_list":["post-25897","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-geometric-algorithms","category-jarviss-algorithm","tag-convex-hull-algorithm","tag-convex-hull-c","tag-convex-hull-definition","tag-convex-hull-divide-and-conquer","tag-convex-hull-example","tag-convex-hull-geeksforgeeks","tag-convex-hull-graham-scan","tag-convex-hull-in-image-processing"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25897"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25897\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}