from manim import *
FONT = "Consolas"
class LineTrackDemo(Scene):
    def construct(self):
        ax = Axes(x_range=[-10, 10, 2], y_range=[-9, 9, 2], x_length=13, y_length=7.2,
                  axis_config={"color": GREY_B, "stroke_width": 1.5}, tips=False)
        ax.add_coordinates()
        head = Text("SymPy: track the intersection in real time", font=FONT, font_size=24, color=YELLOW).to_corner(UL, buff=0.35)
        k1, k2, b2 = -2 / 3, -2, 2
        tb = ValueTracker(-4)
        def red_line():
            return ax.plot(lambda t: k1 * t + tb.get_value(), x_range=[-10, 10], color=RED)
        def cross():
            x = (b2 - tb.get_value()) / (k1 - k2)
            return ax.c2p(x, k1 * x + tb.get_value())
        l1 = red_line()
        l2 = ax.plot(lambda t: k2 * t + b2, x_range=[-4.5, 5.5], color=BLUE)
        o = Dot(cross(), color=YELLOW, radius=0.11)
        ol = Text("O", font=FONT, font_size=24, color=YELLOW)
        l1.add_updater(lambda m: m.become(red_line()))
        o.add_updater(lambda m: m.move_to(cross()))
        ol.add_updater(lambda m: m.next_to(o, UR, buff=0.08))
        pts = [("A", (-6, 0), RED), ("B", (0, -4), RED), ("C", (0, 2), BLUE), ("D", (1, 0), BLUE)]
        dots = VGroup(*[Dot(ax.c2p(*p), color=c, radius=0.09) for _, p, c in pts])
        labs = VGroup(*[Text(n, font=FONT, font_size=24, color=c).next_to(ax.c2p(*p), UR, buff=0.08)
                        for n, p, c in pts])
        self.add(head, ax, l1, l2, dots, labs, o, ol)
        self.play(tb.animate.set_value(-1), run_time=2.0)
        self.play(tb.animate.set_value(-6), run_time=2.0)
        self.wait(1.6)
