{"id":200,"date":"2026-11-13T12:34:22","date_gmt":"2026-11-13T04:34:22","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-jarvis-march-wraps-a-convex-hull\/"},"modified":"2026-11-13T21:52:37","modified_gmt":"2026-11-13T13:52:37","slug":"how-jarvis-march-wraps-a-convex-hull","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-jarvis-march-wraps-a-convex-hull\/","title":{"rendered":"How Jarvis march wraps a convex hull"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Jarvis march<\/strong>, or gift wrapping, builds a planar convex hull by starting at the leftmost point and repeatedly choosing the next hull vertex as the point with the smallest polar angle from the current vertex. Each step scans every point, so the time is <strong>O(n\u00b7h)<\/strong>, where h is the number of hull vertices. The worst case is <strong>O(n\u00b2)<\/strong> when every point is on the hull. The best practical case is a hull of a few vertices on a large cloud, where the linear dependence on h beats an n log n sort.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The hull is a cycle. From a known hull edge, the next edge is the one that leaves every other point to the left. That edge ends at the point of minimum turning angle. No global sort is required. The price is a scan of the cloud at every hull vertex.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The same left-turn predicate used by the Graham scan decides the angle comparison: point c is a better next vertex than point b if a\u2192b\u2192c is a right turn, meaning b is inside the turn toward c. Ties, collinear points, choose the farther point so the march does not stop early on an edge.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The march returns the hull vertices in order and closes when it returns to the start. Time is \u0398(n\u00b7h) orientation tests. Extra memory is the output polygon. The algorithm is output-sensitive. It does not become O(n log n) by a trick inside the same loop. Chan&#8217;s algorithm combines a mini-hull sort with gift wrapping to reach O(n log h), which is the asymptotically nice output-sensitive bound. Jarvis is the plain wrap.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A cloud with h = 3 pays a small constant times n. A convex polygon fed back as input pays n\u00b2 \/ something. Choosing the algorithm because &#8220;hulls are small&#8221; is a bet about h, and the bet should be conscious.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose the leftmost point as the start. It is on the hull. Set the current vertex to the start.<\/li>\n<li>Scan every other point and keep the one that is smallest by polar angle from the current vertex, using the cross-product turn test. On a tie, keep the farther point.<\/li>\n<li>Append that point as the next hull vertex and make it current.<\/li>\n<li>Stop when the next vertex would be the start. The appended list is the hull in boundary order.<\/li>\n<li>If a collinear triple should contribute only its endpoints, the farther-point tie break already skips the middle point.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The O(n\u00b7h) bound is tight for this loop. An n log n algorithm, Graham or Andrew, is faster when h is a large fraction of n. Chan&#8217;s algorithm is the theoretical improvement that is still output-sensitive. Jarvis does not compute a 3D hull. The 3D analog is a different gift wrapping.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the next point is the nearest rather than the most external<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Choose the closest point to the current vertex<\/td>\n<td>Distance does not decide the supporting edge. A near interior point is chosen over a far hull point, and the chain cuts into the cloud.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(n log n) for the march<\/td>\n<td>There is no sort. The bound is O(n\u00b7h), which is quadratic when h is \u0398(n).<\/td>\n<\/tr>\n<tr>\n<td>Stop after a fixed number of steps<\/td>\n<td>The hull can have any number of vertices up to n. The closing condition is the return to the start.<\/td>\n<\/tr>\n<tr>\n<td>Break collinear ties by the nearer point<\/td>\n<td>The march can place a middle boundary point and then fail to see the true endpoint, or it can stall. The farther point continues the same supporting line to its end.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">An angular sort and a stack are the Graham scan. A coordinate sort and two chains are Andrew&#8217;s monotone chain. Chan&#8217;s algorithm is the O(n log h) refinement of wrapping. Jarvis march is the direct O(n\u00b7h) wrap.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment wraps the hull by repeatedly choosing the point most counter-clockwise from the current endpoint. On the diamond it also reports <code>4<\/code> hull vertices.<\/p>\n\n<pre><code class=\"language-python\">pts = [(0, 0), (1, 1), (2, 0), (1, -1)]\n\ndef cross(o, a, b):\n    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])\n\ndef jarvis(points):\n    start = min(points)\n    hull = []\n    p = start\n    while True:\n        hull.append(p)\n        nxt = points[0]\n        for q in points[1:]:\n            if nxt == p or cross(p, nxt, q) &lt; 0:\n                nxt = q\n        p = nxt\n        if p == start:\n            break\n    return hull\n\nassert len(jarvis(pts)) == 4\nprint(len(jarvis(pts)))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Jarvis&#8217; march (gift wrapping) builds a convex hull by walking the next tangent, which is attractive when the hull has very few points. Mapping code with a large hull uses Andrew&#8217;s monotone chain. The hull is the same polygon.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop gift wrapping when h may be large. Sort and scan with Andrew&#8217;s algorithm. Keep Jarvis when the hull is known to have a handful of vertices and a full sort would dominate, or when the wrapping view is the one being taught.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Jarvis march returns the O(n\u00b7h) wrap and the turn test that selects the next hull vertex. <strong>Jarvis march builds a planar convex hull in O(n\u00b7h) time by walking from each hull vertex to the point of smallest turning angle.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Jarvis march, or gift wrapping, builds a planar convex hull by starting at the leftmost point and repeatedly choosing the next hull vertex as the point with the smallest polar angle from the current vertex. Each step scans every point, so the time is O(n\u00b7h), where h&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,40],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-geometry"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}