from manim import *
from sympy import Symbol, solve
FONT = "Consolas"
class LineCrossDemo(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: solve the intersection of two lines", font=FONT, font_size=24, color=YELLOW).to_corner(UL, buff=0.35)
        k1, b1, k2, b2 = -2 / 3, -4, -2, 2
        xs, ys = Symbol("x"), Symbol("y")
        ret = solve((k1 * xs + b1 - ys, k2 * xs + b2 - ys), dict=True)
        ox, oy = float(ret[0][xs]), float(ret[0][ys])
        l1 = ax.plot(lambda t: k1 * t + b1, x_range=[-10, 10], color=RED)
        l2 = ax.plot(lambda t: k2 * t + b2, x_range=[-4.5, 5.5], color=BLUE)
        pts = [("A", (-6, 0), RED), ("B", (0, -4), RED), ("C", (0, 2), BLUE),
               ("D", (1, 0), BLUE), ("O", (ox, oy), YELLOW)]
        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)
        self.play(Create(ax), run_time=0.8)
        self.play(Create(l1), run_time=1.0)
        self.play(Create(l2), run_time=1.0)
        self.play(FadeIn(dots), FadeIn(labs), run_time=1.0)
        self.wait(1.8)
