利用Python求阴影部分的面积实例代码
Python  /  管理员 发布于 7年前   334
一、前言说明
今天看到微信群里一道六年级数学题,如下图,求阴影部分面积
看起来似乎并不是很难,可是博主添加各种辅助线,写各种方法都没出来,不得已而改用写Python代码来求面积了
二、思路介绍
1.用Python将上图画在坐标轴上,主要是斜线函数和半圆函数
2.均匀的在长方形上面洒满豆子(假设是豆子),求阴影部分豆子占比*总面积
三、源码设计
1.做图源码
import matplotlib.pyplot as pltimport numpy as npdef init(): plt.xlabel('X') plt.ylabel('Y') fig = plt.gcf() fig.set_facecolor('lightyellow') fig.set_edgecolor("black") ax = plt.gca() ax.patch.set_facecolor("lightgray") # 设置ax区域背景颜色 ax.patch.set_alpha(0.1) # 设置ax区域背景颜色透明度 ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0))# 原下半函数def f1(px, r, a, b): return b - np.sqrt(r**2 - (px - a)**2)# 斜线函数def f2(px, m, n): return px*n/m# 斜线函数2def f3(px, m, n): return n-1*px*n/mif __name__ == '__main__': r = 4 # 圆半径 m = 8 # 宽 n = 4 # 高 a, b = (4, 4) # 圆心坐标 init() x = np.linspace(0, m, 100*m) y = np.linspace(0, n, 100*n) # 半圆形 y1 = f1(x, r, a, b) plt.plot(x, y1) # 矩形横线 plt.plot((x.min(), x.max()), (y.min(), y.min()), 'g') plt.plot((x.min(), x.max()), (y.max(), y.max()), 'g') plt.plot((x.max(), x.max()), (y.max()+2, y.max()+2), 'g') # 画点(8,6)避免图形变形 # 矩形纵向 plt.plot((x.min(), x.min()), (y.min(), y.max()), 'g') plt.plot((x.max(), x.max()), (y.min(), y.max()), 'g') # 斜线方法 y2 = f2(x, m, n) plt.plot(x, y2, 'purple') # 阴影部分填充 xf = x[np.where(x <= 0.5*x.max())] plt.fill_between(xf, y.min(), f1(xf, r, a, b), where=f1(xf, r, a, b) <= f2(xf, m, n), facecolor='y', interpolate=True) plt.fill_between(xf, y.min(), f2(xf, m, n), where=f1(xf, r, a, b) > f2(xf, m, n), facecolor='y', interpolate=True) # 半圆填充 plt.fill_between(x, y1, y.max(), facecolor='r', alpha=0.25) plt.show()Draw.py
2.计算源码,其中side是要不要计算图形边框上的点,理论上side只能为True;t设置越大运行时间越长也越精准
import numpy as npdef f1(px, r, a, b): return b - np.sqrt(r**2 - (px - a)**2)def f2(px, m, n): return px*n/mif __name__ == '__main__': r = 4 # 圆半径 m = 8 # 宽 n = 4 # 高 a, b = (4, 4) # 圆心坐标 t = 100 # 精度 xs = np.linspace(0, m, 2*t*m) ys = np.linspace(0, n, t*n) # 半圆形 y1 = f1(xs, r, a, b) # 斜线 y2 = f2(xs, m, n) numin = 0 numtotel = 0 side = True # 是否计算边框 for x in xs: for y in ys: if not side: if (x <= 0) | (x >= 8) | (y <= 0) | (y >= 4): continue numtotel += 1 if x >= 4: continue y1 = f1(x, r, a, b) y2 = f2(x, m, n) if y1 - y2 >= 0: if y2 - y > 0: numin += 1 if (y2 - y == 0) and side: numin += 1 elif y2 - y1 > 0: if y1 - y > 0: numin += 1 if (y2 - y == 0) and side: numin += 1 print(32*numin/numtotel)calc.py
四、最后小结
1.此种算法t为100时,阴影面积为1.268;t为1000时,阴影面积为1.253,已经非常接近正确答案(正确答案1.252)
2.举一反三,类似于这种不规则的面积,只要可以写出来函数,就可以求解面积.
2.下面有三种求解方法,第三种表示比大学高数还难看懂,你们呢?
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号