from manim import *
import numpy as np
class HarmonicDemo(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):
            return np.array([state[1], -state[0], 0.0])
        state_dot = Dot(point=axes.c2p(1.0, 0.0), color=YELLOW, radius=0.13)
        trace = TracedPath(state_dot.get_center, stroke_color=BLUE, stroke_width=4)
        self.add(state_dot, trace)
        self.play(PhaseFlow(deriv, state_dot, virtual_time=4.0, rate_func=linear), run_time=4.0)
        note = Text("向量场由 dx/dt=v、dv/dt=-x 定义，轨迹是一条闭合曲线", font="STLiti", font_size=21, color=YELLOW).to_edge(DOWN)
        self.play(FadeIn(note), run_time=0.4)
        self.wait(1.0)
