如何将箭头沿着路径移动,调整终点和方向?

发布于 2025-01-24 12:23:17 字数 1630 浏览 4 评论 0原文

我希望能够使用Manim沿圆形弧移动一对矢量(径向和切线)。这需要移动并旋转操作。我到了:

from manim import *
import numpy as np
import math

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

        axes = Axes( x_range = [0, 1, 1], y_range = [0, 1, 1], x_length = 4, y_length = 4,
                     axis_config = {"include_tip": True, "numbers_to_exclude": [0]} ).add_coordinates()

        pc = lambda t: axes.coords_to_point( np.cos( t * 0.5 * PI ), np.sin( t * 0.5 * PI ) )
        xone = pc(0)
        t_parameter = ValueTracker(0)
        e1 = Arrow( start = xone, end = xone + RIGHT, color = GREEN, buff = 0 ).add_updater(
            lambda mob: mob.move_to( pc( t_parameter.get_value() ) ) #.rotate( t_parameter.get_value() * PI / 2 )
        ).update()
        e2 = Arrow( start = xone, end = xone + UP, color = GREEN, buff = 0 ).add_updater(
            lambda mob: mob.move_to( pc( t_parameter.get_value() ) )
        ).update()
        g1 = ParametricFunction( pc,
                                 t_range=[0, 1],
                                 scaling=axes.x_axis.scaling, color=YELLOW )

        self.add( VGroup( axes, g1, e1, e2 ) )
        self.play( UpdateFromAlphaFunc( t_parameter, 
                                        lambda mob, alpha: mob.set_value( alpha ) ),
                                        run_time=6 )
        self.wait( )

有几个问题:

  • 我在Updater()中编码的MOVE_TO移动了箭头的中心,但是我希望此移动适用于终点。
  • 我想将旋转旋转到那个箭头(将箭头旋转末端,而不是中心)。

如果我在我的updater()中包括(评论了)rotate(),它越来越快地旋转箭头,因为旋转不是原始位置的绝对,我不知道先前的alpha值是什么更新者最后运行。

有什么办法可以将箭头启动重置,在更新程序中到位的结束值,就好像我正在构建一个新的箭头()以替换动画中每个点的原始值吗?

I'd like to be able to move a pair of vectors (radial and tangent) along a circular arc using Manim. This requires a move and rotate operation. I got as far as:

from manim import *
import numpy as np
import math

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

        axes = Axes( x_range = [0, 1, 1], y_range = [0, 1, 1], x_length = 4, y_length = 4,
                     axis_config = {"include_tip": True, "numbers_to_exclude": [0]} ).add_coordinates()

        pc = lambda t: axes.coords_to_point( np.cos( t * 0.5 * PI ), np.sin( t * 0.5 * PI ) )
        xone = pc(0)
        t_parameter = ValueTracker(0)
        e1 = Arrow( start = xone, end = xone + RIGHT, color = GREEN, buff = 0 ).add_updater(
            lambda mob: mob.move_to( pc( t_parameter.get_value() ) ) #.rotate( t_parameter.get_value() * PI / 2 )
        ).update()
        e2 = Arrow( start = xone, end = xone + UP, color = GREEN, buff = 0 ).add_updater(
            lambda mob: mob.move_to( pc( t_parameter.get_value() ) )
        ).update()
        g1 = ParametricFunction( pc,
                                 t_range=[0, 1],
                                 scaling=axes.x_axis.scaling, color=YELLOW )

        self.add( VGroup( axes, g1, e1, e2 ) )
        self.play( UpdateFromAlphaFunc( t_parameter, 
                                        lambda mob, alpha: mob.set_value( alpha ) ),
                                        run_time=6 )
        self.wait( )

There are a couple problems:

  • The move_to that I've coded in the updater() moves the center of the arrow, but I'd like that move to apply to the end point.
  • I'd like to apply a rotation to that arrow (rotating the arrow about it's end point, not it's center).

If I include (the commented out) rotate() in my updater(), it spins the arrow faster and faster, since the rotation isn't absolute from the original position, and I don't know what the previous alpha value was when the updater last ran.

Is there any way that I can just reset the arrow start,end values in place in my updater, as if I was constructing a new Arrow() to replace the original at each point in the animation?

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

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

