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 CurveIntersection(Scene):
    def construct(self):
        self.add(head("曲线交点：SymPy 数值求解", "sp.nsolve / Axes.plot"))
        x = sp.symbols("x")
        root = float(sp.nsolve(sp.sin(x) - x / 2, 1.8))
        axes = Axes(x_range=[-4, 4, 1], y_range=[-2, 2, 1], x_length=9, y_length=4.8,
                    axis_config={"color": GREY_B}).shift(DOWN * 0.3)
        g1 = axes.plot(lambda t: np.sin(t), x_range=[-4, 4], color=BLUE).set_stroke(width=5)
        g2 = axes.plot(lambda t: t / 2, x_range=[-4, 4], color=YELLOW).set_stroke(width=5)
        dot = Dot(axes.c2p(root, np.sin(root)), color=RED, radius=0.16)
        lab = Text("x = %.4f" % root, font="Consolas", font_size=22,
                   color=GREY_A).to_corner(UR)
        self.add(axes, g1, g2, lab)
        self.play(Create(g1), Create(g2), run_time=2.4)
        self.play(FadeIn(dot, scale=2), Flash(dot, color=RED), run_time=1.4)
        self.play(FadeIn(foot("nsolve 给出数值解，无需手算或目测")), run_time=0.4)
        self.wait(1.4)
