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
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
2.举一反三,类似于这种不规则的面积,只要可以写出来函数,就可以求解面积.