본문 바로가기

Programming/Manim code

Draw elbow-line between two dots

반응형

code

from manimlib.imports import *

class ConnectObjects_ByLine(Scene):
    def construct(self):
        #1. Creates dots and display them
        dot1, dot2 = Dot(), Dot()
        dot1.shift(UP)

        connected_line = VGroup(Line(dot1.get_center(), dot2.get_center()))
        self.add(dot1, dot2, connected_line)

        #2. makes update function to draw the line between the dots
        def update_line(mob):
            x1, y1 = dot1.get_center()[0], dot1.get_center()[1]
            x2, y2 = dot2.get_center()[0], dot2.get_center()[1]
            h_line = Line(np.array([x1,y1,0]),np.array([x1,y2,0]) )
            v_line = Line(np.array([x1, y2, 0]), np.array([x2, y2, 0]))

            line_group = VGroup(h_line, v_line)
            mob.become(line_group)

        connected_line.add_updater(update_line)

        #3. move dot1 and dot2
        self.play(dot1.to_edge, LEFT, run_time=2)
        self.play(dot1.to_edge, RIGHT, run_time=2)
        self.play(dot2.to_edge, UP, run_time=2)
        self.play(dot2.move_to, ORIGIN, run_time=2)
        self.play(dot1.move_to, ORIGIN)

        self.wait()

 

GIF

MP4

 


Go to the Table of Contents for Manim Code Collection

 

 

반응형