我已经说明了由两个向量跨越的平行四边形,并希望在该平行四边形的区域中进行着色,我尝试这样做:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )然而,这个平行四边形的颜色覆盖了我已经绘制的箭头,如下所示:

我想让它出现在背景中。有没有一种方法可以在场景中引入一个新的对象,使其在逻辑上位于任何现有对象的后面,就像我先绘制了它一样,这样它看起来就像:

发布于 2021-10-21 20:27:24
可以使用set_z_index方法将平行四边形的z_index属性设置为小于箭头的值。
在这里,我将它设置为比v1更低的值
poly.set_z_index(v1.z_index - 1)或者,您可以直接操作z_index属性:
poly.z_index = v1.z_index - 1使用set_z_index方法将是更干净的解决方案。
完整代码:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
# Set the z-index
poly.set_z_index(v1.z_index - 1)
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )https://stackoverflow.com/questions/69597904
复制相似问题