首页 > 解决方案 > 如何在manim中为扇区的角度和旋转同时变化设置动画

问题描述

是否可以同时在同一个扇区上设置旋转和大小变化的动画?

我知道它可以被欺骗,Transform把它变成一个不同的部门。但这没有我想要的动画。Transform收缩然后膨胀。我希望扇区在改变大小的同时旋转。

我可以得到我想要的两种效果,但不能同时得到。

我已尝试但未能找到manim. 我看过readthedocs但这不是一个显示每个类的所有方法和变量的 API。

class Test(Scene):
    def construct(self):
        rotateGroup=VGroup(*[SmallDot()],
                           Sector(angle=45*DEGREES,
                                  start_angle=25*DEGREES,
                                  stroke_width=0,
                                  fill_color=RED),
                           Sector(angle=135*DEGREES,
                                  start_angle=185*DEGREES,
                                  stroke_width=0,
                                  fill_color=GREEN))\
                                  .shift(RIGHT,DOWN)
        # Start
        self.add(rotateGroup[1])
        self.wait()
        self.play(FadeOut(rotateGroup[1]),
                  # Ideal finishing position
                  FadeIn(rotateGroup[2]))
        self.wait()
        self.remove(rotateGroup[2])
        copy1=rotateGroup[1].copy()
        copy2=rotateGroup[2].copy()
        copy3=rotateGroup[1].copy()
        self.add(copy1)
        self.wait()

        # This has the desired rotation but lacks the size change
        self.play(Rotating(copy1,
                           axis=OUT,
                           run_time=2,
                           about_point=rotateGroup[0].get_center(),
                           radians=170*DEGREES,
                           rate_func=smooth))
        self.wait()
        self.remove(copy1)

        # This has the size change but lacks the correct rotation
        self.add(copy3)
        self.play(Transform(copy3,copy2))

提前致谢。

标签: pythonanimationmanim

解决方案


class Test(Scene):
    def construct(self):
        rotateGroup=VGroup(
            Sector(angle=45*DEGREES,
                    start_angle=25*DEGREES,
                    stroke_width=0,
                    fill_color=RED
            ),
            Sector(angle=135*DEGREES,
                    start_angle=185*DEGREES,
                    stroke_width=0,
                    fill_color=GREEN
                    )
        )
        red_sector, green_sector = rotateGroup
        # -----------------------------------------------
        red_sector.save_state()
        def update_sector(mob,alpha):
            mob.restore()
            angle = interpolate(45*DEGREES,135*DEGREES,alpha)
            start_angle = interpolate(25*DEGREES,185*DEGREES,alpha)
            mob.become(
                Sector(
                    angle=angle,
                    start_angle=start_angle,
                    stroke_width=0,
                    fill_color=interpolate_color(RED,GREEN,alpha)
                )
            )
        self.play(
            UpdateFromAlphaFunc(red_sector,update_sector)
        )
        self.wait()

作为一个任务,尝试做一个自定义动画,教程在这里


推荐阅读