发布评论

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

评论(1

云醉月微眠 2025-01-31 12:23:17

由于箭头从行中继承,因此所有行方法都可用,包括put_start_and_end_on()

from manim import *
import numpy as np
import math

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

        radius = 4
        axes = Axes( x_range = [0, 1, 1], y_range = [0, 1, 1], x_length = radius, y_length = radius,
                        axis_config = {"include_tip": True, "numbers_to_exclude": [0]} ).add_coordinates()

        origin = axes.coords_to_point( 0, 0 )
        e1dir = RIGHT
        e2dir = UP

        x1 = origin + radius * e1dir
        x2 = origin + radius * e2dir

        r = lambda t: origin + radius * e1dir * np.cos( t * 0.5 * PI ) + radius * e2dir * np.sin( t * 0.5 * PI )
        tv = lambda t: - e1dir * np.sin( t * 0.5 * PI ) + e2dir * np.cos( t * 0.5 * PI )

        def rmove(a, t):
            p = r(t)
            d = (p - origin)/4
            a.put_start_and_end_on( p, p + d )

        def tmove(a, t):
            p = r(t)
            d = tv(t)
            a.put_start_and_end_on( p, p + d )

        def u1(mob):
            t = t_parameter.get_value()
            rmove( mob, t )

        def u2(mob):
            t = t_parameter.get_value()
            tmove( mob, t )

        t_parameter = ValueTracker(0)
        e1 = Arrow( start = x1, end = x1 + e1dir, color = GREEN, buff = 0 ).add_updater(u1).update()
        e2 = Arrow( start = x1, end = x1 + e2dir, color = GREEN, buff = 0 ).add_updater(u2).update()

        g1 = ParametricFunction( r,
                                    t_range=[0, 1],
                                    scaling=axes.x_axis.scaling, color=YELLOW )

        self.add( VGroup( axes, g1, e1, e2 ) )
        self.wait( 1 )

        self.play( UpdateFromAlphaFunc( t_parameter, 
                                        lambda mob, alpha: mob.set_value( alpha ) ),
                                        run_time=6 )
        self.wait( 1 )

Because Arrow inherits from Line, all the Line methods are available, including put_start_and_end_on()

from manim import *
import numpy as np
import math

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

        radius = 4
        axes = Axes( x_range = [0, 1, 1], y_range = [0, 1, 1], x_length = radius, y_length = radius,
                        axis_config = {"include_tip": True, "numbers_to_exclude": [0]} ).add_coordinates()

        origin = axes.coords_to_point( 0, 0 )
        e1dir = RIGHT
        e2dir = UP

        x1 = origin + radius * e1dir
        x2 = origin + radius * e2dir

        r = lambda t: origin + radius * e1dir * np.cos( t * 0.5 * PI ) + radius * e2dir * np.sin( t * 0.5 * PI )
        tv = lambda t: - e1dir * np.sin( t * 0.5 * PI ) + e2dir * np.cos( t * 0.5 * PI )

        def rmove(a, t):
            p = r(t)
            d = (p - origin)/4
            a.put_start_and_end_on( p, p + d )

        def tmove(a, t):
            p = r(t)
            d = tv(t)
            a.put_start_and_end_on( p, p + d )

        def u1(mob):
            t = t_parameter.get_value()
            rmove( mob, t )

        def u2(mob):
            t = t_parameter.get_value()
            tmove( mob, t )

        t_parameter = ValueTracker(0)
        e1 = Arrow( start = x1, end = x1 + e1dir, color = GREEN, buff = 0 ).add_updater(u1).update()
        e2 = Arrow( start = x1, end = x1 + e2dir, color = GREEN, buff = 0 ).add_updater(u2).update()

        g1 = ParametricFunction( r,
                                    t_range=[0, 1],
                                    scaling=axes.x_axis.scaling, color=YELLOW )

        self.add( VGroup( axes, g1, e1, e2 ) )
        self.wait( 1 )

        self.play( UpdateFromAlphaFunc( t_parameter, 
                                        lambda mob, alpha: mob.set_value( alpha ) ),
                                        run_time=6 )
        self.wait( 1 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文