티스토리 뷰

1일 1마님 시리즈는 예제 코드를 하루에 하나씩 따라하면서 마님 라이브러리 사용에 익숙해지기 위해 만든 시리즈입니다! 간단하게 코드 + 코드 리뷰 + 실행 결과만 작성합니다!

class LinearTransformation(LinearTransformationScene):
    # 1. For LinearTransformationScene, you need to do initiation first
    # This make the coordinates on the screen
    def __init__(self):
        LinearTransformationScene.__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
            show_basis_vectors=True
        )
    
    # 2. And Then You make the construct fuction
    def construct(self):
        # 3. Make Matrix
        matrix = [[1,2],[2,1]]
        
        matrix_tex = MathTex("A = \\begin{bmatrix} 1 & 2 \\\ 2 & 1 \\end{bmatrix}")
        matrix_tex.to_edge(UL).add_background_rectangle()

        # 4. Unit square
        unit_square = self.get_unit_square()

        # 5. Text
        text = always_redraw(lambda : Tex("Det(A)").set(width=0.7).move_to(unit_square.get_center()))

        # 6. vector
        vect = self.get_vector([1, -2], color=PURPLE_B)

        # 7. figures
        rect1 = Rectangle(height=2, width=1, stroke_color=BLUE_A,
                          fill_color = BLUE_D, fill_opacity=0.6).shift(UP*2 + LEFT*2)
        circ1 = Circle(radius=1, stroke_color=BLUE_A, fill_color=BLUE_D,
                       fill_opacity=0.6).shift(DOWN*2 + RIGHT*2)

        # Tex_VG = VGroup(matrix_tex, text)

        self.add_background_mobject(matrix_tex, text) 
        # Adds the mobjects to the special list self.background_mobjects.
        
        # NEVER ADD self.wait() during assign your mobjects! It will make an Error!
        
        self.add_transformable_mobject(vect, unit_square, rect1, circ1) 
        # Adds the mobjects to the special list self.transformable_mobjects.
        # This list have all Mobjects which will be Transformed by your function
        
        # play method1.
        self.apply_matrix(matrix) # you can't apply run_time like kargs options with this method1.
        self.wait(3)

        # play method2.
        VG = VGroup(*self.transformable_mobjects) 
        self.play(ApplyMatrix(matrix, VG), run_time=6)
        self.wait(3)

 

오늘은 모르는 버그가 발생해서 고생좀 했다.

self.wait()은 변수를 생성하고 있는 와중에는 실행해서는 안된다. self.play() 실행 이후에만 실행하자.

self.play()실행 없이 self.wait()을 실행하면 오류가 나는데 에러 메세지가 self.wait()을 타겟으로 작성되지 않고, self.wait()으로 인해 생성된 Mobject가 아닌 time과 관련된 객체에 의해서 다른 메서드가 잘못된 것으로 출력되기 때문에 입문자에게는 막막하다.

질문글도 많지 않고, 관련된 질문도 없어서 해결하는데 고생을 좀 했다.

예를들어, 나의 경우 self.apply_matrix()에는 반드시 submobjects로 VMobject만 전달되어야만 한다는 에러가 떴다. 근데 이게 self.wait()의 문제일 거라고 생각하기가 쉽지 않았다. 어찌되었든, 해결완료

  • 지금까지 상속받던 Scene class와는 다르게 LinearTransformation을 작성하기 위해서는 LinearTransformationScene class를 상속받아야 한다.
  • 그리고 정말 중요한 것은, LinearTransformationScene을 상속받을때는 def __init__(self): 를 통해서 Initiation을 해줘야 한다. (Scene에서는 필요 없었다.)
  • Initiation에 몇가지 옵션을 전달해서 어떤 형태의 Plane을 만들지를 결정할 수 있고, 따로 self.add라던가 play라던가 해주지 않아도 initiation하면 plane은 형성된다.
  • LinearTransformation을 위한 matrix는 m*n차원 list형태로 전달하면 된다.
  • Mobject.add_background_rectangle() 메서드를 통해 백그라운드 사각형 이미지를 지정해둘 수 있다.(실행결과 참고)
  • LinearTransformationScene.get_unit_square() 메서드는 plane에 단위벡터로 구성된 사각형을 만든다.
  • always_redraw(lambda : Your Mobject)를 통해 Mobject의 변화를 추적해 Your Mobject를 update 할 수 있다.
  • LinearTransformationScene.get_vector() 메서드를 통해 plane위에 벡터를 얻을 수 있다.
  • Rectangle과 Circle은 말 그대로 사각형과 원을 생성한다. (단, plot되는 것은 아니다. 그냥 메모리에 생성만 한다.)
  • LinearTransformationScene.add_background_mobject(Mobject List)는 Transformation하지 않을 mobject들을 지정하는 self.background_mobjects에 Mobject List를 전달하여 저장한다.
  • LinearTransformationScene.add_transformable_mobject(Mobject List)는 반대로 Transformation할 mobject들을 지정하는 self.transformable_mobjects에 Mobject List를 전달하여 저장한다.
  • 이때 두 기능이 plot까지 진행한다.
  • Transformation을 play하는 방법은 여러가지 있겠지만, 일단 내가 알아낸 방법은 두가지이다.
    • 1. self.apply_matrix(Your Transformation Matrix) 이 방법은 간단하지만 self.play()를 사용하지 않기때문에 play의 kwargs를 사용할 수 없다. 따라서 run_time,  lag_ratio등 옵션의 사용에 제약이 존재한다.
    • 2. 반면 위에서 Transformation할 mobject들을 저장한 self.transformable_mobjects를 다시 VGroup으로 묶어서 self.play(ApplyMatrix(Your Transformation Matrix, VG), kwargs)를 사용하면 play() 메서드에 대한 다양한 옵션들을 사용할 수 있다. 

method1. run_time 등 옵션 조작 불가
method2. run_time 조작가능

댓글