from manim import *
class Line3DDirections(ThreeDScene):
    """3.2 parallel_to / perpendicular_to 是类方法，返回一条「新的」Line3D，
    必须接住返回值并自己上屏；老示例调用了却把结果丢掉，画面里只剩一条蓝线。
    point 指定派生线经过的位置，用它把三条线错开。
    """
    def construct(self):
        self.set_camera_orientation(phi=65 * DEGREES, theta=-50 * DEGREES)
        title = Text("Line3D 与平行线 / 垂线", font="STLiti", font_size=32).to_edge(UP)
        self.add_fixed_in_frame_mobjects(title)
        line = Line3D(start=[-1, -2, -1], end=[1, 2, 2],
                      thickness=0.04, color=BLUE)
        parallel = Line3D.parallel_to(line, point=UP * 2, length=4,
                                      color=YELLOW, thickness=0.04)
        perpendicular = Line3D.perpendicular_to(line, point=RIGHT * 2, length=4,
                                                color=RED, thickness=0.04)
        legend = VGroup(
            Text("蓝：原始线段", font="STLiti", font_size=21, color=BLUE),
            Text("黄：parallel_to 平行线", font="STLiti", font_size=21, color=YELLOW),
            Text("红：perpendicular_to 垂线", font="STLiti", font_size=21, color=RED),
        ).arrange(DOWN, aligned_edge=LEFT, buff=0.16).to_corner(UL).shift(DOWN * 1.2)
        self.add_fixed_in_frame_mobjects(legend)
        self.play(FadeIn(title), run_time=0.6)
        self.play(LaggedStart(*[FadeIn(x) for x in (line, parallel, perpendicular)],
                              lag_ratio=0.35, run_time=2.0))
        self.play(FadeIn(legend), run_time=0.8)
        note = Text("两个方法都返回新 Line3D，必须用 self.add / self.play 上屏",
                    font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN)
        self.add_fixed_in_frame_mobjects(note)
        self.play(FadeIn(note), run_time=0.6)
        self.wait(2.6)
