from manim import *
import numpy as np
from numpy import array
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
    return Dot(pos, **kwargs)
import numpy as np
config.background_color = WHITE
class DynamicMullerLyer(Scene):
    def construct(self):
        self.vertexes = []
        count = 11
        # 所有线都一样长，蓝色和红色的线段也是一样长。
        lines = self.create_lines(count)
        self.play(Create(lines))
        self.wait(1)
        self.clear()
        wings = self.create_wings(self.vertexes)
        self.play(*[FadeIn(_o) for _o in [*wings]], run_time=1.0)
        self.rotate_wings(
            wings,
            self.vertexes,
            list(np.random.uniform(0.5, 1.5, len(wings))),
            repeat=4,
        )
        self.wait(1)
        # 放在一起
        self.play(*[FadeIn(_o) for _o in [lines]], run_time=1.0)
        self.rotate_wings(
            wings,
            self.vertexes,
            list(np.random.uniform(0.5, 1.5, len(wings))),
            repeat=8,
        )
        self.wait(0.5)
    def create_lines(self, count=11, interval=0.4) -> VGroup:
        l_group = VGroup()
        for i in range(count // 2 + 1):
            vertical_l_group = VGroup()
            vertical_l_group.add(
                Line(UP * 2.5, UP * 1.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.add(
                Line(UP * 1.5, UP * 0.5, stroke_width=2, color=PURE_RED)
            )
            vertical_l_group.add(
                Line(UP * 0.5, DOWN * 0.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.add(
                Line(DOWN * 0.5, DOWN * 1.5, stroke_width=2, color=PURE_RED)
            )
            vertical_l_group.add(
                Line(DOWN * 1.5, DOWN * 2.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.shift(LEFT * i * interval)
            self.vertexes.append(UP * 2.5 + LEFT * i * interval)
            self.vertexes.append(UP * 1.5 + LEFT * i * interval)
            self.vertexes.append(UP * 0.5 + LEFT * i * interval)
            self.vertexes.append(DOWN * 0.5 + LEFT * i * interval)
            self.vertexes.append(DOWN * 1.5 + LEFT * i * interval)
            self.vertexes.append(DOWN * 2.5 + LEFT * i * interval)
            l_group.add(vertical_l_group)
        for i in range(1, count // 2 + 1):
            vertical_l_group = VGroup()
            vertical_l_group.add(
                Line(UP * 2.5, UP * 1.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.add(
                Line(UP * 1.5, UP * 0.5, stroke_width=2, color=PURE_RED)
            )
            vertical_l_group.add(
                Line(UP * 0.5, DOWN * 0.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.add(
                Line(DOWN * 0.5, DOWN * 1.5, stroke_width=2, color=PURE_RED)
            )
            vertical_l_group.add(
                Line(DOWN * 1.5, DOWN * 2.5, stroke_width=2, color=PURE_BLUE)
            )
            vertical_l_group.shift(RIGHT * i * interval)
            self.vertexes.append(UP * 2.5 + RIGHT * i * interval)
            self.vertexes.append(UP * 1.5 + RIGHT * i * interval)
            self.vertexes.append(UP * 0.5 + RIGHT * i * interval)
            self.vertexes.append(DOWN * 0.5 + RIGHT * i * interval)
            self.vertexes.append(DOWN * 1.5 + RIGHT * i * interval)
            self.vertexes.append(DOWN * 2.5 + RIGHT * i * interval)
            l_group.add(vertical_l_group)
        return l_group
    def create_wings(self, vertexes, wing_radio=0.1):
        groups = []
        # 创建两条线，呈V字形
        for vertex in vertexes:
            left_line = Line(
                vertex, vertex + (UP + LEFT) * wing_radio, stroke_width=2, color=BLACK
            )
            right_line = Line(
                vertex, vertex + (UP + RIGHT) * wing_radio, stroke_width=2, color=BLACK
            )
            groups.append(VGroup(left_line, right_line))
        return groups
    def rotate_wings(self, wings, vertexes, run_times, repeat=4):
        anims = []
        for i in range(len(wings)):
            ag1 = AnimationGroup(
                Rotate(
                    wings[i][0], angle=90 * DEGREES, about_point=vertexes[i]
                ).set_run_time(run_times[i]),
                Rotate(
                    wings[i][1], angle=-90 * DEGREES, about_point=vertexes[i]
                ).set_run_time(run_times[i]),
            )
            ag2 = AnimationGroup(
                Rotate(
                    wings[i][0], angle=-90 * DEGREES, about_point=vertexes[i]
                ).set_run_time(run_times[i]),
                Rotate(
                    wings[i][1], angle=90 * DEGREES, about_point=vertexes[i]
                ).set_run_time(run_times[i]),
            )
            anim = Succession([ag1, ag2] * repeat)
            anims.append(anim)
        self.play(
            AnimationGroup(*anims),
            run_time=max(run_times) * repeat,
        )