from manim import *
import numpy as np
class PendulumDemo(Scene):
    def construct(self):
        title = Text("PhaseFlow：单摆的相空间轨迹", font="STLiti", font_size=32).to_edge(UP)
        self.play(FadeIn(title), run_time=0.6)
        axes = Axes(x_range=[-3, 3, 1], y_range=[-3, 3, 1], x_length=6, y_length=6,
                    axis_config={"include_tip": True})
        self.play(Create(axes), run_time=0.8)
        def deriv(state):
            theta, omega = state[0], state[1]
            return np.array([omega, -9.8 * np.sin(theta), 0.0])
        state_dot = Dot(point=axes.c2p(PI / 4, 0.0), color=YELLOW, radius=0.13)
        trace = TracedPath(state_dot.get_center, stroke_color=GREEN, stroke_width=4)
        self.add(state_dot, trace)
        self.play(PhaseFlow(deriv, state_dot, virtual_time=1.4, rate_func=linear), run_time=4.0)
        note = Text("初始角度 45 度、角速度 0；g/L=9.8 决定回复加速度", font="STLiti", font_size=21, color=YELLOW).to_edge(DOWN)
        self.play(FadeIn(note), run_time=0.4)
        self.wait(1.0)
