{"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>&nbsp;<\/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\/\" target=\"_blank\" rel=\"noopener\">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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>struct Point<br\/>{<br\/>    int x, y;<br\/>};<br\/> <br\/>\/\/ To find orientation of ordered triplet (p, q, r).<br\/>\/\/ The function returns following values<br\/>\/\/ 0 --&gt; p, q and r are colinear<br\/>\/\/ 1 --&gt; Clockwise<br\/>\/\/ 2 --&gt; Counterclockwise<br\/>int orientation(Point p, Point q, Point r)<br\/>{<br\/>    int val = (q.y - p.y) * (r.x - q.x) -<br\/>              (q.x - p.x) * (r.y - q.y);<br\/> <br\/>    if (val == 0) return 0;  \/\/ colinear<br\/>    return (val &gt; 0)? 1: 2; \/\/ clock or counterclock wise<br\/>}<br\/> <br\/>\/\/ Prints convex hull of a set of n points.<br\/>void convexHull(Point points[], int n)<br\/>{<br\/>    \/\/ There must be at least 3 points<br\/>    if (n &lt; 3) return;<br\/> <br\/>    \/\/ Initialize Result<br\/>    vector&lt;Point&gt; hull;<br\/> <br\/>    \/\/ Find the leftmost point<br\/>    int l = 0;<br\/>    for (int i = 1; i &lt; n; i++)<br\/>        if (points[i].x &lt; points[l].x)<br\/>            l = i;<br\/> <br\/>    \/\/ Start from leftmost point, keep moving counterclockwise<br\/>    \/\/ until reach the start point again.  This loop runs O(h)<br\/>    \/\/ times where h is number of points in result or output.<br\/>    int p = l, q;<br\/>    do<br\/>    {<br\/>        \/\/ Add current point to result<br\/>        hull.push_back(points[p]);<br\/> <br\/>        \/\/ Search for a point &#039;q&#039; such that orientation(p, x,<br\/>        \/\/ q) is counterclockwise for all points &#039;x&#039;. The idea<br\/>        \/\/ is to keep track of last visited most counterclock-<br\/>        \/\/ wise point in q. If any point &#039;i&#039; is more counterclock-<br\/>        \/\/ wise than q, then update q.<br\/>        q = (p+1)%n;<br\/>        for (int i = 0; i &lt; n; i++)<br\/>        {<br\/>           \/\/ If i is more counterclockwise than current q, then<br\/>           \/\/ update q<br\/>           if (orientation(points[p], points[i], points[q]) == 2)<br\/>               q = i;<br\/>        }<br\/> <br\/>        \/\/ Now q is the most counterclockwise with respect to p<br\/>        \/\/ Set p as q for next iteration, so that q is added to<br\/>        \/\/ result &#039;hull&#039;<br\/>        p = q;<br\/> <br\/>    } while (p != l);  \/\/ While we don&#039;t come to first point<br\/> <br\/>    \/\/ Print Result<br\/>    for (int i = 0; i &lt; hull.size(); i++)<br\/>        cout &lt;&lt; &quot;(&quot; &lt;&lt; hull[i].x &lt;&lt; &quot;, &quot;<br\/>              &lt;&lt; hull[i].y &lt;&lt; &quot;)\\n&quot;;<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},<br\/>                      {3, 0}, {0, 0}, {3, 3}};<br\/>    int n = sizeof(points)\/sizeof(points[0]);<br\/>    convexHull(points, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/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 &lt;= 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=&#8221;banner&#8221;]\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}]}}