Python各类图形绘制—turtle与Matplotlib-23、四边形阴影面积计算
目录
Python各类图形绘制—turtle与Matplotlib-23、四边形阴影面积计算
前言
既然是学习数学,肯定会离不开各种图形,之前的文章中很多我都尽可能的不使用图来表示了,但是觉得不好,毕竟数学离开了图就会很抽象,所以我们这里单独的学习一下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
# 参数设置
top_base = 160
bottom_base = 240
height = 180
shade_height = 60
# 初始化画布
screen = turtle.Screen()
screen.setup(1000, 800)
screen.title("梯形面积计算")
turtle.speed(0)
turtle.hideturtle()
# 创建绘图海龟
drawer = turtle.Turtle()
drawer.speed(0)
drawer.hideturtle()
# 绘制梯形函数
def draw_trapezoid(t, top, bottom, height):
left_offset = (bottom - top)/2
t.penup()
t.goto(-bottom/2, -height/2) # 左下角
t.pendown()
t.goto(bottom/2, -height/2) # 右下角
t.goto(top/2, height/2) # 右上角
t.goto(-top/2, height/2) # 左上角
t.goto(-bottom/2, -height/2) # 闭合图形
# 绘制主梯形
drawer.pencolor('blue')
drawer.pensize(2)
draw_trapezoid(drawer, top_base, bottom_base, height)
# 绘制阴影平行四边形
drawer.penup()
drawer.goto(-top_base/2, height/2 - shade_height)
drawer.pendown()
drawer.fillcolor('gray')
drawer.begin_fill()
for _ in range(2):
drawer.forward(top_base)
drawer.right(60)
drawer.forward(shade_height * 2)
drawer.right(120)
drawer.end_fill()
# 标注功能
def add_annotation(x, y, text, color):
ann = turtle.Turtle()
ann.hideturtle()
ann.penup()
ann.goto(x, y)
ann.color(color)
ann.write(text, align="center", font=("SimHei", 12, "normal"))
# 添加标注
add_annotation(0, -height/2 - 30, f"下底长度: {bottom_base}单位", 'black')
add_annotation(0, height/2 + 20, f"上底长度: {top_base}单位", 'black')
add_annotation(-bottom_base/2 - 50, 0, f"总高度: {height}单位", 'red')
add_annotation(top_base/2 + 30, height/2 - shade_height, f"阴影高度: {shade_height}单位", 'blue')
# 解题步骤显示
steps_turtle = turtle.Turtle()
steps_turtle.hideturtle()
steps_turtle.penup()
steps_turtle.goto(screen.window_width()/2 - 250, screen.window_height()/2 - 80)
steps = [
"解题步骤:",
"1. 梯形面积 = (上底+下底)×高/2",
f" = ({top_base}+{bottom_base})×{height}/2 = {(top_base+bottom_base)*height/2}",
"2. 阴影平行四边形面积 = 底边 × 高",
f" 底边 = {top_base}单位(上底长度)",
f" 高 = {shade_height}单位",
f" 阴影面积 = {top_base}×{shade_height} = {top_base*shade_height}",
f"最终阴影面积: {top_base*shade_height}平方单位"
]
for step in steps:
steps_turtle.write(step, align="right", font=("KaiTi", 14, "normal"))
steps_turtle.sety(steps_turtle.ycor() - 30)
turtle.done()
效果示例:
Matplotlib_demo
代码示例:
import matplotlib.pyplot as plt
import numpy as np
# 参数设置
top_base = 160 # 上底
bottom_base = 240 # 下底
height = 180 # 梯形高度
shade_height = 60 # 阴影区域高度
# 创建画布(保留原有画布设置)
fig, ax = plt.subplots(figsize=(10, 7))
ax.set_xlim(-150, 250)
ax.set_ylim(-50, height+50)
# 计算梯形顶点坐标
trapezoid = np.array([
[-bottom_base/2, 0],
[bottom_base/2, 0],
[top_base/2, height],
[-top_base/2, height],
[-bottom_base/2, 0]
])
# 绘制梯形主体(保留原有绘图逻辑)
ax.plot(trapezoid[:,0], trapezoid[:,1], 'b-', linewidth=2)
# 绘制阴影区域(新增平行四边形阴影)
shade_points = np.array([
[-top_base/2, height - shade_height],
[top_base/2, height - shade_height],
[top_base/2 + (bottom_base-top_base)/2, height - shade_height*2],
[-top_base/2 + (bottom_base-top_base)/2, height - shade_height*2],
[-top_base/2, height - shade_height]
])
ax.fill(shade_points[:,0], shade_points[:,1], 'gray', alpha=0.5)
# 添加标注系统(修改标注内容)
annotations = [
# (x坐标, y坐标, 文本内容, 垂直对齐, 水平对齐, 颜色)
(0, -20, f"下底长度: {bottom_base}单位", 'bottom', 'center', 'black'),
(0, height+20, f"上底长度: {top_base}单位", 'top', 'center', 'black'),
(-bottom_base/2-30, height/2, f"总高度: {height}单位", 'center', 'right', 'red'),
(shade_points[2][0]+10, shade_points[2][1], f"阴影高度: {shade_height}单位", 'center', 'left', 'blue')
]
for x, y, text, va, ha, color in annotations:
ax.text(x, y, text, color=color, fontsize=12,
fontfamily='SimHei', va=va, ha=ha) # 分离垂直和水平对齐参数
# 更新解题步骤(修改计算逻辑)
steps = [
"解题步骤:",
"1. 梯形面积 = (上底+下底)×高/2",
f" = ({top_base}+{bottom_base})×{height}/2 = {(top_base+bottom_base)*height/2}",
"2. 阴影平行四边形面积 = 底边 × 高",
f" 底边 = {top_base}单位(上底长度)",
f" 高 = {shade_height}单位",
f" 阴影面积 = {top_base}×{shade_height} = {top_base*shade_height}",
f"最终阴影面积: {top_base*shade_height}平方单位"
]
# 调整步骤显示位置(防止重叠)
ax.text(0.97, 0.82, '\n'.join(steps), transform=ax.transAxes,
fontsize=13, fontfamily='KaiTi', linespacing=1.5,
verticalalignment='top', horizontalalignment='right',
bbox=dict(facecolor='white', alpha=0.8))
plt.show()
效果示例: