class Tute2(Scene):
def construct(self):

# 1. ValueTracker
e = ValueTracker(0.01)

# 2. PolarPlane
plane = PolarPlane(radius_max=3).add_coordinates().shift(LEFT * 2)
# radius_max는 int 단위로 입력
graph1 = always_redraw(lambda :
ParametricFunction(lambda t : plane.polar_to_point(2*np.sin(3*t), t),
t_range=[0, e.get_value()], color=GREEN)
)
# ParametricFunction은 (lambda x : x를 매개변수로 하는 함수식) 형태로 사용한다.
# .polar_to_point는 polar plane에서 좌표를 얻는 메소드
dot1 = always_redraw(lambda : Dot(fill_color=GREEN, fill_opacity=0.8).scale(0.5).move_to(graph1.get_end()))

# 3. Axes
axes = Axes(x_range=[0,4,1], x_length=4, y_range=[-3,3,1], y_length=4).shift(RIGHT * 4)
axes.add_coordinates()
graph2 = always_redraw(lambda :
axes.plot(lambda x : 2*np.sin(3*x), x_range=[0, e.get_value()], color=GREEN))
dot2 = always_redraw(lambda : Dot(fill_color=GREEN, fill_opacity=0.8).scale(0.5).move_to(graph2.get_end()))
# 4. MathTex
title = MathTex("f(\\theta) = 2sin(3\\theta)", color=GREEN).next_to(axes, UP, buff=0.2)

VG = VGroup(graph1, graph2, dot1, dot2)

# play : LaggedStart
self.play(LaggedStart(
Write(plane), Create(axes), Write(title), run_time=3, lag_ratio=0.5
))
self.wait(1)
self.add(VG)
self.play(e.animate.set_value(PI), run_time=4, rate_func=linear)
self.wait(2)



  • ValueTracker(value)는 plot이나 vector등 값의 변화에 따른 변화를 추적하며 나타내고자 할 때 그 변화시킬 값을 저장하는 class이다. 주로 always_redraw() 함수와 함께 사용된다.
  • PolarPlane()은 주인장의 무지로 인해 처음보는 형태의 그래프라 잘 모르겠지만 거리와 각도를 나타내는 좌표평면 class인 것 같다.
  • always_redraw(lambda : [your function & graph]) 함수는 ValueTracker class와 주로 같이 쓰이는데 lambda 함수에 plot할 graph를 적으면 매 fps마다 해당 graph를 ValueTracker의 value값에 따라 update하는 updator기능이다.
  • ParametricFunctiond은 lambda t : [t를 매개변수로 하는 함수식] 형태로 사용한다. t_range 옵션을 통해 t의 범위를 설정할 수 있다.
  • ValueTracker.get_value() : ValueTracker의 현재 value를 얻어서 return한다.
  • ValueTracker.set_value(value) : ValueTracker의 현재 value를 set한다.
  • Dot() 함수는 말 그대로 점을 plotting 할때 쓰는 class이다. plot한 graph.get_end() 메소드를 이용해서 그래프의 end point 좌표를 얻을 수 있다.
  • Axes는 우리가 알고있는 보통의 x,y 좌표계를 그리는 class이다.
  • Axes.plot(lambda x : x를 매개변수로 하는 함수식) 형태로 graph를 좌표계 위에 그릴 수 있다.
  • MathTex는 특히 수식을 입력할 때 사용하는 class이다. LaTeX 문법을 기반으로 하기 때문에 원하는 수식을 입력하려면 Latex 문법을 알아야 한다.
  • VGroup은 원하는 class들이 저장된 변수들을 하나의 그룹으로 그룹화한다. 그룹화된 변수는 하나처럼 함께 움직일 수도 있고 그룹화하였어도 따로따로 움직일 수도 있다.
  • LaggedStart() 는 self.play를 시작하는 하나의 방식으로 , lag_ratio를 통해서 play를 시작할 때 약간씩 딜레이를 줄 수 있다. (이전 포스팅 Lag_ratio참고)
  • e.animate.set_value(PI)를 통해서 0.01로 설정되어있던 ValueTracker의 값을 PI(3.14)까지 fps마다 변경한다. 이때 run_time을 통해서 몇초동안 변하게 할것인지를 결정할 수 있고, rate_func를 통해서 매 초당 변화하는 정도를 동일하게(linear)할 것인지 또는 다이나믹을 줄 것인지를 결정할 수 있다.



19a8c423b79c3faf689fe8b115ef0468f7d775602a


실행 결과