from manim import *
import numpy as np
import sympy as sp
def head(cn, en):
    t = Text(cn, font="STLiti", font_size=27).to_edge(UP)
    s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10)
    return VGroup(t, s)
def foot(cn):
    return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
class CatchUp(Scene):
    def construct(self):
        self.add(head("追及问题：列方程求相遇时间", "SymPy / solve"))
        t = sp.symbols("t")
        v1, v2, d0 = 5, 2, 10
        meet = float(sp.solve(sp.Eq(v1 * t, d0 + v2 * t), t)[0])
        line = NumberLine(x_range=[0, 20, 2], length=11).shift(DOWN * 1.2)
        a = Dot(line.n2p(0) + UP * 0.32, color=BLUE, radius=0.18)
        b = Dot(line.n2p(d0) + DOWN * 0.32, color=RED, radius=0.18)
        la = Text("A", font="Consolas", font_size=22, color=BLUE).next_to(a, UP)
        lb = Text("B", font="Consolas", font_size=22, color=RED).next_to(b, DOWN)
        la.add_updater(lambda m: m.next_to(a, UP))
        lb.add_updater(lambda m: m.next_to(b, UP))
        lab = Text("t = %.2f" % meet, font="Consolas", font_size=24,
                   color=YELLOW).to_corner(UR)
        self.add(line, a, b, la, lb, lab)
        self.play(a.animate.move_to(line.n2p(v1 * meet) + UP * 0.32),
                  b.animate.move_to(line.n2p(d0 + v2 * meet) + DOWN * 0.32), run_time=3.6)
        self.play(Flash(line.n2p(v1 * meet), color=YELLOW), run_time=1.2)
        self.play(FadeIn(foot("位置相等的方程交给 SymPy，动画只负责画")), run_time=0.4)
        self.wait(1.2)
