from manim import *
import numpy as np
from numpy import array
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
    return Dot(pos, **kwargs)
import math
class PainfulDemo(Scene):
    def construct(self):
        a, c = 1, 2
        b_tracker = ValueTracker(-3)
        axes = Axes(x_range=[-5,5], y_range=[-1,6])
        # 抛物线
        graph = always_redraw(lambda: axes.plot(
            lambda x: a*x**2 + b_tracker.get_value()*x + c
        ))
        # 计算交点 —— 这里就是噩梦开始的地方
        def get_roots():
            b = b_tracker.get_value()
            disc = b**2 - 4*a*c
            if disc >= 0:
                root1 = (-b + math.sqrt(disc)) / (2*a)   # 负数平方根直接报错
                root2 = (-b - math.sqrt(disc)) / (2*a)
                return [root1, root2]
            else:
                return []  # 如果忘了判断，上面一行就炸了
        dots = always_redraw(lambda: VGroup(*[
            Dot(axes.coords_to_point(r, 0)) for r in get_roots()
        ]))
        self.play(*[FadeIn(_o) for _o in [axes, graph, dots]], run_time=1.0)
        self.play(b_tracker.animate.set_value(3), run_time=5)
        self.wait(1)