{"id":1013896,"date":"2024-12-27T11:51:46","date_gmt":"2024-12-27T03:51:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013896.html"},"modified":"2024-12-27T11:51:48","modified_gmt":"2024-12-27T03:51:48","slug":"python%e5%a6%82%e4%bd%95%e6%a8%a1%e6%8b%9f%e6%8a%9b%e7%89%a9%e8%bf%90%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1013896.html","title":{"rendered":"python\u5982\u4f55\u6a21\u62df\u629b\u7269\u8fd0\u52a8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094153\/67d0b093-681c-457a-957b-ce80c0de8463.webp\" alt=\"python\u5982\u4f55\u6a21\u62df\u629b\u7269\u8fd0\u52a8\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6a21\u62df\u629b\u7269\u8fd0\u52a8\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7269\u7406\u5b66\u7684\u57fa\u672c\u516c\u5f0f\uff0c\u8003\u8651\u91cd\u529b\u52a0\u901f\u5ea6\u3001\u521d\u59cb\u901f\u5ea6\u3001\u53d1\u5c04\u89d2\u5ea6\u7b49\u56e0\u7d20\uff0c\u6765\u8ba1\u7b97\u7269\u4f53\u7684\u8fd0\u52a8\u8f68\u8ff9\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7f16\u7a0b\u5b9e\u73b0\u8fd0\u52a8\u65b9\u7a0b\u3001\u4f7f\u7528\u5faa\u73af\u6765\u66f4\u65b0\u4f4d\u7f6e\u3001\u53ef\u89c6\u5316\u8f68\u8ff9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u629b\u7269\u8fd0\u52a8\u7684\u57fa\u672c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u629b\u7269\u8fd0\u52a8\u662f\u6307\u7269\u4f53\u5728\u91cd\u529b\u4f5c\u7528\u4e0b\uff0c\u6cbf\u629b\u7269\u7ebf\u8f68\u8ff9\u8fd0\u52a8\u7684\u8fc7\u7a0b\u3002\u8981\u6a21\u62df\u8fd9\u79cd\u8fd0\u52a8\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u57fa\u672c\u7684\u7269\u7406\u516c\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fd0\u52a8\u65b9\u7a0b\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6c34\u5e73\u4f4d\u79fb\uff1a( x = v_0 \\cdot t \\cdot \\cos(\\theta) )<\/li>\n<li>\u5782\u76f4\u4f4d\u79fb\uff1a( y = v_0 \\cdot t \\cdot \\sin(\\theta) &#8211; \\frac{1}{2} \\cdot g \\cdot t^2 )<\/li>\n<\/ul>\n<p><p>\u5176\u4e2d\uff0c( v_0 ) \u662f\u521d\u901f\u5ea6\uff0c( \\theta ) \u662f\u53d1\u5c04\u89d2\u5ea6\uff0c( g ) \u662f\u91cd\u529b\u52a0\u901f\u5ea6\uff08\u7ea69.81 m\/s\u00b2\uff09\uff0c( t ) \u662f\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8003\u8651\u56e0\u7d20\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u521d\u901f\u5ea6<\/strong>\uff1a\u5f71\u54cd\u629b\u7269\u7ebf\u7684\u9ad8\u5ea6\u548c\u8ddd\u79bb\u3002<\/li>\n<li><strong>\u53d1\u5c04\u89d2\u5ea6<\/strong>\uff1a\u5f71\u54cd\u8f68\u8ff9\u7684\u5f62\u72b6\u3002<\/li>\n<li><strong>\u91cd\u529b\u52a0\u901f\u5ea6<\/strong>\uff1a\u5f71\u54cd\u7269\u4f53\u7684\u4e0b\u964d\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u7f16\u7a0b\u6a21\u62df\u629b\u7269\u8fd0\u52a8<\/h3>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u521d\u59cb\u6761\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u62df\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u9700\u8981\u8bbe\u5b9a\u521d\u59cb\u6761\u4ef6\uff0c\u5305\u62ec\u521d\u901f\u5ea6\u3001\u53d1\u5c04\u89d2\u5ea6\u548c\u91cd\u529b\u52a0\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u521d\u59cb\u6761\u4ef6<\/strong><\/h2>\n<p>initial_velocity = 20  # \u521d\u901f\u5ea6 (m\/s)<\/p>\n<p>angle_of_projection = 45  # \u53d1\u5c04\u89d2\u5ea6 (degrees)<\/p>\n<p>g = 9.81  # \u91cd\u529b\u52a0\u901f\u5ea6 (m\/s^2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8ba1\u7b97\u65f6\u95f4\u6b65\u957f\u548c\u603b\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u884c\u6a21\u62df\uff0c\u6211\u4eec\u9700\u8981\u51b3\u5b9a\u65f6\u95f4\u6b65\u957f\uff08\u5373\u6bcf\u6b21\u8ba1\u7b97\u7684\u65f6\u95f4\u95f4\u9694\uff09\u4ee5\u53ca\u6a21\u62df\u7684\u603b\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">time_step = 0.01  # \u65f6\u95f4\u6b65\u957f (s)<\/p>\n<p>total_time = 2 * initial_velocity * math.sin(math.radians(angle_of_projection)) \/ g<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u5faa\u73af\u66f4\u65b0\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u6b65\u66f4\u65b0\u7269\u4f53\u7684\u4f4d\u7f6e\uff0c\u76f4\u5230\u5b83\u843d\u5730\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4f4d\u7f6e\u548c\u65f6\u95f4<\/p>\n<p>x, y = 0, 0<\/p>\n<p>t = 0<\/p>\n<h2><strong>\u5217\u8868\u7528\u4e8e\u5b58\u50a8\u8f68\u8ff9\u70b9<\/strong><\/h2>\n<p>trajectory_x = []<\/p>\n<p>trajectory_y = []<\/p>\n<p>while y &gt;= 0:<\/p>\n<p>    # \u66f4\u65b0\u65f6\u95f4<\/p>\n<p>    t += time_step<\/p>\n<p>    # \u66f4\u65b0\u4f4d\u7f6e<\/p>\n<p>    x = initial_velocity * t * math.cos(math.radians(angle_of_projection))<\/p>\n<p>    y = initial_velocity * t * math.sin(math.radians(angle_of_projection)) - 0.5 * g * t  2<\/p>\n<p>    # \u5b58\u50a8\u8f68\u8ff9\u70b9<\/p>\n<p>    trajectory_x.append(x)<\/p>\n<p>    trajectory_y.append(y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u53ef\u89c6\u5316\u8fd0\u52a8\u8f68\u8ff9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>matplotlib<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u7b97\u51fa\u7684\u8f68\u8ff9\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot(trajectory_x, trajectory_y)<\/p>\n<p>plt.title(&#39;Projectile Motion&#39;)<\/p>\n<p>plt.xlabel(&#39;Horizontal Distance (m)&#39;)<\/p>\n<p>plt.ylabel(&#39;Vertical Distance (m)&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f18\u5316\u548c\u6269\u5c55\u6a21\u62df<\/h3>\n<\/p>\n<p><h4>1. \u8003\u8651\u7a7a\u6c14\u963b\u529b<\/h4>\n<\/p>\n<p><p>\u5728\u73b0\u5b9e\u4e16\u754c\u4e2d\uff0c\u7a7a\u6c14\u963b\u529b\u4f1a\u5f71\u54cd\u629b\u7269\u8fd0\u52a8\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u8fd0\u52a8\u65b9\u7a0b\u4e2d\u6dfb\u52a0\u7a7a\u6c14\u963b\u529b\u9879\u6765\u6a21\u62df\u66f4\u771f\u5b9e\u7684\u8fd0\u52a8\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u79cd\u521d\u59cb\u6761\u4ef6\u7684\u6a21\u62df<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5141\u8bb8\u7528\u6237\u8f93\u5165\u4e0d\u540c\u7684\u521d\u901f\u5ea6\u548c\u53d1\u5c04\u89d2\u5ea6\uff0c\u4ee5\u89c2\u5bdf\u4e0d\u540c\u6761\u4ef6\u4e0b\u7684\u8fd0\u52a8\u8f68\u8ff9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simulate_projectile(initial_velocity, angle_of_projection):<\/p>\n<p>    # \u8ba1\u7b97\u5e76\u53ef\u89c6\u5316\u8f68\u8ff9<\/p>\n<p>    # ...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4e09\u7ef4\u629b\u7269\u8fd0\u52a8<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5f15\u5165\u7b2c\u4e09\u4e2a\u7ef4\u5ea6\uff08\u5373z\u8f74\uff09\uff0c\u53ef\u4ee5\u6a21\u62df\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7684\u629b\u7269\u8fd0\u52a8\uff0c\u8fd9\u9700\u8981\u8003\u8651\u521d\u59cb\u901f\u5ea6\u5728\u4e09\u4e2a\u65b9\u5411\u4e0a\u7684\u5206\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7Python\u6a21\u62df\u629b\u7269\u8fd0\u52a8\uff0c\u6211\u4eec\u4e0d\u4ec5\u80fd\u591f\u52a0\u6df1\u5bf9\u7269\u7406\u8fd0\u52a8\u7684\u7406\u89e3\uff0c\u8fd8\u80fd\u5e94\u7528\u4e8e\u6e38\u620f\u5f00\u53d1\u3001\u5de5\u7a0b\u8ba1\u7b97\u7b49\u9886\u57df\u3002\u901a\u8fc7\u4e0d\u65ad\u4f18\u5316\u548c\u6269\u5c55\u6a21\u62df\u6a21\u578b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u548c\u771f\u5b9e\u7684\u8fd0\u52a8\u6548\u679c\uff0c\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u629b\u7269\u8fd0\u52a8\u7684\u57fa\u672c\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u629b\u7269\u8fd0\u52a8\u662f\u6307\u7269\u4f53\u5728\u91cd\u529b\u4f5c\u7528\u4e0b\u6cbf\u7740\u629b\u7269\u7ebf\u8f68\u8ff9\u8fd0\u52a8\u7684\u8fc7\u7a0b\u3002\u5b83\u7531\u4e24\u4e2a\u72ec\u7acb\u7684\u8fd0\u52a8\u7ec4\u6210\uff1a\u6c34\u5e73\u8fd0\u52a8\u548c\u5782\u76f4\u8fd0\u52a8\u3002\u6c34\u5e73\u8fd0\u52a8\u901a\u5e38\u662f\u5300\u901f\u7684\uff0c\u800c\u5782\u76f4\u8fd0\u52a8\u5219\u662f\u53d7\u91cd\u529b\u5f71\u54cd\u7684\u52a0\u901f\u8fd0\u52a8\u3002\u7406\u89e3\u8fd9\u4e24\u4e2a\u90e8\u5206\u7684\u5173\u7cfb\u662f\u6a21\u62df\u629b\u7269\u8fd0\u52a8\u7684\u5173\u952e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u629b\u7269\u8fd0\u52a8\u7684\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u5f62\u5e93\uff0c\u5982Matplotlib\u6216Pygame\uff0c\u6765\u5b9e\u73b0\u629b\u7269\u8fd0\u52a8\u7684\u53ef\u89c6\u5316\u3002\u9996\u5148\uff0c\u8ba1\u7b97\u7269\u4f53\u5728\u6bcf\u4e2a\u65f6\u95f4\u70b9\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u4f4d\u7f6e\u7ed8\u5236\u5728\u56fe\u5f62\u7a97\u53e3\u4e2d\u3002\u901a\u8fc7\u8bbe\u7f6e\u65f6\u95f4\u6b65\u957f\uff0c\u53ef\u4ee5\u4f7f\u8fd0\u52a8\u770b\u8d77\u6765\u66f4\u52a0\u8fde\u8d2f\u548c\u81ea\u7136\u3002<\/p>\n<p><strong>\u5982\u4f55\u6839\u636e\u4e0d\u540c\u7684\u521d\u59cb\u6761\u4ef6\u8c03\u6574\u629b\u7269\u8fd0\u52a8\u7684\u8f68\u8ff9\uff1f<\/strong><br \/>\u6539\u53d8\u521d\u59cb\u901f\u5ea6\u548c\u53d1\u5c04\u89d2\u5ea6\u5c06\u76f4\u63a5\u5f71\u54cd\u629b\u7269\u8fd0\u52a8\u7684\u8f68\u8ff9\u3002\u901a\u8fc7\u4fee\u6539\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u89c2\u5bdf\u5230\u7269\u4f53\u98de\u884c\u8ddd\u79bb\u548c\u9ad8\u5ea6\u7684\u53d8\u5316\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u4e0d\u540c\u7684\u521d\u59cb\u6761\u4ef6\u6765\u52a8\u6001\u8c03\u6574\u6a21\u62df\u7ed3\u679c\uff0c\u4ece\u800c\u6df1\u5165\u7406\u89e3\u629b\u7269\u8fd0\u52a8\u7684\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6a21\u62df\u629b\u7269\u8fd0\u52a8\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7269\u7406\u5b66\u7684\u57fa\u672c\u516c\u5f0f\uff0c\u8003\u8651\u91cd\u529b\u52a0\u901f\u5ea6\u3001\u521d\u59cb\u901f\u5ea6\u3001\u53d1\u5c04\u89d2\u5ea6\u7b49\u56e0\u7d20\uff0c\u6765\u8ba1\u7b97\u7269 [&hellip;]","protected":false},"author":3,"featured_media":1013900,"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\/1013896"}],"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=1013896"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013896\/revisions"}],"predecessor-version":[{"id":1013904,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013896\/revisions\/1013904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013900"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}