如何使用Manim在数字行上动画点动画?

发布于 2025-01-23 21:10:22 字数 765 浏览 0 评论 0原文

我想使用Manim在数字线上对参数的位置进行动画动画。我想出了如何使用parametricfunction将参数的终端位置作为线的动画:

from manim import *
import numpy as np
import math

class line( Scene ):
    def construct( self ):

        tline = NumberLine(
            x_range=[ 0, 1 ],
            length=4,
            color=BLUE,
            include_numbers=False )

        t1 = ParametricFunction( lambda t: tline.number_to_point(np.sin(t * PI)),
                                 t_range=[0, 1],
                                 scaling=tline.scaling, color=YELLOW )

        self.play( DrawBorderThenFill( VGroup( tline ) ), run_time = 2 )
        self.play( AnimationGroup(Create(t1)), run_time = 6 )

这对单调增加值可以奏效,但是如果终点重新倍增本身,则不太好,因为动画在该阶段变得不可见。

有没有办法更改线图来动画动画动画,而不是跟踪线路?

I'd like to use Manim to animate the position of a parameter on a number line. I figured out how to animate the end position of the parameter as a line using ParametricFunction:

from manim import *
import numpy as np
import math

class line( Scene ):
    def construct( self ):

        tline = NumberLine(
            x_range=[ 0, 1 ],
            length=4,
            color=BLUE,
            include_numbers=False )

        t1 = ParametricFunction( lambda t: tline.number_to_point(np.sin(t * PI)),
                                 t_range=[0, 1],
                                 scaling=tline.scaling, color=YELLOW )

        self.play( DrawBorderThenFill( VGroup( tline ) ), run_time = 2 )
        self.play( AnimationGroup(Create(t1)), run_time = 6 )

This works okay for monotonically increasing values, but is no good if the end point doubles back on itself, as the animation becomes invisible at that stage.

Is there a way to change the line plot to animate a moving point instead of tracing out a line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-01-30 21:10:22

如果您的参数只是在某些固定值之间移动,则可以按照文档中的示例

如果您想对标记在数字行中移动的确切方式进行更多控制,我建议您复制与valueTrackerupdatefromalphafunc动画相似的内容,例如:

from manim import *

class Example(Scene):
    def construct(self):
        tline = NumberLine(
            x_range=[ 0, 1 ],
            length=4,
            color=BLUE,
            include_numbers=False )

        t_parameter = ValueTracker(0)
        t_marker = Dot(color=YELLOW).add_updater(
            lambda mob: mob.move_to(tline.number_to_point(t_parameter.get_value())),
        ).update()
        self.play( DrawBorderThenFill( VGroup( tline ) ), run_time = 2 )
        self.add(t_marker)
        self.play(
            UpdateFromAlphaFunc(
                t_parameter, 
                lambda mob, alpha: mob.set_value(np.sin(alpha * PI)),
                run_time=6
            )
        )

If your parameter simply moves between certain fixed values, you could follow a simple approach as in this example in the documentation.

If you want more control over the exact way the marker moves across the number line, I'd recommend replicating something similar with a ValueTracker and the UpdateFromAlphaFunc animation, like this:

from manim import *

class Example(Scene):
    def construct(self):
        tline = NumberLine(
            x_range=[ 0, 1 ],
            length=4,
            color=BLUE,
            include_numbers=False )

        t_parameter = ValueTracker(0)
        t_marker = Dot(color=YELLOW).add_updater(
            lambda mob: mob.move_to(tline.number_to_point(t_parameter.get_value())),
        ).update()
        self.play( DrawBorderThenFill( VGroup( tline ) ), run_time = 2 )
        self.add(t_marker)
        self.play(
            UpdateFromAlphaFunc(
                t_parameter, 
                lambda mob, alpha: mob.set_value(np.sin(alpha * PI)),
                run_time=6
            )
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文