from manim import *
import numpy as np
class CutoutTangent(Scene):
    """2.3 同心圆与切线切割：用 np.cos / np.sin 显式算出圆上的点。
    原文的 A/P/B/O 全是未定义变量；这里 O 是圆心，A、B 取小圆上的点，P 取大圆上的点。
    main = 四边形 APBO，sub = 三角形 APO，Cutout 后剩下的蓝色区域即三角形 PBO。
    """
    def construct(self):
        kw = dict(font="STLiti")
        title = Text("2.3 圆的切线：沿切线切割", font_size=36, color=RED, **kw).to_edge(UP, buff=0.3)
        rule = Line(LEFT * 6.6, RIGHT * 6.6, color=BLUE_B, stroke_width=3).next_to(title, DOWN, buff=0.18)
        def on(r, deg):
            return r * np.array([np.cos(deg * DEGREES), np.sin(deg * DEGREES), 0])
        O = ORIGIN
        big = Circle(radius=2.0, color=YELLOW, stroke_width=3)
        small = Circle(radius=1.15, color=BLUE_B, stroke_width=3)
        A, P, B = on(1.15, 140), on(2.0, 45), on(1.15, -20)
        main = Polygon(A, P, B, O)
        sub = Polygon(A, P, O)
        cut = Cutout(main, sub, fill_opacity=1, color=BLUE,
                     stroke_color=GREEN, stroke_width=3)
        marks = VGroup(*[Dot(p, radius=0.06, color=YELLOW) for p in (O, A, P, B)])
        labels = VGroup()
        for name, p, d in [("O", O, DL), ("A", A, UL), ("P", P, UR), ("B", B, DR)]:
            labels.add(Text(name, font_size=24, color=YELLOW, **kw).next_to(p, d, buff=0.14))
        note = Text("黄圈为大圆、蓝圈为小圆；Cutout 挖去 APO，留下蓝色 PBO",
                    font_size=24, color=GREY_B, **kw).to_edge(DOWN, buff=0.3)
        self.play(FadeIn(title), FadeIn(rule), run_time=0.7)
        self.play(FadeIn(big), FadeIn(small), run_time=1.2)
        self.play(FadeIn(cut), run_time=1.2)
        self.play(FadeIn(marks), FadeIn(labels), run_time=1.0)
        self.play(FadeIn(note), run_time=0.6)
        self.wait(2.4)
