30秒带你搞懂小孔成像~(BV1bebSz5Eiq)

· · 科技·工程

原视频地址

源代码:

from manim import *
import math

import numpy as np
from scipy.optimize import fsolve

class Project(Scene):
    def construct(self):
        class Light: # 光线
            line: Line
            arrow: Arrow

            def __init__(_, start, end, stroke_width, color) -> None:
                _.line = Line(
                    start=xOy.c2p(start[0], start[1]),
                    end=xOy.c2p(end[0], end[1]),
                    stroke_width=stroke_width,
                    color=color
                )
                _.arrow = Arrow(
                    start=xOy.c2p(start[0], start[1]),
                    end=xOy.c2p((start[0] + end[0]) / 2, (start[1] + end[1]) / 2),
                    stroke_width=stroke_width,
                    color=color,
                    buff=0
                )

            def In(_):
                self.play(Create(_.line), GrowArrow(_.arrow))

            def fadeout(_):
                self.play(FadeOut(_.line), FadeOut(_.arrow))

        def In2(a: Light, b: Light):
            self.play(Create(a.line), GrowArrow(a.arrow), Create(b.line), GrowArrow(b.arrow))
        # def Out2(a: Light, b: Light, c: Light, d: Light):
        #     self.play(FadeOut(a.line), FadeOut(a.arrow), FadeOut(b.line), FadeOut(b.arrow), FadeOut(c.line), FadeOut(c.arrow), FadeOut(d.line), FadeOut(d.arrow))

        self.wait(1)

        xOy = NumberPlane(
            axis_config={
                "stroke_color": BLUE
            }
        )
        self.play(Write(xOy)) # 坐标纸
        self.wait(1)

        up = Line(
            start=xOy.c2p(0, 0.05),
            end=xOy.c2p(0, 4),
            stroke_width=10,
            color=WHITE
        )
        down = Line(
            start=xOy.c2p(0, -0.05),
            end=xOy.c2p(0, -4),
            stroke_width=10,
            color=WHITE
        )
        self.play(FadeIn(up), FadeIn(down)) # 小孔
        self.wait(1)

        obj = Arrow(
            start=xOy.c2p(-4, -2),
            end=xOy.c2p(-4, 2),
            stroke_width=5,
            color=RED,
            buff=0
        )
        self.play(FadeIn(obj)) # 物
        self.wait(1)

        light1 = Light(
            start=[-4, 2],
            end=[0, 0],
            stroke_width=3,
            color=GRAY
        )
        light2 = Light(
            start=[-4, -2],
            end=[0, 0],
            stroke_width=3,
            color=GRAY
        )
        In2(light1, light2)
        light3 = Light(
            start=[0, 0],
            end=[8, -4],
            stroke_width=3,
            color=GRAY
        )
        light4 = Light(
            start=[0, 0],
            end=[8, 4],
            stroke_width=3,
            color=GRAY
        )
        In2(light3, light4)

        # pic = Arrow(
        #     start=ORIGIN,
        #     end=ORIGIN,
        #     stroke_width=3,
        #     color=GRAY,
        #     buff=0
        # )

        pic = obj.copy()

        self.play(FadeIn(pic))

        # pics = []

        for i in range(1, 8):
            pic.generate_target()
            pic.target = Arrow(
                start=xOy.c2p(i, i / 2),
                end=xOy.c2p(i, -i / 2),
                stroke_width=3,
                color=RED,
                buff=0
            )
            self.play(MoveToTarget(pic))
            pic = pic.target
            self.wait(0.5)

        self.wait(2)
        self.play(*[FadeOut(_) for _ in self.mobjects])

        self.wait(1)
        text = Text("谢谢观看", color=WHITE)
        self.play(FadeIn(text))
        self.wait(2)
        self.play(FadeOut(text))
        self.wait(1)