Python各类图形绘制—turtle与Matplotlib-33、多辅助线三角形角度计算
目录
Python各类图形绘制—turtle与Matplotlib-33、多辅助线三角形角度计算
前言
既然是学习数学,肯定会离不开各种图形,之前的文章中很多我都尽可能的不使用图来表示了,但是觉得不好,毕竟数学离开了图就会很抽象,所以我们这里单独的学习一下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)
# 设置参数和计算
side_length = 200 # 实际边长10的放大显示
angle_A = 60 # 顶角60°
height = side_length * math.sqrt(3)/2 # 正确计算等边三角形高度
# 将整个图形向左移动以留出空间给解答步骤
start_x = -300
start_y = -100
# 绘制等腰三角形
t.penup()
t.goto(start_x, start_y) # 新的起点
t.pendown()
t.color('black')
# 绘制底边BC
t.forward(side_length)
point_C = t.position()
point_B = (start_x, start_y)
# 绘制AC和AB
point_A = (start_x + side_length/2, start_y + height)
t.goto(point_A)
t.goto(point_B)
# 计算点D的位置(AD⊥BC)
point_D = (start_x + side_length/2, start_y) # D在BC中点
# 绘制AD(高线)
t.penup()
t.goto(point_A)
t.pendown()
t.color('red')
t.goto(point_D)
# 计算点E的位置(AE:AD=1:2)
e_x = point_A[0] # E在AD上
e_y = point_A[1] - height/3 # 从A向下1/3处
point_E = (e_x, e_y)
# 绘制E点和△ADE
t.color('blue')
t.goto(point_E)
t.goto(point_D)
# 添加角度标注(更准确的位置)
t.penup()
t.color('black')
# 标注顶角60°
t.goto(point_A[0], point_A[1]-20)
t.write("60°", font=("Arial", 14, "normal"))
# 标注底角60°
t.goto(point_B[0]+10, point_B[1]+10)
t.write("60°", font=("Arial", 14, "normal"))
t.goto(point_C[0]-30, point_C[1]+10)
t.write("60°", font=("Arial", 14, "normal"))
# 标注直角
t.goto(point_D[0]-25, point_D[1]+10)
t.write("90°", font=("Arial", 14, "normal"))
# 添加点的标注
t.penup()
t.color('black')
for point, label in [(point_A, 'A'), (point_B, 'B'),
(point_C, 'C'), (point_D, 'D'),
(point_E, 'E')]:
t.goto(point[0]-10, point[1]+10)
t.write(label, font=("Arial", 16, "normal"))
# 显示角度标注
t.goto(point_A[0], point_A[1]-20)
t.write("60°", font=("Arial", 14, "normal"))
t.goto(point_D[0]-20, point_D[1]+10)
t.write("90°", font=("Arial", 14, "normal"))
# 显示计算过程
t.goto(150, 150)
t.write("解答步骤:", font=("Arial", 16, "normal"))
steps = [
"1. 已知条件:",
" · AB = AC = 10",
" · ∠BAC = 60°",
" · AD ⊥ BC",
" · AE:AD = 1:2",
"",
"2. 计算AD(高):",
" · AD = 10 × sin(60°)",
f" · AD = {10 * math.sin(math.radians(60)):.2f}",
"",
"3. 计算AE:",
" · AE = AD × (1/2)",
f" · AE = {10 * math.sin(math.radians(60))/2:.2f}",
"",
"4. 计算△ADE面积:",
" · S△ADE = (1/2) × AD × DE",
" · DE = AD/2",
f" · S△ADE = {(10 * math.sin(math.radians(60)))**2/8:.2f}"
]
for i, step in enumerate(steps):
t.goto(150, 100 - i*25)
t.write(step, 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=(12, 8))
# 计算三角形顶点坐标
side_length = 6
height = side_length * math.sqrt(3)/2
# 定义顶点坐标
A = np.array([0, height])
B = np.array([-side_length/2, 0])
C = np.array([side_length/2, 0])
D = np.array([0, 0]) # D在BC中点
E = np.array([0, height/3]) # E在AD上,AE:AD=1:2
# 绘制三角形ABC
plt.plot([B[0], C[0]], [B[1], C[1]], 'b-', label='三角形边')
plt.plot([A[0], B[0]], [A[1], B[1]], 'b-')
plt.plot([A[0], C[0]], [A[1], C[1]], 'b-')
# 绘制AD和DE
plt.plot([A[0], D[0]], [A[1], D[1]], 'r--', label='高线AD')
plt.plot([D[0], E[0]], [D[1], E[1]], 'g-', label='线段DE')
plt.plot([A[0], E[0]], [A[1], E[1]], 'g-')
# 标注点
plt.text(A[0]-0.3, A[1]+0.2, 'A', fontsize=12)
plt.text(B[0]-0.5, B[1]-0.3, 'B', fontsize=12)
plt.text(C[0]+0.3, C[1]-0.3, 'C', fontsize=12)
plt.text(D[0]-0.5, D[1]-0.3, 'D', fontsize=12)
plt.text(E[0]+0.3, E[1], 'E', fontsize=12)
# 标注角度
plt.text(A[0]-0.3, A[1]-0.5, '60°', fontsize=10)
plt.text(B[0]+0.3, B[1]+0.2, '60°', fontsize=10)
plt.text(C[0]-0.5, C[1]+0.2, '60°', fontsize=10)
plt.text(D[0]-0.5, D[1]+0.2, '90°', fontsize=10)
# 添加计算过程
calculation_text = [
"解答步骤:",
"\n1. 已知条件:",
" · AB = AC = 10",
" · ∠BAC = 60°",
" · AD ⊥ BC",
" · AE:AD = 1:2",
"\n2. 计算AD(高):",
" · AD = 10 × sin(60°)",
f" · AD = {10 * math.sin(math.radians(60)):.2f}",
"\n3. 计算AE:",
" · AE = AD × (1/2)",
f" · AE = {10 * math.sin(math.radians(60))/2:.2f}",
"\n4. 计算△ADE面积:",
" · S△ADE = (1/2) × AD × DE",
" · DE = AD/2",
f" · S△ADE = {(10 * math.sin(math.radians(60)))**2/8:.2f}"
]
# 添加文本说明
plt.text(2, height/2, '\n'.join(calculation_text),
bbox=dict(facecolor='white', alpha=0.8, pad=10))
# 设置图形属性
plt.title('三角形面积计算')
plt.axis('equal')
plt.grid(True, linestyle='--', alpha=0.3)
plt.legend(loc='upper right')
# 设置坐标轴范围
plt.xlim(-3, 8)
plt.ylim(-1, 6)
# 显示图形
plt.tight_layout()
plt.show()
效果示例: