利用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)文章
基于python的selenium全網(wǎng)最新超詳細(xì)教程
這篇文章主要介紹了基于python的selenium全網(wǎng)最新超詳細(xì)教程,本文內(nèi)容比較長,結(jié)合實例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2023-12-12
基于matplotlib中ion()和ioff()的使用詳解
這篇文章主要介紹了基于matplotlib中ion()和ioff()的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python使用selenium實現(xiàn)批量文件下載
這篇文章主要介紹了python使用selenium實現(xiàn)批量文件下載,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
pandas read_excel()和to_excel()函數(shù)解析
這篇文章主要介紹了pandas read_excel()和to_excel()函數(shù)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python2中的raw_input() 與 input()
這篇文章主要介紹了Python2中的raw_input() 與 input(),本文分析了它們的內(nèi)部實現(xiàn)和不同之處,并總結(jié)了什么情況下使用哪個函數(shù),需要的朋友可以參考下2015-06-06
python爬蟲模擬瀏覽器訪問-User-Agent過程解析
這篇文章主要介紹了python爬蟲模擬瀏覽器訪問-User-Agent過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

