본문 바로가기

Programming/Manim code

Moving Sine-Wave

반응형


Code

from manimlib.imports import *


class MovingWave(Scene):
    def construct(self):
        self.draw_axis()
        self.draw_sine_wave()

    def draw_axis(self):
        x_axis = Line(np.array([-4,0,0]), np.array([4,0,0]))
        y_axis = Line(np.array([-4,2,0]), np.array([-4,-2,0]))

        self.add(x_axis, y_axis)
        self.x_min = -4
        self.x_max = 4

    def draw_sine_wave(self):
        sine_wave = self.get_sine_wave()
        vt = ValueTracker(0)

        def update_wave(wave):
            wave.become(self.get_sine_wave(dx=vt.get_value()))

        sine_wave.add_updater(update_wave)

        self.add(sine_wave)
        self.wait(2)

        # move sine wave 2PI in 4 seconds
        moving_time = 4
        moving_length = 2*PI
        self.play(vt.set_value, moving_length, run_time=moving_time, rate_func=linear)
        self.wait()


    def get_sine_wave(self, dx=0):
        return FunctionGraph(
            # to start on sin(0)
            lambda x: np.sin((x-self.x_min + dx)),
            x_min=self.x_min, x_max=self.x_max
        )


GIF


MP4

 


Go to the Table of Contents for the Manim Code Collection

반응형