利用Python求陰影部分的面積實例代碼
一、前言說明
今天看到微信群里一道六年級數(shù)學(xué)題,如下圖,求陰影部分面積
看起來似乎并不是很難,可是博主添加各種輔助線,寫各種方法都沒出來,不得已而改用寫Python代碼來求面積了
二、思路介紹
1.用Python將上圖畫在坐標(biāo)軸上,主要是斜線函數(shù)和半圓函數(shù)
2.均勻的在長方形上面灑滿豆子(假設(shè)是豆子),求陰影部分豆子占比*總面積
三、源碼設(shè)計
1.做圖源碼
import matplotlib.pyplot as plt import numpy as np def 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") # 設(shè)置ax區(qū)域背景顏色 ax.patch.set_alpha(0.1) # 設(shè)置ax區(qū)域背景顏色透明度 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)) # 原下半函數(shù) def f1(px, r, a, b): return b - np.sqrt(r**2 - (px - a)**2) # 斜線函數(shù) def f2(px, m, n): return px*n/m # 斜線函數(shù)2 def f3(px, m, n): return n-1*px*n/m if __name__ == '__main__': r = 4 # 圓半徑 m = 8 # 寬 n = 4 # 高 a, b = (4, 4) # 圓心坐標(biāo) 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設(shè)置越大運行時間越長也越精準(zhǔn)
import numpy as np def f1(px, r, a, b): return b - np.sqrt(r**2 - (px - a)**2) def f2(px, m, n): return px*n/m if __name__ == '__main__': r = 4 # 圓半徑 m = 8 # 寬 n = 4 # 高 a, b = (4, 4) # 圓心坐標(biāo) 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
四、最后小結(jié)
1.此種算法t為100時,陰影面積為1.268;t為1000時,陰影面積為1.253,已經(jīng)非常接近正確答案(正確答案1.252)
2.舉一反三,類似于這種不規(guī)則的面積,只要可以寫出來函數(shù),就可以求解面積.
2.下面有三種求解方法,第三種表示比大學(xué)高數(shù)還難看懂,你們呢?
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Python3.4 tkinter,PIL圖片轉(zhuǎn)換
我們給大家整理了關(guān)于Python3.4 tkinter,PIL圖片轉(zhuǎn)換的相關(guān)完整代碼,大家可以學(xué)習(xí)測試下。2018-06-06python實現(xiàn)點擊按鈕修改數(shù)據(jù)的方法
今天小編就為大家分享一篇python實現(xiàn)點擊按鈕修改數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python?Decorator的設(shè)計模式演繹過程解析
本文主要梳理了Python?decorator的實現(xiàn)思路,解釋了為什么Python?decorator是現(xiàn)在這個樣子,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07淺談python 導(dǎo)入模塊和解決文件句柄找不到問題
今天小編就為大家分享一篇淺談python 導(dǎo)入模塊和解決文件句柄找不到問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12