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.

93 lines
2.5 KiB

5 months ago
import manim
from manim import *
import numpy as np
class PolarGraph(Scene):
def construct(self):
# 初始化参数跟踪器
e = ValueTracker(0.01)
# ===== 极坐标系部分 =====
# 修正1使用PolarPlane的正确拼写
polar_plane = PolarPlane(
radius_max=3,
azimuth_step=6 # 极角刻度间隔(单位:度)
).add_coordinates().shift(LEFT * 2)
# 修正2使用正确的参数方程
polar_graph = always_redraw(
lambda: ParametricFunction(
lambda t: polar_plane.polar_to_point(2 * np.sin(3 * t), t),
t_range=[0, e.get_value()],
color=GREEN
)
)
polar_dot = always_redraw(
lambda: Dot(
color=GREEN,
radius=0.08
).move_to(polar_graph.get_end())
)
# ===== 直角坐标系部分 =====
axes = Axes(
x_range=[0, 4, 1],
y_range=[-3, 3, 1],
x_length=3,
y_length=3,
axis_config={"color": BLUE}
).shift(RIGHT * 4)
axes.add_coordinates()
cartesian_graph = always_redraw(
lambda: axes.plot(
lambda x: 2 * np.sin(3 * x),
x_range=[0, e.get_value()],
color=GREEN
)
)
cartesian_dot = always_redraw(
lambda: Dot(
color=GREEN,
radius=0.08
).move_to(cartesian_graph.get_end())
)
# 公式标签
title = MathTex(r"f(\theta) = 2\sin(3\theta)", color=GREEN).next_to(axes, UP)
# ===== 动画序列 =====
# 修正3正确拼写LaggedStart
self.play(
LaggedStart(
Create(polar_plane),
Create(axes),
Write(title),
lag_ratio=0.5
),
run_time=3
)
self.add(polar_graph, cartesian_graph, polar_dot, cartesian_dot)
# 参数变化动画
self.play(
e.animate.set_value(PI),
run_time=10,
rate_func=linear
)
self.wait()
if __name__ == '__main__':
config = {
"quality": "low_quality",
"preview": True,
"media_dir": "./output"
}
with manim.tempconfig(config):
scene = PolarGraph()
scene.render()