欢迎光临散文网 会员登陆 & 注册

〔manim教程〕第一讲 物体的位置与坐标变换 代码展示

2023-06-16 17:10 作者:NUMT片段  | 我要投稿


00:30


FRAME_HEIGHT=8
FRAME_WIDTH≈14
RIGHT=np.array([1, 0, 0])
UP=np.array([0, 1, 0])
LEFT=np.array([-1, 0, 0])
DOWN=np.array([0, -1, 0])
IN=np.array([0, 0, -1])
OUT=np.array([0, 0, 1])


02:06


使用shift方法可以根据传入的vector移动物体

self.add(mob)
mob.shift(LEFT*5)
mob.shift(UP*2+RIGHT*3)
mob.shift(DOWN*2, RIGHT*2) #先相加后平移

还可以使用move_to方法来使物体移动到目标位置

move_to(point_or_mobject,aligned_edge=ORIGIN,coor_mask=np.array([1, 1, 1]))
self.add(logo)
logo.move_to(LEFT*5+UP*2)
logo.move_to(LEFT*3,aligned_edge=LEFT)
logo.move_to(LEFT*3,aligned_edge=RIGHT)
logo.move_to(UP*2,color_mask=np.array([1, 0, 1]))


04:05


使用scale可以对物件进行缩放

self.play(FadeIn(mob))
mob.scale(2)
mob.scale(0.5)
mob.scale(1.5,about_edge=UP)
mob.scale(0.3,about_edge=RIGHT)
mob.scale(2,about_point=np.array([-2,-2,0]))
self.play(mob.scale,1.5)
self.play(mob.scale,{'scale_factor':0.6,'about_edge':LEFT})


05:07


旋转

self.add(mob)
mob.shift(LEFT*2)
mob.rotate(90*DEGREES,axis=OUT)
mob.rotate(180*DEGREES,axis=OUT)
mob.rotate(270*DEGREES,axis=OUT)
mob.rotate(360*DEGREES,axis=OUT)
mob.rotate(90*DEGREES,axis=IN)
mob.rotate(180*DEGREES,axis=IN)
mob.rotate(270*DEGREES,axis=IN)
mob.rotate(360*DEGREES,axis=IN)


05:59


mob.rotate(angle,axis=OUT,np.array([1,0,0]))
mob.rotate(angle,axis=IN,np.array([1,0,0]))
mob.rotate_about_origin(angle,axis=OUT)


07:09


self.add(mob)
mob.shift(LEFT*2)
mob.rotate(90*DEGREES,axis=OUT)
mob.rotate(90*DEGREES,axis=IN)
mob.rotate(90*DEGREES,axis=OUT,about_point=ORIGIN)
mob.rotate(90*DEGREES,axis=IN,about_point=ORIGIN)


07:20

翻转

self.add(mob)
mob.flip()
mob.flip(axis=RIGHT) #对称轴方向方向
mob.flip(axis=RIGHT,about_point=ORIGIN) #对称轴过原点
mob.flip(axis=UR,about_point=LEFT*2.5) 


09:06


self.play(FadeIn(mob))
mob.stretch(factor=2, dim=0)
mob.stretch(factor=2, dim=1)
mob.stretch(factor=3, dim=2)
mob.stretch(factor=2, dim=1,about_point=np.array([-3,-3,0]))
mob.stretch(factor=0.5, dim=1,about_point=LEFT_SIDE+TOP)
mob.stretch(factor=2, dim=1,about_edge=UP)
mob.stretch(factor=0.5, dim=1,about_edge=DOWN)
# 同样的,所有mob的变换均为瞬间完成
# 但为了演示变换过程,
# 视频中执行的是将变换放入
# self.play()后对应的动画过程
self.play(mur.stretch, {"factor": 2, "dim": 0})


10:21


mob.to_corner(UL,buff=0)
for i in range(1,7):
    mob[i].to_corner(UL,buff=i*0.5)

所有对mob的移动操作 需要self.add(mob)才可以呈现

buff默认0.5

mob.to_edge(UP,buff=0)


11:30

对齐

self.add(mob)
self.add(logo)
mob.align_to(logo)#无作用
mob.align_to(logo,RIGHT)#右侧对齐
mob.align_to(logo,UR)#右上角对齐


12:32


c=Circle(radius=0.5)
sq = Square(side_length=0.5)
c.next_to(sq)
c.next_to(sq,RIGHT)
c.next_to(sq,RIGHT,aligned_edge=UP)

对齐Vgroup

A=VGroup(...)
B=VGroup(...)
B.next_to(A[2],DOWN,submoject_to_slign=B[1],aligned_edge=LEFT)
#等价于
B.next_to(A,DOWN,index_of_submobject_to_align=B[1],aligned_edge=LEFT)


15:04


这是一个mur猫 下面我们将用set_height和set_width方法调教它


15:04


img.set_height(height = 5)
img.set_height(height = 3)
img.set_height(height = 1)
img.set_width(width = 5)
img.set_width(width = 3)
img.set_width(width = 1)

按名字来说set_height是设置宽,set_width是设置长,效果不是应当不一样吗???

set_width和set_height方法在没有使用stretch参数的情况下和使用scale方法效果一致

加入stretch参数后可以对应的拉伸该对象

img.set_height(
       height = 5,
       stretch = True
    )
img.set_width(
       width = 5,
       stretch = True
    )

〔manim教程〕第一讲 物体的位置与坐标变换 代码展示的评论 (共 条)

分享到微博请遵守国家法律