Python各类图形绘制—turtle与Matplotlib-29、计算两个圆相交部分面积
目录
Python各类图形绘制—turtle与Matplotlib-29、计算两个圆相交部分面积
前言
既然是学习数学,肯定会离不开各种图形,之前的文章中很多我都尽可能的不使用图来表示了,但是觉得不好,毕竟数学离开了图就会很抽象,所以我们这里单独的学习一下Python的各类图形绘制,包含绘制切线什么的,这样在数学学习的图像处理上就会好很多。
编号 | 中文名称 | 英文名称 | 图形说明 |
---|---|---|---|
1 | 点 | Point | 几何中最基本元素,无大小、长度、宽度、高度,仅表示位置,二维平面用坐标 (x, y) 确定位置 |
2 | 线段 | Line Segment | 直线上两点间的有限部分,有两个端点,可确定长度,由两端点坐标确定位置和长度 |
3 | 射线 | Ray | 由线段一端无限延长形成,有一个端点,另一端无限延伸,不可测量长度 |
4 | 直线 | Line | 没有端点,可向两端无限延伸,不可度量长度,平面直角坐标系中可用线性方程表示 |
5 | 角 | Angle | 由两条有公共端点的射线组成,公共端点是顶点,两条射线是边,用度数衡量大小 |
6 | 三角形 | Triangle | 同一平面内不在同一直线上的三条线段首尾顺次连接组成的封闭图形。按边分有等边(三边相等)、等腰(至少两边相等)、不等边三角形;按角分有锐角(三角皆锐角)、直角(一角为直角)、钝角(一角为钝角)三角形 |
7 | 四边形 | Quadrilateral | 由不在同一直线上的四条线段依次首尾相接围成的封闭的平面图形或立体图形。常见的有平行四边形(两组对边分别平行)、矩形(四个角为直角的平行四边形)、菱形(四条边相等的平行四边形)、正方形(四个角为直角且四条边相等的平行四边形)、梯形(一组对边平行,另一组对边不平行)等 |
8 | 五边形 | Pentagon | 由五条线段首尾相连组成的封闭图形,内角和为 540 度,正五边形五条边相等,五个角也相等 |
9 | 六边形 | Hexagon | 由六条线段首尾相连围成的封闭图形,内角和为 720 度,正六边形六条边相等,六个内角也相等,每个内角为 120 度 |
10 | 圆形 | Circle | 平面上到定点的距离等于定长的所有点组成的图形,定点称为圆心,定长称为半径。圆是轴对称图形,也是中心对称图形 |
11 | 椭圆 | Ellipse | 平面内到两个定点的距离之和等于常数(大于两定点间距离)的点的轨迹,这两个定点叫做椭圆的焦点,两焦点间的距离叫做焦距 |
12 | 扇形 | Sector | 一条圆弧和经过这条圆弧两端的两条半径所围成的图形,扇形面积与圆心角(顶角)、圆半径相关 |
13 | 弓形 | Segment of a circle | 由弦及其所对的弧组成的图形,可分为劣弧弓形(小于半圆)和优弧弓形(大于半圆) |
开发环境
系统:win11
开发语言:Python
使用工具:Jupyter Notebook
使用库:turtle与Matplotlib
turtle_demo
代码示例:
import turtle
import math
# 初始化画布
screen = turtle.Screen()
screen.title("圆形相交面积计算")
t = turtle.Turtle()
t.speed(0) # 最快速度
# 设置参数
radius = 100 # 放大半径便于观察
distance = 130 # 两圆圆心之间的距离
# 绘制第一个圆
t.penup()
t.goto(-200, 0) # 将第一个圆向左移动
t.pendown()
t.circle(radius)
# 绘制第二个圆
t.penup()
t.goto(-200 + distance, 0) # 相应调整第二个圆的位置
t.pendown()
t.color('red')
t.circle(radius)
# 显示计算过程
t.penup()
t.goto(50, 200) # 调整文字起始位置
t.color('black')
t.write("两圆相交面积计算过程:", font=("Arial", 16, "normal"))
t.goto(50, 160) # 后续文字位置相应调整
t.write(f"1. 圆的半径 = {radius} 单位", font=("Arial", 14, "normal"))
t.goto(150, 130)
t.write(f"2. 圆心距离 = {distance} 单位", font=("Arial", 14, "normal"))
t.goto(150, 100)
t.write("3. 相交面积计算:", font=("Arial", 14, "normal"))
t.goto(170, 70)
t.write(f"· 夹角 α = arccos({distance}/(2×{radius}))", font=("Arial", 14, "normal"))
t.goto(170, 40)
t.write(f"· α = {math.acos(distance/(2*radius)):.4f} 弧度", font=("Arial", 14, "normal"))
t.goto(170, 10)
t.write("· 相交面积公式 = 2 × r × r × (α - sin(α))", font=("Arial", 14, "normal"))
t.goto(170, -20)
t.write(f"· 相交面积 = 2 × {radius} × {radius} × ", font=("Arial", 14, "normal"))
t.goto(170, -50)
t.write(f" ({math.acos(distance/(2*radius)):.4f} - {math.sin(math.acos(distance/(2*radius))):.4f})",
font=("Arial", 14, "normal"))
t.goto(170, -80)
# 计算相交面积
intersection_area = 0 # 初始化变量
if distance < 2 * radius:
a = math.acos(distance/(2*radius))
intersection_area = 2 * radius**2 * (a - math.sin(a))
t.write(f"· 最终相交面积 = {intersection_area:.2f} 平方单位", font=("Arial", 14, "normal"))
# 隐藏画笔
t.hideturtle()
# 保持窗口显示
screen.mainloop()
效果示例:
Matplotlib_demo
代码示例:
import numpy as np
import matplotlib.pyplot as plt
import math
# 设置中文字体支持
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 创建图形
plt.figure(figsize=(13, 8))
# 设置圆的参数
radius = 3
distance = 4 # 两圆圆心之间的距离
# 绘制第一个圆(向右移动5个单位)
circle1 = plt.Circle((5, 0), radius, fill=False, color='blue', label='圆形1')
plt.gca().add_patch(circle1)
# 绘制第二个圆(向右移动5个单位)
circle2 = plt.Circle((5 + distance, 0), radius, fill=False, color='red', label='圆形2')
plt.gca().add_patch(circle2)
# 计算相交区域
if distance < 2 * radius:
# 计算相交面积
a = math.acos(distance/(2*radius))
intersection_area = 2 * radius**2 * (a - math.sin(a))
# 绘制相交区域(向右移动5个单位)
x = np.linspace(5 + distance/2 - radius, 5 + distance/2 + radius, 100)
y1 = np.sqrt(radius**2 - (x - 5)**2)
y2 = np.sqrt(radius**2 - (x - (5 + distance))**2)
y = np.minimum(y1, y2)
plt.fill_between(x, -y, y, color='gray', alpha=0.5, label='相交区域')
# 添加计算过程说明
calculation_text = [
"两圆相交面积计算过程:",
f"\n1. 圆的半径 = {radius} 单位",
f"2. 圆心距离 = {distance} 单位",
"\n3. 相交面积计算:",
f" · 夹角 α = arccos({distance}/(2×{radius}))",
f" · α = {math.acos(distance/(2*radius)):.4f} 弧度",
" · 相交面积公式 = 2 × r × r × (α - sin(α))",
f" · 相交面积 = 2 × {radius} × {radius} × ",
f" ({math.acos(distance/(2*radius)):.4f} - {math.sin(math.acos(distance/(2*radius))):.4f})",
f" · 最终相交面积 = {intersection_area:.2f} 平方单位"
]
# 添加说明文字(向右移动,确保不遮挡图形)
plt.text(12, 0, '\n'.join(calculation_text),
bbox=dict(facecolor='white', alpha=0.8, pad=10))
# 设置图形属性
plt.title('圆形相交面积计算')
plt.axis('equal')
plt.grid(True)
plt.legend(loc='upper right')
# 设置坐标轴范围(调整以适应
plt.xlim(-5, 20) # 扩大x轴范围
plt.ylim(-5, 5) # 扩大y轴范围
# 显示图形
plt.tight_layout()
plt.show()
效果示例: