manim 3D graphing
Scene1
创造三维曲线并移动摄像机
class myjob(ThreeDScene): # 继承3DScene def construct(self): axes = ThreeDAxes( x_range=[-6,6,1], # 错误点1,原本写成了x_range=[-6,-6,1] y_range=[-6,6,1], z_range=[-6,6,1], x_length=8, y_length=6, z_length=6 ) graph = axes.plot(lambda x : x**2, x_range=[-2,2,1], color=YELLOW) rects = axes.get_riemann_rectangles(graph=graph, x_range=[-2,2], dx=0.1, stroke_color=WHITE) # 错误点2,原本写成了get_riemann_rectangle(少了s) graph2 = axes.plot_parametric_curve(lambda t : np.array([np.cos(t), np.sin(t), t]), t_range=[-2*PI, 2*PI], color=RED) # 注意旧版本为get_parametric_graph,现已废弃 self.add(axes, graph) self.wait() # the camera is auto set to phi = 0 and theta = -90 self.move_camera(phi = 60*DEGREES) self.wait() self.move_camera(theta = -45*DEGREES) self.begin_ambient_camera_rotation(rate=PI/10, about="theta") self.wait() self.play(Create(rects), run_time=3) self.play(Create(graph2)) self.wait() self.stop_ambient_camera_rotation() self.wait() self.begin_ambient_camera_rotation(rate=PI/10, about="phi") self.wait() self.stop_ambient_camera_rotation()
Scene2
创建三维曲面并移动摄像机
class myjob(ThreeDScene): def construct(self): self.set_camera_orientation(phi = 60*DEGREES, theta = -45*DEGREES) axes = ThreeDAxes() graph = axes.plot(lambda x : x**2, x_range=[-2,2], color=YELLOW) surface = Surface( # 旧版为ParametricSurface,已摒弃 lambda u,v : axes.c2p(v*np.cos(u), v*np.sin(u), 0.5*v**2), # 猜想为coordinates to point u_range=[0,2*PI], # 旧版为u_min和u_max,已摒弃 v_range=[0,3], checkerboard_colors={GREEN,RED} ) three_d_stuff=VGroup(axes, graph, surface) self.add(axes, graph) self.begin_ambient_camera_rotation(rate=PI/20) self.play(Create(surface)) self.play(three_d_stuff.animate.shift(LEFT*5))
Scene3
创造旋转平面形成曲面的动画
class myjob(ThreeDScene): def construct(self): self.set_camera_orientation(phi = 45*DEGREES, theta = -45*DEGREES) axes = ThreeDAxes(y_range=[-3,10,3],y_length=7).add_coordinates() graph = axes.plot(lambda x : x, x_range=[0,3], color=RED_B) area = axes.get_area(graph=graph, x_range=[0,3]) e=ValueTracker(0) surface = always_redraw( lambda : Surface( lambda u,v : axes.c2p(v, v*np.cos(u), v*np.sin(u)), u_range=[0,e.get_value()], v_range=[0,3], checkerboard_colors={GREEN,PURPLE} ) ) self.add(axes, surface) self.begin_ambient_camera_rotation(rate=PI/15) self.play(LaggedStart(Create(surface), Create(area))) self.play( Rotating(area, axis=RIGHT, radians=2*PI, about_point=axes.c2p(0,0,0)), e.animate.set_value(2*PI), run_time=6, rate_func=linear ) self.stop_ambient_camera_rotation() self.wait()
浙公网安备 33010602011771号