from manim import *
class CutoutMidline(Scene):
    """2.4 梯形中位线：原文用了一堆未定义的点，这里全部改成显式坐标。
    上底 AB、下底 DF，E / G 分别是两腰中点，红色线段 EG 就是中位线。
    用 Cutout 从梯形里挖去中位线与下底围成的三角形 EGM，演示「切掉不要的部分」。
    """
    def construct(self):
        kw = dict(font="STLiti")
        title = Text("2.4 梯形的中位线与 Cutout", 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)
        A, B = 1.3 * LEFT + 1.9 * UP, 1.3 * RIGHT + 1.9 * UP
        D, F = 2.8 * LEFT + 1.6 * DOWN, 2.8 * RIGHT + 1.6 * DOWN
        E, G, M = (A + D) / 2, (B + F) / 2, (D + F) / 2
        main = Polygon(A, B, F, D)
        sub = Polygon(E, G, M)
        cut = Cutout(main, sub, fill_opacity=1, color=BLUE,
                     stroke_color=GREEN, stroke_width=4)
        midline = Line(E, G, color=RED, stroke_width=5)
        marks = VGroup(*[Dot(p, radius=0.06, color=YELLOW) for p in (A, B, D, F, E, G)])
        labels = VGroup()
        for name, p, d in [("A", A, UL), ("B", B, UR), ("D", D, DL),
                           ("F", F, DR), ("E", E, LEFT), ("G", G, RIGHT)]:
            labels.add(Text(name, font_size=24, color=YELLOW, **kw).next_to(p, d, buff=0.14))
        note = Text("E、G 为两腰中点，红线 EG 即中位线；蓝色区域已挖去三角形 EGM",
                    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(cut), run_time=1.5)
        self.play(FadeIn(midline), run_time=0.7)
        self.play(FadeIn(marks), FadeIn(labels), run_time=1.0)
        self.play(FadeIn(note), run_time=0.6)
        self.wait(2.4)
