You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.1 KiB

5 months ago
from manim import *
5 months ago
import numpy as np
class PythagoreanProof(Scene):
def update_step(self, current_text, new_content, duration=1.5):
"""带淡入淡出效果的步骤更新"""
5 months ago
new_text = Text(new_content, font="Microsoft YaHei", font_size=32).to_edge(UP, buff=0.8)
5 months ago
new_text.set_color_by_gradient(GOLD, ORANGE)
if current_text:
self.play(
5 months ago
FadeOut(current_text, shift=UP * 0.3),
FadeIn(new_text, shift=DOWN * 0.3),
5 months ago
run_time=0.8
)
else:
self.play(FadeIn(new_text, shift=DOWN))
self.wait(duration)
return new_text
def create_polygon(self, points, color, fill_opacity=0.8):
"""创建带样式的多边形"""
return Polygon(*points,
color=color,
fill_opacity=fill_opacity,
stroke_width=4)
5 months ago
def construct(self):
5 months ago
current_step = None
5 months ago
elements = VGroup()
5 months ago
5 months ago
# === 步骤1使用NumPy数组定义坐标 ===
5 months ago
current_step = self.update_step(None, "步骤1绘制直角三角形")
5 months ago
# 定义三角形顶点使用NumPy数组
A = np.array([-2.0, -1.0, 0.0])
B = np.array([2.0, -1.0, 0.0])
C = np.array([1.0, 1.5, 0.0])
triangle = self.create_polygon([A, B, C], BLUE)
5 months ago
elements.add(triangle)
self.play(Create(triangle), run_time=2)
self.wait()
# === 步骤2构建三个正方形 ===
current_step = self.update_step(current_step, "步骤2构建三个正方形")
5 months ago
# 计算向量
vector_AC = C - A
vector_BC = C - B
# 构建正方形坐标点(使用向量运算)
5 months ago
square_a = Square(side_length=4, color=ORANGE).shift(DOWN * 2)
5 months ago
5 months ago
square_b_points = [
5 months ago
A,
C,
C + vector_AC * 0.8,
A + vector_AC * 0.8
5 months ago
]
square_b = self.create_polygon(square_b_points, ORANGE)
square_c_points = [
5 months ago
B,
C,
C + vector_BC * 0.8,
B + vector_BC * 0.8
5 months ago
]
square_c = self.create_polygon(square_c_points, ORANGE)
5 months ago
self.play(
LaggedStart(
5 months ago
Create(square_a),
Create(square_b),
Create(square_c),
lag_ratio=0.4
5 months ago
),
run_time=3
)
5 months ago
elements.add(square_a, square_b, square_c)
self.wait()
# === 步骤3面积变换演示 ===
current_step = self.update_step(current_step, "步骤3面积恒等变换")
# 创建可移动三角形
5 months ago
moving_tri_points = [C, A, A + (A - C) * 0.8]
moving_tri = self.create_polygon(moving_tri_points, YELLOW)
5 months ago
self.play(Create(moving_tri), run_time=1)
5 months ago
self.play(
5 months ago
Rotate(
moving_tri,
angle=-PI / 2,
5 months ago
about_point=A,
5 months ago
rate_func=smooth
),
run_time=2
)
self.wait()
# === 步骤4最终结论 ===
current_step = self.update_step(current_step, "步骤4得出结论")
# 数学公式
formula = MathTex(r"a^2 + b^2 = c^2", color=GOLD).scale(2)
formula_box = SurroundingRectangle(formula, color=WHITE, buff=0.5)
self.play(
FadeOut(elements),
FadeOut(moving_tri),
run_time=1
5 months ago
)
self.play(
5 months ago
DrawBorderThenFill(formula_box),
Write(formula),
5 months ago
run_time=2
)
self.wait(3)
5 months ago
if __name__ == "__main__":
config = {
"quality": "high_quality",
"preview": True,
"media_dir": "./output",
"pixel_height": 1080,
"pixel_width": 1920,
5 months ago
"background_color": "#333333", # 深色背景
"format": "png" # 解决部分系统渲染问题
5 months ago
}
with tempconfig(config):
scene = PythagoreanProof()
scene.render()