Python求陰影部分面積


一、前言說明

  今天看到微信群里一道六年級數學題,如下圖,求陰影部分面積

    

  看起來似乎並不是很難,可是博主添加各種輔助線,寫各種方法都沒出來,不得已而改用寫Python代碼來求面積了

 

二、思路介紹

  1.用Python將上圖畫在坐標軸上,主要是斜線函數和半圓函數

    

 

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

    

 

三、源碼設計

  1.做圖源碼 

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 
 5 def init():
 6     plt.xlabel('X')
 7     plt.ylabel('Y')
 8 
 9     fig = plt.gcf()
10     fig.set_facecolor('lightyellow')
11     fig.set_edgecolor("black")
12 
13     ax = plt.gca()
14     ax.patch.set_facecolor("lightgray")  # 設置ax區域背景顏色               
15     ax.patch.set_alpha(0.1)  # 設置ax區域背景顏色透明度 
16     ax.spines['right'].set_color('none')
17     ax.spines['top'].set_color('none')
18     ax.xaxis.set_ticks_position('bottom')
19     ax.yaxis.set_ticks_position('left')
20     ax.spines['bottom'].set_position(('data', 0))
21     ax.spines['left'].set_position(('data', 0))
22 
23 
24 # 原下半函數
25 def f1(px, r, a, b):
26     return b - np.sqrt(r**2 - (px - a)**2)
27 
28 
29 # 斜線函數
30 def f2(px, m, n):
31     return px*n/m
32 
33 
34 # 斜線函數2
35 def f3(px, m, n):
36     return n-1*px*n/m
37 
38 
39 if __name__ == '__main__':
40     r = 4  # 圓半徑
41     m = 8  #
42     n = 4  #
43     a, b = (4, 4)  # 圓心坐標
44     init()
45 
46     x = np.linspace(0, m, 100*m)
47     y = np.linspace(0, n, 100*n)
48 
49     # 半圓形
50     y1 = f1(x, r, a, b)
51     plt.plot(x, y1)
52     # 矩形橫線
53     plt.plot((x.min(), x.max()), (y.min(), y.min()), 'g')
54     plt.plot((x.min(), x.max()), (y.max(), y.max()), 'g')
55     plt.plot((x.max(), x.max()), (y.max()+2, y.max()+2), 'g')  # 畫點(8,6)避免圖形變形
56     # 矩形縱向
57     plt.plot((x.min(), x.min()), (y.min(), y.max()), 'g')
58     plt.plot((x.max(), x.max()), (y.min(), y.max()), 'g')
59     # 斜線方法
60     y2 = f2(x, m, n)
61     plt.plot(x, y2, 'purple')
62 
63     # 陰影部分填充
64     xf = x[np.where(x <= 0.5*x.max())]
65     plt.fill_between(xf, y.min(), f1(xf, r, a, b), where=f1(xf, r, a, b) <= f2(xf, m, n),
66                      facecolor='y', interpolate=True)
67     plt.fill_between(xf, y.min(), f2(xf, m, n), where=f1(xf, r, a, b) > f2(xf, m, n),
68                      facecolor='y', interpolate=True)
69     # 半圓填充
70     plt.fill_between(x, y1, y.max(), facecolor='r', alpha=0.25)
71     plt.show()
Draw.py

 

   2.計算源碼,其中side是要不要計算圖形邊框上的點,理論上side只能為True;t設置越大運行時間越長也越精准

 1 import numpy as np
 2 
 3 
 4 def f1(px, r, a, b):
 5     return b - np.sqrt(r**2 - (px - a)**2)
 6 
 7 
 8 def f2(px, m, n):
 9     return px*n/m
10 
11 
12 if __name__ == '__main__':
13     r = 4  # 圓半徑
14     m = 8  #
15     n = 4  #
16     a, b = (4, 4)  # 圓心坐標
17     t = 100  # 精度
18 
19     xs = np.linspace(0, m, 2*t*m)
20     ys = np.linspace(0, n, t*n)
21 
22     # 半圓形
23     y1 = f1(xs, r, a, b)
24     # 斜線
25     y2 = f2(xs, m, n)
26 
27     numin = 0
28     numtotel = 0
29     side = True  # 是否計算邊框
30     for x in xs:
31         for y in ys:
32             if not side:
33                 if (x <= 0) | (x >= 8) | (y <= 0) | (y >= 4):
34                     continue
35             numtotel += 1
36             if x >= 4:
37                 continue
38             y1 = f1(x, r, a, b)
39             y2 = f2(x, m, n)
40             if y1 - y2 >= 0:
41                 if y2 - y > 0:
42                     numin += 1
43                 if (y2 - y == 0) and side:
44                     numin += 1
45             elif y2 - y1 > 0:
46                 if y1 - y > 0:
47                     numin += 1
48                 if (y2 - y == 0) and side:
49                     numin += 1
50 
51     print(32*numin/numtotel)
calc.py

 

四、最后小結

  1.此種算法t為100時,陰影面積為1.268;t為1000時,陰影面積為1.253,已經非常接近正確答案(正確答案1.252)

  2.舉一反三,類似於這種不規則的面積,只要可以寫出來函數,就可以求解面積.

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

     

    

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM