近我者赤

Manim 学习笔记(三)--坐标系与坐标平面

坐标系与坐标平面--效果:

代码:

# -*- coding: utf-8 -*-
from manim import *

class ZBX_ZBPM(Scene):
   
    def construct(self):
        #坐标平面(网格)
        my_plane = NumberPlane(
            faded_line_ratio=2,
            x_range=[-8, 8, 1],#[前两个参数的平均值为原点,其和表示坐标宽度,第三个参数表示坐标步长]
            y_range=[-4, 4, 1],
            x_length=16,#长度和上述范围一致才能是单位长度坐标
            y_length=8,            
            background_line_style={
                "stroke_color": TEAL,
                "stroke_width": 2,
                "stroke_opacity": 0.4,
            }
        )
        #坐标轴
        my_ax=Axes(#系统性默认全屏宽=14,高=8
            x_range=[-7, 7, 1],#[前两个参数的平均值为原点,其和表示坐标宽度,第三个参数表示坐标步长]
            y_range=[-4, 4, 1],
            x_length=14,#长度和上述范围一致才能是单位长度坐标
            y_length=8,
            #tips=True,
            
            axis_config={
                "include_numbers": True,
                "tip_shape": StealthTip,#箭头形状
                "color": TEAL,
                "numbers_to_include": np.arange(-4, 6, 2),
                #"font_size": 24,
            },
        )
        d1=my_ax.c2p(2,2)
        self.add(my_plane,my_ax)
        self.wait()
        self.add(Dot(d1))
        self.wait()
        lines_1 = my_ax.get_lines_to_point(d1)
        self.play(Create(lines_1))
        self.wait(2)
        
        dot0 = Dot()
        dot1 = Dot().move_to(LEFT*2)
        text1 = Tex("$\\vec{a}$")
        l1=Line(dot0,dot1)
        R1=VGroup(dot0,dot1,l1,text1) #把点和文字组合在一起
        text1.next_to(dot1,LEFT) # 文字放在点的左侧

        self.add(R1)
        self.play(Rotating(R1,radians=2*PI,run_time=5,axis=OUT,about_point=0*RIGHT))
        self.wait(2)


        dot2 = Dot().move_to(RIGHT*2)
        text2 = Text("A")
        text2.next_to(dot2,LEFT)
        self.add(dot2,text2)   

        # Update function 保持文字在点左侧
        def update_text(obj):
            obj.next_to(dot2,LEFT,buff=SMALL_BUFF) 

        text2.add_updater(update_text)     # 把这个函数添加给text   
        self.play(Rotating(dot2,radians=2*PI,run_time=5,axis=OUT,about_point=0*RIGHT))

        self.wait()

        dot3 = Dot()
        text3 = Text("B")
        text3.next_to(dot3,RIGHT)
        l3=Line(dot2,dot3)
        self.add(text3)
        R2=VGroup(dot2,dot3,l3)
        self.add(R2)
        # Update function 更新函数
        def update_text(obj):
            obj.next_to(dot3,RIGHT,buff=SMALL_BUFF)
        self.play(Rotating(R2,radians=2*PI,run_time=5,axis=OUT,about_point=2*RIGHT),UpdateFromFunc(text3,update_text))
        self.wait()
        self.clear()


        def x2(x):
            return x**2
        
        def sinx(x):
            return np.sin(x)

        def fx(x):
            for x in [-2,0]:
                return x**2
            
            for x in [0,6.28]:
                return np.sin(x)
            
        c1=Circle(color=BLUE,radius=3)

        self.add(my_plane,my_ax)

        f1= my_ax.plot(x2,color=YELLOW,x_range=[-2, 0])
        f2= my_ax.plot(sinx, x_range=[0, 6.28], color=BLUE)
        f3= my_ax.plot(fx, x_range=[-2, 0], color=RED)
        dot4=Dot(color=RED).scale(2)
        dot5=Dot(my_ax.c2p(3,0))
        l2=Line(dot0,dot5)
        vg1=VGroup(f1,f2)
        self.add(vg1,l2)        
        def update_dot(obj):
            
            obj.move_to(my_ax.c2p(x,np.sin(x))) 

        dot5.add_updater(update_dot) 
    

        self.play(FadeIn(c1),run_time=4)
        self.play(MoveAlongPath(dot4,f1),run_time=5,rate_fun=linear)
        self.play(MoveAlongPath(dot4,f2),run_time=5,rate_fun=linear)
        self.play(MoveAlongPath(dot4,c1),run_time=5,rate_fun=linear)
        self.wait()
        self.clear()

        self.add(f3)
        self.wait()
posted @ 2024-07-26 09:24  近我者赤  阅读(207)  评论(0)    收藏  举报