简体   繁体   English

Manim:这是移动和缩放一堆 mobjects 的好方法吗?

[英]Manim: Is the a nice way to move and scale a bunch of mobjects?

I'm using the manim module in Python to display some decision trees.我正在使用 Python 中的 manim 模块来显示一些决策树。 Fist I want to show Tree_1 as it is in the code below.首先,我想在下面的代码中显示 Tree_1。 Then I want it to scale it down and shift it to the left.然后我想让它缩小并向左移动。 Next I want Tree_2 to appear where Tree_1 is and them move to the upper right quadrant of the screen.接下来,我希望 Tree_2 出现在 Tree_1 所在的位置,并且它们移动到屏幕的右上象限。 Also the PURE_RED lines should move from tilted (as in Tree_1) to straight (as in Tree_2 in the code below).此外, PURE_RED线应从倾斜(如在 Tree_1 中)移动到笔直(如在下面代码中的 Tree_2 中)。 The same should then happen with Tree_3 just in the bottom right quadrant.同样的情况也应该发生在右下象限的 Tree_3 上。

Now I could do it by figuring out all the points and then hardcode it.现在我可以通过弄清楚所有的点然后硬编码来做到这一点。 But I wanted to ask if there is a nicer way.但我想问问有没有更好的方法。 Maybe one where I could define points in a local coordinate system and then I can just scale and move the whole tree.也许我可以在局部坐标系中定义点,然后我可以缩放和移动整棵树。

Also I'm sorry if its is considered common knowledge, but I'm super new to manim.如果它被认为是常识,我也很抱歉,但我对 manim 是超级新手。

from manim import *

class Tree_1(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_1l = Line([-6, 0, 0], [-7,-3, 0]).set_color(PURE_GREEN)
        line_1r = Line([-6, 0, 0], [-5,-3, 0]).set_color(PURE_RED)
        line_2l = Line([ 0, 0, 0], [-1,-3, 0]).set_color(PURE_GREEN)
        line_2r = Line([ 0, 0, 0], [ 1,-3, 0]).set_color(PURE_RED)
        line_3l = Line([ 6, 0, 0], [ 5,-3, 0]).set_color(PURE_GREEN)
        line_3r = Line([ 6, 0, 0], [ 7,-3, 0]).set_color(PURE_RED)

        self.play(
            Create(line_1l),
            Create(line_1r),
            Create(line_2l),
            Create(line_2r),
            Create(line_3l),
            Create(line_3r),
            )

class Tree_2(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_4 = Line([-6, 0, 0], [-6,-3, 0]).set_color(PURE_RED)
        line_5 = Line([ 0, 0, 0], [-0,-3, 0]).set_color(PURE_RED)
        line_6 = Line([ 6, 0, 0], [ 6,-3, 0]).set_color(PURE_RED)

        self.play(
            Create(line_4),
            Create(line_5),
            Create(line_6),
            )

class Tree_3(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_4 = Line([-6, 0, 0], [-6,-3, 0]).set_color(PURE_GREEN)
        line_5 = Line([ 0, 0, 0], [-0,-3, 0]).set_color(PURE_GREEN)
        line_6 = Line([ 6, 0, 0], [ 6,-3, 0]).set_color(PURE_GREEN)

        self.play(
            Create(line_4),
            Create(line_5),
            Create(line_6),
            )

Yes there is, add them to a VGroup , here is the docs .是的,将它们添加到VGroup中,这是文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM