본문 바로가기

Programming/Manim code

(57)
Sine-curve by rotating dot around the circle Code from manimlib.imports import * class Sine_Curve(Scene): def construct(self): self.show_axis() self.show_circle() self.move_dot_and_draw_curve() self.wait() def show_axis(self): x_start = np.array([-6,0,0]) x_end = np.array([6,0,0]) y_start = np.array([-4,-2,0]) y_end = np.array([-4,2,0]) x_axis = Line(x_start, x_end) y_axis = Line(y_start, y_end) self.add(x_axis, y_axis) self.add_x_labels()..
[49] Update Animation class UpdateFromFunc class def update_from_func(self): dot = Dot().to_edge(LEFT) label = TextMobject("Hello").next_to(dot,UP) def update_func(mob): mob.next_to(dot,UP) self.play( dot.to_edge,RIGHT, UpdateFromFunc(label, update_func), run_time=5, ) def update_from_func2(self): dot = Dot().to_edge(LEFT) label = TextMobject("Hello").next_to(dot,UP) def update_func(mob): mob.next_to(dot, UP) x = dot.get_c..
[48]always_redraw class AlwaysRedraw(Scene): def construct(self): self.draw_tangent() def draw_tangent(self): x_axis = NumberLine(x_min=-5,x_max=5) def sine_func(x): return 1.5 * np.sin(x * PI) graph = FunctionGraph(sine_func, x_min=-4, x_max=4) tracker = ValueTracker(-4) def get_tangent_line(): line = Line( ORIGIN, 2 * RIGHT, color=RED, stroke_width=5, ) dx = 0.0001 x = tracker.get_value() p0 = np.array([x-dx,si..
[47] add_updater def moving_dot(self): x_axis = NumberLine(x_min=-5, x_max=5) dot = Dot(color=RED, radius=0.15).move_to(x_axis.get_left()) number = DecimalNumber(-5, color=RED).next_to(dot, UP) number.add_updater(lambda m: m.next_to(dot, UP)) number.add_updater(lambda m: m.set_value(dot.get_center()[0])) self.add(x_axis, dot, number) self.play(dot.shift, RIGHT * 10, rate_func=there_and_back, run_time=10) self.wa..
[46]Mobject moving/coloring Animation def simple(self): circle = Circle() circle.save_state() self.play(circle.to_edge, LEFT) self.play(circle.to_edge, RIGHT) self.play(circle.restore) self.play(circle.to_edge, UP) self.play(circle.to_edge, DOWN) self.play(circle.restore) self.play(circle.set_fill, RED, {"opacity": 0.5}) self.play(circle.set_color_by_gradient, YELLOW) self.play(circle.to_corner, UL) self.play(circle.to_corner, DR) s..
[45]Special Effects def broadcast(self): text = VGroup( TextMobject("Monday"), TextMobject("Tuesday"), TextMobject("Wednesday"), TextMobject("Thursday"), TextMobject("Friday"), ).arrange(DOWN) dot = Dot(color=RED).move_to(2 * RIGHT) self.add(dot) def get_broadcast(): return Broadcast(dot, big_radius=5, color=RED, run_time=5) self.play( LaggedStartMap(FadeIn, text, run_time=4, lag_ratio=0.7), Succession(*[ get_broad..
[44]AnimationGroup lag_ration test class AnimationGroupTest(Scene): def construct(self): self.animation_group() def animation_group(self): self.t1 = TextMobject("Hello").shift(UP*2).to_edge(LEFT, buff=1) self.t2 = TextMobject("Wordld").shift(UP*2).to_edge(RIGHT, buff=1) self.shape1 = Circle().to_edge(LEFT, buff=1) self.shape2 = Square().to_edge(RIGHT, buff=1) self.arrow1 = Arrow().shift(DOWN*2).to_edge(LEFT, buff=..
[43]Transform(3) ApplyMethod arrow = Arrow().to_edge(LEFT) self.add(arrow) self.play(ApplyMethod(arrow.to_edge, RIGHT)) self.wait() ApplyPointwiseFunction Class def apply_pointwise_function(self): circle = Circle(radius=0.5) circle.save_state() self.add(circle) self.wait() self.play( ApplyPointwiseFunction( lambda p: 10 * p / get_norm(p), circle ) ) self.play(Restore(circle)) circle.set_fill(color=RED, opacity=0..
[42]Transform (2) CyclicReplace def cyclic(self): tri = Triangle().to_edge(LEFT, buff=0.5) square = Square() circle = Circle().to_edge(RIGHT, buff=0.5) group = VGroup(tri, square, circle) self.add(group) self.wait() self.play(CyclicReplace(tri, square, circle), run_time=4) self.wait() ScaleInPlace def scale(self): tri = Triangle().to_edge(LEFT, buff=1) square = Square() circle = Circle().to_edge(RIGHT, buff=1) gr..
[41]Transform(1) class TransformTest(Scene): def construct(self): self.transform_to_target() # self.cyclic() def get_text(self, str): return Text(str, size=0.5, color=YELLOW, stroke_width=0) def transform_to_target(self): self.left_name = self.get_text("A").shift(UP * 2 + LEFT * 3) self.right_name = self.get_text("B").shift(UP * 2 + RIGHT * 3) self.square = Square().shift(LEFT*3) self.circle = Circle(fill_opacit..