from manim import *

FONT = "STLiti"


class Demo(Scene):
    def construct(self):
        head = Text("公式拆分与着色", font=FONT, font_size=36, color=YELLOW).to_edge(UP, buff=0.5)
        bar = Line(LEFT * 6.6, RIGHT * 6.6, color=BLUE).next_to(head, DOWN, buff=0.12)
        self.play(FadeIn(head), Create(bar), run_time=0.6)
        f1 = MathTex(r"z = \sqrt{x^2 + y^2}", color=RED)
        f2 = MathTex("z", "=", r"\sqrt{", "x", "^2 + ", "y", "^2}")
        f2.set_color_by_tex("x", BLUE)
        f2.set_color_by_tex("y", RED)
        f2.set_color_by_tex("z", GREEN)
        f3 = MathTex("z", "=", r"\sqrt{", "x", "^2 + ", "y", "^2}")
        f3.set_color_by_tex_to_color_map({"z": GREEN, "x": BLUE, "y": RED})
        rows = VGroup()
        for lab, f in zip(("拆分前：", "拆分后：", "拆分后："), (f1, f2, f3)):
            rows.add(VGroup(Text(lab, font=FONT, font_size=32), f).arrange(RIGHT, buff=0.9))
        rows.arrange(DOWN, buff=0.75).shift(DOWN * 0.4)
        for row in rows:
            self.play(FadeIn(row), run_time=0.7)
        self.wait(2.9)
