移动正方形后的Manim奇异角点计算行为

2024-09-29 00:14:40 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我刚开始学习曼尼姆,但我遇到了一些奇怪的行为。如果我得到一个正方形的坐标并画出来,它们就会与正方形对齐。但是,如果我尝试移动/移动/对齐到/下一个到正方形,然后再次绘制角点,它们将不再与正方形对齐。它适用于所有方向,适用于所有比例,但奇怪的是偏移量似乎与正方形的偏移量成正比Picture of behavior!

以下是我的(提炼)代码:

def square_and_corners(square, color):
    # Calculate the corners of the square
    DL = square.get_left() + square.get_bottom()
    DR = square.get_right() + square.get_bottom()
    UL = square.get_left() + square.get_top()
    UR = square.get_right() + square.get_top()

    # Add corners and square
    self.add(Dot(DL, color=color), Dot(DR, color=color),
             Dot(UL, color=color), Dot(UR, color=color))
    self.add(square)

# No moving
square1 = Square(2, color=WHITE)
square_and_corners(square1, WHITE)

# Move to RIGHT
square2 = Square(2, color=RED)
square2.move_to(RIGHT)
square_and_corners(square2, RED)

# align to the left of the central square
square3 = Square(2, color=GREEN)
square3.next_to(square1, LEFT)
square_and_corners(square3, GREEN)

Tags: andthetogetleftdot偏移量color
1条回答
网友
1楼 · 发布于 2024-09-29 00:14:40

解决方案代码

class SquareScene(Scene):
    def construct(self):
        def square_and_corners(square, color):
            DL = square.get_left() + square.get_bottom()
            DR = square.get_right() + square.get_bottom()
            UL = square.get_left() + square.get_top()
            UR = square.get_right() + square.get_top()
            g = [DL,DR,UL,UR]
            G = VGroup()
            for s in g:
                dot = Dot(point = s,color = color)
                G.add(dot)
            G.add(square)
            return G

# No moving
        square1 = Square(side_length = 2)
        self.add(square_and_corners(square1, WHITE))

# Move to RIGHT
        square2 = Square(side_length = 2).set_stroke(color = RED)
        a = square_and_corners(square2, RED)
        self.add(a.move_to(RIGHT))

# align to the left of the central square
        square3 = Square(side_length = 2, color=GREEN)
        b = square_and_corners(square3, GREEN)
        self.add(b.next_to(square1, LEFT))

# You can move only the square if you want (or any mobject in the VGroup)
        square4 = Square(side_length = 2, color=BLUE)
        b = square_and_corners(square4, BLUE)
        self.add(b.next_to(square1, UP))
        self.add(square4.next_to(square1,DOWN))

细节

在manim中处理一组mobject的最佳方法是将它们分组到VGroup()中通过这种方式,可以分别为组中的所有移动对象或任何需要的移动对象设置动画,减少渲染时的错误数和动画中出现的问题,减少为制作动画而编写的线条,并且在迭代中处理起来非常容易

输出

enter image description here

相关问题 更多 >