{"id":1126484,"date":"2025-01-08T20:01:38","date_gmt":"2025-01-08T12:01:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126484.html"},"modified":"2025-01-08T20:01:41","modified_gmt":"2025-01-08T12:01:41","slug":"python-%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e4%b8%80%e5%a0%86%e7%82%b9%e7%9a%84%e5%a4%96%e6%8e%a5%e7%9f%a9%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126484.html","title":{"rendered":"python \u5982\u4f55\u9009\u62e9\u4e00\u5806\u70b9\u7684\u5916\u63a5\u77e9\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090801\/925da309-d4ac-4261-b5e1-604ced8c52ad.webp\" alt=\"python \u5982\u4f55\u9009\u62e9\u4e00\u5806\u70b9\u7684\u5916\u63a5\u77e9\u5f62\" \/><\/p>\n<p><p> <strong>\u5728\u9009\u62e9\u4e00\u5806\u70b9\u7684\u5916\u63a5\u77e9\u5f62\u65f6\uff0c\u6709\u51e0\u4e2a\u6838\u5fc3\u6b65\u9aa4\uff1a\u627e\u51fa\u6240\u6709\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u3001\u786e\u5b9a\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\u3001\u8ba1\u7b97\u77e9\u5f62\u7684\u8fb9\u957f\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fdd\u77e9\u5f62\u5b8c\u5168\u5305\u56f4\u6240\u6709\u7684\u70b9\u3002<\/strong>\u5176\u4e2d\uff0c\u627e\u51fa\u6240\u6709\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u8fd9\u51b3\u5b9a\u4e86\u5916\u63a5\u77e9\u5f62\u7684\u8303\u56f4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u89e3\u91ca\u6bcf\u4e2a\u6b65\u9aa4\u7684\u5177\u4f53\u64cd\u4f5c\u548c\u80cc\u540e\u7684\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u627e\u51fa\u6240\u6709\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807<\/h2>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u5916\u63a5\u77e9\u5f62\u65f6\uff0c\u9996\u5148\u9700\u8981\u627e\u5230\u70b9\u96c6\u4e2d\u6700\u5c0f\u7684x\u5750\u6807\u3001\u6700\u5927\u7684x\u5750\u6807\u3001\u6700\u5c0f\u7684y\u5750\u6807\u548c\u6700\u5927\u7684y\u5750\u6807\u3002\u8fd9\u56db\u4e2a\u6781\u503c\u70b9\u5c06\u5e2e\u52a9\u6211\u4eec\u786e\u5b9a\u77e9\u5f62\u7684\u8fb9\u754c\u3002<\/p>\n<\/p>\n<p><h3>\u786e\u5b9a\u6700\u5c0f\u548c\u6700\u5927x\u5750\u6807<\/h3>\n<\/p>\n<p><p>\u8981\u786e\u5b9a\u6700\u5c0f\u548c\u6700\u5927\u7684x\u5750\u6807\uff0c\u53ef\u4ee5\u904d\u5386\u6240\u6709\u70b9\u5e76\u6bd4\u8f83\u5b83\u4eec\u7684x\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230x\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u70b9\u96c6 <code>points<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u627e\u5230\u6700\u5c0f\u548c\u6700\u5927\u7684x\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">points = [(x1, y1), (x2, y2), ..., (xn, yn)]<\/p>\n<p>min_x = min(point[0] for point in points)<\/p>\n<p>max_x = max(point[0] for point in points)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u786e\u5b9a\u6700\u5c0f\u548c\u6700\u5927y\u5750\u6807<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u786e\u5b9a\u6700\u5c0f\u548c\u6700\u5927\u7684y\u5750\u6807\u4e5f\u9700\u8981\u904d\u5386\u6240\u6709\u70b9\u5e76\u6bd4\u8f83\u5b83\u4eec\u7684y\u503c\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_y = min(point[1] for point in points)<\/p>\n<p>max_y = max(point[1] for point in points)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u5916\u63a5\u77e9\u5f62\u7684\u56db\u4e2a\u5173\u952e\u5750\u6807\u70b9\uff1a<code>(min_x, min_y)<\/code>\u3001<code>(max_x, min_y)<\/code>\u3001<code>(min_x, max_y)<\/code>\u3001<code>(max_x, max_y)<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u786e\u5b9a\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9<\/h2>\n<\/p>\n<p><p>\u5728\u627e\u5230\u70b9\u96c6\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u5916\u63a5\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\u3002\u8fd9\u4e9b\u89d2\u70b9\u662f\u77e9\u5f62\u7684\u9876\u70b9\uff0c\u7528\u6765\u5b8c\u5168\u5305\u56f4\u6240\u6709\u7684\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u524d\u9762\u7684\u6b65\u9aa4\uff0c\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\u5916\u63a5\u77e9\u5f62\u7684\u56db\u4e2a\u9876\u70b9\u3002\u6839\u636e\u524d\u9762\u627e\u5230\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\uff0c\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\u5206\u522b\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5de6\u4e0b\u89d2\uff1a(min_x, min_y)<\/li>\n<li>\u53f3\u4e0b\u89d2\uff1a(max_x, min_y)<\/li>\n<li>\u5de6\u4e0a\u89d2\uff1a(min_x, max_y)<\/li>\n<li>\u53f3\u4e0a\u89d2\uff1a(max_x, max_y)<\/li>\n<\/ul>\n<p><p>\u8fd9\u4e9b\u89d2\u70b9\u7684\u786e\u5b9a\u662f\u975e\u5e38\u76f4\u89c2\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u7531\u6700\u5c0f\u548c\u6700\u5927\u7684x\u3001y\u503c\u7ec4\u5408\u800c\u6210\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8ba1\u7b97\u77e9\u5f62\u7684\u8fb9\u957f<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u786e\u5b9a\u4e86\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\uff0c\u5c31\u53ef\u4ee5\u8ba1\u7b97\u77e9\u5f62\u7684\u8fb9\u957f\u3002\u77e9\u5f62\u7684\u8fb9\u957f\u7531\u5176\u6c34\u5e73\u548c\u5782\u76f4\u8fb9\u7684\u957f\u5ea6\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><h3>\u6c34\u5e73\u548c\u5782\u76f4\u8fb9\u7684\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u6c34\u5e73\u8fb9\u7684\u957f\u5ea6\u7531\u6700\u5927x\u503c\u548c\u6700\u5c0fx\u503c\u7684\u5dee\u51b3\u5b9a\uff0c\u5782\u76f4\u8fb9\u7684\u957f\u5ea6\u7531\u6700\u5927y\u503c\u548c\u6700\u5c0fy\u503c\u7684\u5dee\u51b3\u5b9a\u3002\u5177\u4f53\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">width = max_x - min_x<\/p>\n<p>height = max_y - min_y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u9762\u79ef\u548c\u5468\u957f\u7684\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u8fdb\u4e00\u6b65\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u77e9\u5f62\u7684\u9762\u79ef\u548c\u5468\u957f\u3002\u77e9\u5f62\u7684\u9762\u79ef\u7531\u5176\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7684\u4e58\u79ef\u51b3\u5b9a\uff0c\u5468\u957f\u7531\u5176\u56db\u6761\u8fb9\u7684\u957f\u5ea6\u4e4b\u548c\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">area = width * height<\/p>\n<p>perimeter = 2 * (width + height)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8ba1\u7b97\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u5168\u4e86\u89e3\u5916\u63a5\u77e9\u5f62\u7684\u5c3a\u5bf8\u548c\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001Python\u5b9e\u73b0\u5916\u63a5\u77e9\u5f62<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u5b9e\u73b0\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u70b9\u96c6\u5e76\u8fd4\u56de\u5916\u63a5\u77e9\u5f62\u7684\u5173\u952e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bounding_rectangle(points):<\/p>\n<p>    min_x = min(point[0] for point in points)<\/p>\n<p>    max_x = max(point[0] for point in points)<\/p>\n<p>    min_y = min(point[1] for point in points)<\/p>\n<p>    max_y = max(point[1] for point in points)<\/p>\n<p>    width = max_x - min_x<\/p>\n<p>    height = max_y - min_y<\/p>\n<p>    area = width * height<\/p>\n<p>    perimeter = 2 * (width + height)<\/p>\n<p>    return {<\/p>\n<p>        &quot;min_x&quot;: min_x,<\/p>\n<p>        &quot;max_x&quot;: max_x,<\/p>\n<p>        &quot;min_y&quot;: min_y,<\/p>\n<p>        &quot;max_y&quot;: max_y,<\/p>\n<p>        &quot;width&quot;: width,<\/p>\n<p>        &quot;height&quot;: height,<\/p>\n<p>        &quot;area&quot;: area,<\/p>\n<p>        &quot;perimeter&quot;: perimeter,<\/p>\n<p>        &quot;corners&quot;: [(min_x, min_y), (max_x, min_y), (min_x, max_y), (max_x, max_y)]<\/p>\n<p>    }<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>points = [(1, 2), (3, 4), (5, 6), (7, 8)]<\/p>\n<p>result = bounding_rectangle(points)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8ba1\u7b97\u51fa\u7ed9\u5b9a\u70b9\u96c6\u7684\u5916\u63a5\u77e9\u5f62\u7684\u6240\u6709\u5173\u952e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u5728\u5f88\u591a\u9886\u57df\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><h3>\u8ba1\u7b97\u673a\u89c6\u89c9<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\uff0c\u5916\u63a5\u77e9\u5f62\u5e38\u7528\u4e8e\u5bf9\u8c61\u68c0\u6d4b\u548c\u8fb9\u754c\u6846\u7684\u786e\u5b9a\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u4e2d\u68c0\u6d4b\u5230\u4e00\u4e2a\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5916\u63a5\u77e9\u5f62\u6765\u6807\u8bb0\u5bf9\u8c61\u7684\u8fb9\u754c\u3002<\/p>\n<\/p>\n<p><h3>\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\u4e2d\uff0c\u5916\u63a5\u77e9\u5f62\u7528\u4e8e\u786e\u5b9a\u5730\u7406\u5bf9\u8c61\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5916\u63a5\u77e9\u5f62\u6765\u786e\u5b9a\u67d0\u4e2a\u5730\u7406\u533a\u57df\u7684\u8fb9\u754c\uff0c\u4ece\u800c\u65b9\u4fbf\u5730\u8fdb\u884c\u5730\u56fe\u7ed8\u5236\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u673a\u5668\u4eba\u8def\u5f84\u89c4\u5212<\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u4eba\u8def\u5f84\u89c4\u5212\u4e2d\uff0c\u5916\u63a5\u77e9\u5f62\u7528\u4e8e\u786e\u5b9a\u673a\u5668\u4eba\u8fd0\u52a8\u7684\u8fb9\u754c\u548c\u969c\u788d\u7269\u7684\u8303\u56f4\u3002\u901a\u8fc7\u8ba1\u7b97\u5916\u63a5\u77e9\u5f62\uff0c\u673a\u5668\u4eba\u53ef\u4ee5\u66f4\u597d\u5730\u89c4\u5212\u8def\u5f84\uff0c\u907f\u514d\u78b0\u649e\u548c\u969c\u788d\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u4e2d\uff0c\u5916\u63a5\u77e9\u5f62\u7528\u4e8e\u786e\u5b9a\u6570\u636e\u70b9\u7684\u8303\u56f4\u548c\u5206\u5e03\u3002\u4f8b\u5982\uff0c\u5728\u6563\u70b9\u56fe\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5916\u63a5\u77e9\u5f62\u6765\u6807\u8bb0\u6570\u636e\u70b9\u7684\u8fb9\u754c\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5e94\u7528\u573a\u666f\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u5728\u4e0d\u540c\u9886\u57df\u4e2d\u53d1\u6325\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002\u65e0\u8bba\u662f\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u3001\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff0c\u8fd8\u662f\u5728\u673a\u5668\u4eba\u8def\u5f84\u89c4\u5212\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5916\u63a5\u77e9\u5f62\u90fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f18\u5316\u4e0e\u6539\u8fdb<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u8fdb\u884c\u4f18\u5316\u548c\u6539\u8fdb\u3002\u4f8b\u5982\uff0c\u5f53\u70b9\u96c6\u975e\u5e38\u5927\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u52a0\u901f\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u7684\u8ba1\u7b97\u3002\u53e6\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u5982\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\uff0c\u6765\u8ba1\u7b97\u6700\u5c0f\u9762\u79ef\u5916\u63a5\u77e9\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u5e76\u884c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u52a0\u901f\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u3002\u901a\u8fc7\u5c06\u70b9\u96c6\u5206\u6210\u591a\u4e2a\u5b50\u96c6\uff0c\u5e76\u5728\u591a\u4e2a\u5904\u7406\u5668\u4e0a\u5e76\u884c\u8ba1\u7b97\u6bcf\u4e2a\u5b50\u96c6\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\uff0c\u7136\u540e\u5408\u5e76\u7ed3\u679c\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\u662f\u4e00\u79cd\u7528\u4e8e\u8ba1\u7b97\u6700\u5c0f\u9762\u79ef\u5916\u63a5\u77e9\u5f62\u7684\u7b97\u6cd5\u3002\u4e0e\u7b80\u5355\u7684\u8f74\u5bf9\u9f50\u5916\u63a5\u77e9\u5f62\u4e0d\u540c\uff0c\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\u53ef\u4ee5\u627e\u5230\u4e00\u4e2a\u4efb\u610f\u65b9\u5411\u4e0a\u7684\u6700\u5c0f\u9762\u79ef\u5916\u63a5\u77e9\u5f62\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u9002\u7528\u4e8e\u8ba1\u7b97\u51e0\u4f55\u4e2d\u7684\u8bb8\u591a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u73b0\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\u7684Python\u5b9e\u73b0\uff0c\u7528\u4e8e\u8ba1\u7b97\u6700\u5c0f\u9762\u79ef\u5916\u63a5\u77e9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def rotating_calipers(points):<\/p>\n<p>    def angle(p1, p2):<\/p>\n<p>        return math.atan2(p2[1] - p1[1], p2[0] - p1[0])<\/p>\n<p>    hull = convex_hull(points)<\/p>\n<p>    n = len(hull)<\/p>\n<p>    if n == 1:<\/p>\n<p>        return hull[0], hull[0], 0, 0, 0<\/p>\n<p>    elif n == 2:<\/p>\n<p>        return hull[0], hull[1], 0, 0, 0<\/p>\n<p>    min_area = float(&#39;inf&#39;)<\/p>\n<p>    best_rect = None<\/p>\n<p>    k = 1<\/p>\n<p>    for i in range(n):<\/p>\n<p>        while True:<\/p>\n<p>            j = (k + 1) % n<\/p>\n<p>            if angle(hull[i], hull[j]) &gt; angle(hull[i], hull[k]):<\/p>\n<p>                k = j<\/p>\n<p>            else:<\/p>\n<p>                break<\/p>\n<p>        width = math.sqrt((hull[k][0] - hull[i][0]) &lt;strong&gt; 2 + (hull[k][1] - hull[i][1]) &lt;\/strong&gt; 2)<\/p>\n<p>        height = max(abs(hull[j][1] - hull[i][1]), abs(hull[(j + 1) % n][1] - hull[(i + 1) % n][1]))<\/p>\n<p>        area = width * height<\/p>\n<p>        if area &lt; min_area:<\/p>\n<p>            min_area = area<\/p>\n<p>            best_rect = (hull[i], hull[j], width, height, area)<\/p>\n<p>    return best_rect<\/p>\n<p>def convex_hull(points):<\/p>\n<p>    points = sorted(points)<\/p>\n<p>    lower = []<\/p>\n<p>    for p in points:<\/p>\n<p>        while len(lower) &gt;= 2 and cross(lower[-2], lower[-1], p) &lt;= 0:<\/p>\n<p>            lower.pop()<\/p>\n<p>        lower.append(p)<\/p>\n<p>    upper = []<\/p>\n<p>    for p in reversed(points):<\/p>\n<p>        while len(upper) &gt;= 2 and cross(upper[-2], upper[-1], p) &lt;= 0:<\/p>\n<p>            upper.pop()<\/p>\n<p>        upper.append(p)<\/p>\n<p>    return lower[:-1] + upper[:-1]<\/p>\n<p>def cross(o, a, b):<\/p>\n<p>    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>points = [(1, 2), (3, 4), (5, 6), (7, 8)]<\/p>\n<p>result = rotating_calipers(points)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u6700\u5c0f\u9762\u79ef\u7684\u5916\u63a5\u77e9\u5f62\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4f18\u5316\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u5806\u70b9\u7684\u5916\u63a5\u77e9\u5f62\u662f\u4e00\u4e2a\u5e38\u89c1\u800c\u91cd\u8981\u7684\u95ee\u9898\uff0c\u901a\u8fc7\u627e\u5230\u6240\u6709\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\uff0c\u53ef\u4ee5\u786e\u5b9a\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\u548c\u8fb9\u957f\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86Python\u5b9e\u73b0\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u5916\u63a5\u77e9\u5f62\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u3001\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\u3001\u673a\u5668\u4eba\u8def\u5f84\u89c4\u5212\u548c\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528\uff0c\u53ca\u5176\u4f18\u5316\u4e0e\u6539\u8fdb\u7684\u65b9\u6cd5\uff0c\u5982\u5e76\u884c\u8ba1\u7b97\u548c\u65cb\u8f6c\u5361\u58f3\u7b97\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u3001\u66f4\u51c6\u786e\u5730\u8ba1\u7b97\u5916\u63a5\u77e9\u5f62\uff0c\u5e76\u5e94\u7528\u4e8e\u5404\u79cd\u5b9e\u9645\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u4e00\u7ec4\u70b9\u7684\u5916\u63a5\u77e9\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u9009\u62e9\u4e00\u7ec4\u70b9\u7684\u5916\u63a5\u77e9\u5f62\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u8fd9\u4e9b\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u6765\u5b9e\u73b0\u3002\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5c06\u6240\u6709\u70b9\u7684\u5750\u6807\u5b58\u50a8\u5728\u4e00\u4e2aNumPy\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/li>\n<li>\u6839\u636e\u8fd9\u4e9b\u6700\u5c0f\u548c\u6700\u5927\u503c\uff0c\u6784\u5efa\u5916\u63a5\u77e9\u5f62\u7684\u56db\u4e2a\u8fb9\u754c\u3002<\/li>\n<\/ol>\n<p><strong>\u5728\u9009\u62e9\u5916\u63a5\u77e9\u5f62\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u56e0\u7d20\uff1f<\/strong><br \/>\u9009\u62e9\u5916\u63a5\u77e9\u5f62\u65f6\uff0c\u60a8\u9700\u8981\u5173\u6ce8\u70b9\u7684\u5206\u5e03\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u70b9\u7684\u5206\u5e03\u4e0d\u5747\u5300\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5916\u63a5\u77e9\u5f62\u7684\u9762\u79ef\u8fc7\u5927\uff0c\u5305\u542b\u5f88\u591a\u7a7a\u767d\u533a\u57df\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fb9\u754c\u70b9\u7684\u96c6\u5408\u6765\u4f18\u5316\u7ed3\u679c\uff0c\u786e\u4fdd\u5916\u63a5\u77e9\u5f62\u5c3d\u91cf\u7d27\u51d1\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u5b9e\u73b0\u5916\u63a5\u77e9\u5f62\u7684\u8ba1\u7b97\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u548cOpenCV\u3002NumPy\u9002\u5408\u8fdb\u884c\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u800cOpenCV\u63d0\u4f9b\u4e86\u66f4\u591a\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u80fd\u591f\u5728\u5904\u7406\u56fe\u50cf\u6570\u636e\u65f6\u66f4\u6709\u6548\u5730\u9009\u62e9\u5916\u63a5\u77e9\u5f62\u3002\u8fd9\u4e9b\u5e93\u7684\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u53ef\u89c6\u5316\u5916\u63a5\u77e9\u5f62\u53ca\u5176\u5305\u542b\u7684\u70b9\uff1f<\/strong><br \/>\u4e3a\u4e86\u53ef\u89c6\u5316\u5916\u63a5\u77e9\u5f62\u53ca\u5176\u5305\u542b\u7684\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u901a\u8fc7\u7ed8\u5236\u70b9\u7684\u6563\u70b9\u56fe\u548c\u77e9\u5f62\u8fb9\u6846\uff0c\u60a8\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u5916\u63a5\u77e9\u5f62\u7684\u6548\u679c\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u70b9\uff0c\u4f7f\u7528<code>plt.Rectangle()<\/code>\u6765\u7ed8\u5236\u5916\u63a5\u77e9\u5f62\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u70b9\u7684\u5206\u5e03\u548c\u5916\u63a5\u77e9\u5f62\u7684\u5f62\u72b6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u9009\u62e9\u4e00\u5806\u70b9\u7684\u5916\u63a5\u77e9\u5f62\u65f6\uff0c\u6709\u51e0\u4e2a\u6838\u5fc3\u6b65\u9aa4\uff1a\u627e\u51fa\u6240\u6709\u70b9\u7684\u6700\u5c0f\u548c\u6700\u5927\u5750\u6807\u3001\u786e\u5b9a\u77e9\u5f62\u7684\u56db\u4e2a\u89d2\u70b9\u3001\u8ba1\u7b97\u77e9\u5f62\u7684\u8fb9\u957f\u3002\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1126492,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126484"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1126484"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126484\/revisions"}],"predecessor-version":[{"id":1126494,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126484\/revisions\/1126494"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126492"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}