【问题标题】:How to move Arrow in manim along a path, adjusting end points and orientation?如何沿路径移动箭头,调整端点和方向?
【发布时间】:2022-07-09 13:07:41
【问题描述】:

我希望能够使用 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值是更新程序上次运行的时间。

有什么方法可以让我在更新程序中重置箭头的开始、结束值,就好像我正在构造一个新的 Arrow() 来替换动画中每个点的原始值?

【问题讨论】:

    标签: animation coordinates manim


    【解决方案1】:

    由于 Arrow 继承自 Line,所有 Line 方法都可用,包括 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 )
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2015-03-22
      • 1970-01-01
      • 2012-08-04
      • 2011-01-06
      • 1970-01-01
      • 2017-10-31
      • 2013-09-04
      • 2017-09-29
      相关资源
      最近更新 更多