溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

利用Python求陰影部分的面積實(shí)例代碼

發(fā)布時(shí)間:2020-09-29 13:33:28 來(lái)源:腳本之家 閱讀:393 作者:Vrapile 欄目:開(kāi)發(fā)技術(shù)

一、前言說(shuō)明

今天看到微信群里一道六年級(jí)數(shù)學(xué)題,如下圖,求陰影部分面積

利用Python求陰影部分的面積實(shí)例代碼

看起來(lái)似乎并不是很難,可是博主添加各種輔助線,寫(xiě)各種方法都沒(méi)出來(lái),不得已而改用寫(xiě)Python代碼來(lái)求面積了

二、思路介紹

1.用Python將上圖畫(huà)在坐標(biāo)軸上,主要是斜線函數(shù)和半圓函數(shù)

利用Python求陰影部分的面積實(shí)例代碼

2.均勻的在長(zhǎng)方形上面灑滿豆子(假設(shè)是豆子),求陰影部分豆子占比*總面積

利用Python求陰影部分的面積實(shí)例代碼

三、源碼設(shè)計(jì)

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') # 畫(huà)點(diǎn)(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.計(jì)算源碼,其中side是要不要計(jì)算圖形邊框上的點(diǎn),理論上side只能為T(mén)rue;t設(shè)置越大運(yùn)行時(shí)間越長(zhǎng)也越精準(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 # 是否計(jì)算邊框
 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時(shí),陰影面積為1.268;t為1000時(shí),陰影面積為1.253,已經(jīng)非常接近正確答案(正確答案1.252)

  2.舉一反三,類(lèi)似于這種不規(guī)則的面積,只要可以寫(xiě)出來(lái)函數(shù),就可以求解面積.

  2.下面有三種求解方法,第三種表示比大學(xué)高數(shù)還難看懂,你們呢?

利用Python求陰影部分的面積實(shí)例代碼

利用Python求陰影部分的面積實(shí)例代碼

利用Python求陰影部分的面積實(shí)例代碼

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI