Value Tracker
The defined value tracker
VMobject in value_tracker.py
- Class ValueTracker(Mobject)
- Class ExponentialValueTracker(ValueTracker)
- Class ComplexValueTracker(ValueTracker)
Class ValueTracker(Mobject)
ValueTracker is used to generate a value tracker
Mobject for given parameters.
Example
Code
# folder/file: tut/manim_mobject_valuetracker_001a.py
import numpy as np
from manimlib.scene.scene import Scene
from manimlib.mobject.value_tracker import ValueTracker
from manimlib.mobject.geometry import Dot, Line
from manimlib.mobject.number_line import NumberLine
from manimlib.mobject.svg.tex_mobject import TexMobject
from manimlib.mobject.numbers import DecimalNumber, Integer
class manim_mobject_valuetracker_001a(Scene):
def construct(self):
def linear(t): return t
def v_tracker(obj):
obj.set_value(v.get_value())
obj.set_x(v.get_value()-7)
v=ValueTracker(1)
self.add(NumberLine(x_min=-6,x_max=6).move_to([0,-1.5,0]))
dotv = Dot().add_updater(lambda i: i.set_x(v.get_value()-7))
self.add(dotv.move_to([-4,-1.5,0]))
textv = DecimalNumber(v.get_value()).add_updater(v_tracker)
self.add(textv.move_to([-4,-2.5,0]))
self.play(v.set_value,13,rate_func=linear,run_time=2)
self.wait()
value1=ValueTracker(0.0000001)
text1 = DecimalNumber(value1.get_value(),num_decimal_places=5).add_updater(lambda x: x.set_value(value1.get_value())).move_to([-3,3,0])
self.add(text1)
self.play(value1.set_value,1,run_time=2)
self.wait()
text2 = DecimalNumber(value1.get_value(),num_decimal_places=5)
self.add(text2.move_to([3,3,0]))
def update_text(obj):
obj.set_value(np.cos(value1.get_value()))
text2.add_updater(update_text)
self.play(value1.set_value,0.0000001,run_time=2)
self.wait()
def update_tracker(obj,obj1):
dx=x.get_value()-update_tracker.x
obj.increment_value(-dx),
if (x.get_value()**2+y.get_value()**2+z.get_value()**2) > 1 :
obj.increment_value(dx)
update_tracker.x=x.get_value()
z.set_value((1-x.get_value()**2-y.get_value()**2)**0.5)
return obj.get_value()
x=ValueTracker(1)
update_tracker.x=x.get_value()
y=ValueTracker(0)
z=ValueTracker(0)
self.add(Line((0,0,0)).move_to([-3.5,1.5,0]))
dot1 = Dot().add_updater(lambda i: i.set_x(x.get_value()-4))
self.add(dot1.move_to([-4,1.5,0]))
self.add(Line((0,0,0)).move_to([-3.5,1,0]))
dot2 = Dot().add_updater(lambda i: i.set_x(y.get_value()-4))
self.add(dot2.move_to([-4-1,1,0]))
self.add(Line((0,0,0)).move_to([-3.5,0.5,0]))
dot3 = Dot().add_updater(lambda i: i.set_x(z.get_value()-4))
self.add(dot3.move_to([-4-1,0.5,0]))
def update_texty(obj):
obj.set_value(update_tracker(y,x))
textx = DecimalNumber(x.get_value(),num_decimal_places=5).add_updater(lambda i: i.set_value(x.get_value()))
self.add(textx.move_to([-4+2,1.5,0]))
texty = DecimalNumber(y.get_value(),num_decimal_places=5).add_updater(update_texty)
self.add(texty.move_to([-4+2,1,0]))
textz = DecimalNumber(z.get_value(),num_decimal_places=5).add_updater(lambda i: i.set_value(z.get_value()))
self.add(textz.move_to([-4+2,0.5,0]))
total = DecimalNumber(1,num_decimal_places=22).add_updater(lambda i: i.set_value((x.get_value()**2+y.get_value()**2+z.get_value()**2)))
self.add(total.next_to(texty,buff=0.7))
self.play(x.set_value,0.0000001,run_time=2)
self.wait()
Output
Class ExponentialValueTracker(ValueTracker)
ExponentialValueTracker is used to generate a exponential value tracker
Mobject for given parameters.
Example
Code
# folder/file: tut/manim_mobject_ExponentialValueTracker_001a.py
import numpy as np
from manimlib.scene.scene import Scene
from manimlib.mobject.value_tracker import ExponentialValueTracker
from manimlib.mobject.geometry import Dot, Line
from manimlib.mobject.number_line import NumberLine
from manimlib.mobject.svg.tex_mobject import TexMobject
from manimlib.mobject.numbers import DecimalNumber, Integer
class manim_mobject_ExponentialValueTracker_001a(Scene):
def construct(self):
def linear(t): return t
def e_tracker(obj):
obj.set_value(e.get_value())
obj.set_x(e.get_value()-7)
textv.set_x(np.log(e.get_value())*12/np.log(13)-6)
e=ExponentialValueTracker(1)
self.add(NumberLine(x_min=-6,x_max=6).move_to([0,-1.5,0]))
dotv = Dot(radius=0.15,color="#FF3300").add_updater(lambda i: i.set_x(np.log(e.get_value())*12/np.log(13)-6))
self.add(dotv.move_to([-4,-1.5,0]))
textv = DecimalNumber(e.get_value()).add_updater(lambda i: i.set_value(np.log(e.get_value())*12/np.log(13)+1))
self.add(textv.move_to([-4,-0.5,0]))
dote = Dot().add_updater(lambda i: i.set_x(e.get_value()-7))
self.add(dote.move_to([-4,-1.5,0]))
texte = DecimalNumber(e.get_value()).add_updater(e_tracker)
self.add(texte.move_to([-4,-2.5,0]))
self.play(e.set_value,13,rate_func=linear,run_time=2)
self.wait()
value1=ExponentialValueTracker(0.0000001)
text1 = DecimalNumber(value1.get_value(),num_decimal_places=5).add_updater(lambda x: x.set_value(value1.get_value())).move_to([-3,3,0])
self.add(text1)
self.play(value1.set_value,1,run_time=2)
self.wait()
text2 = DecimalNumber(value1.get_value(),num_decimal_places=5)
self.add(text2.move_to([3,3,0]))
def update_text(obj):
obj.set_value(np.cos(value1.get_value()))
text2.add_updater(update_text)
self.play(value1.set_value,0.0000001,run_time=2)
self.wait()
def update_tracker(obj,obj1):
dx=x.get_value()-update_tracker.x
obj.increment_value(-dx),
if (x.get_value()**2+y.get_value()**2+z.get_value()**2) > 1 :
obj.increment_value(dx)
update_tracker.x=x.get_value()
z.set_value((1-x.get_value()**2-y.get_value()**2)**0.5)
return obj.get_value()
x=ExponentialValueTracker(1)
update_tracker.x=x.get_value()
y=ExponentialValueTracker(0)
z=ExponentialValueTracker(0)
self.add(Line((0,0,0)).move_to([-3.5,1.5,0]))
dot1 = Dot().add_updater(lambda i: i.set_x(x.get_value()-4))
self.add(dot1.move_to([-4,1.5,0]))
self.add(Line((0,0,0)).move_to([-3.5,1,0]))
dot2 = Dot().add_updater(lambda i: i.set_x(y.get_value()-4))
self.add(dot2.move_to([-4-1,1,0]))
self.add(Line((0,0,0)).move_to([-3.5,0.5,0]))
dot3 = Dot().add_updater(lambda i: i.set_x(z.get_value()-4))
self.add(dot3.move_to([-4-1,0.5,0]))
def update_texty(obj):
obj.set_value(update_tracker(y,x))
textx = DecimalNumber(x.get_value(),num_decimal_places=5).add_updater(lambda i: i.set_value(x.get_value()))
self.add(textx.move_to([-4+2,1.5,0]))
texty = DecimalNumber(y.get_value(),num_decimal_places=5).add_updater(update_texty)
self.add(texty.move_to([-4+2,1,0]))
textz = DecimalNumber(z.get_value(),num_decimal_places=5).add_updater(lambda i: i.set_value(z.get_value()))
self.add(textz.move_to([-4+2,0.5,0]))
total = DecimalNumber(1,num_decimal_places=22).add_updater(lambda i: i.set_value((x.get_value()**2+y.get_value()**2+z.get_value()**2)))
self.add(total.next_to(texty,buff=0.7))
self.play(x.set_value,0.0000001,run_time=2)
self.wait()
Output
Class ComplexValueTracker(ValueTracker)
ComplexValueTracker is used to generate a complex value tracker
Mobject for given parameters.
Example
Code
# folder/file: tut/manim_mobject_ComplexValueTracker_001a.py
import numpy as np
from manimlib.scene.scene import Scene
from manimlib.mobject.value_tracker import ComplexValueTracker, ValueTracker
from manimlib.mobject.geometry import Dot, Line, Circle
from manimlib.mobject.number_line import NumberLine
from manimlib.mobject.svg.tex_mobject import TexMobject
from manimlib.mobject.numbers import DecimalNumber, Integer
class manim_mobject_ComplexValueTracker_001a(Scene):
def construct(self):
def linear(t): return t
def v_tracker(obj):
obj.set_value(v.get_value())
p_tracker(obj)
def p_tracker(obj):
obj.set_x(v.get_value().real)
obj.set_y(v.get_value().imag)
v=ComplexValueTracker(complex(5.5,3.5))
self.add(NumberLine(x_min=-6,x_max=6))
self.add(NumberLine(x_min=-4,x_max=4).rotate(1.5708))
dotv = Dot(color="#FF3300").add_updater(p_tracker)
self.add(dotv.move_to([5.5,3.5,0]))
textv = DecimalNumber(v.get_value()).add_updater(v_tracker)
self.add(textv.move_to([5.5,3.5,0]))
self.play(v.set_value,complex(-5.5,-3.5),rate_func=linear,run_time=2)
self.wait()
def s_tracker(obj):
obj.set_value(np.abs(s.get_value())**0.5)
obj.set_y(np.abs(s.get_value())**0.5)
s=ComplexValueTracker(complex(5.5,3.5))
dots = Dot(color="#FF3300").add_updater(lambda x: x.set_y(np.abs(s.get_value())**0.5))
self.add(dots.move_to([0,0,0]))
texts = DecimalNumber(s.get_value()).add_updater(s_tracker)
self.add(texts.next_to(dots,buff=1))
self.play(s.set_value,complex(-3.5,-3.5),rate_func=linear,run_time=2)
self.wait()
s.set_value(complex(5.5,3.5))
circle1=Circle()
self.add(circle1)
circle1.i=circle1.copy()
def a_tracker(obj):
obj.become(Circle())
obj.scale(np.abs(s.get_value())**0.5)
circle1.add_updater(a_tracker)
self.add(circle1)
self.play(s.set_value,complex(-3.5,-3.5),rate_func=linear,run_time=2)
self.wait()
i=ComplexValueTracker(complex(5.5,3.5))
circle1=Circle()
self.add(circle1)
circle1.i=circle1.copy()
def i_tracker(obj):
obj.become(Circle())
obj.scale(i.get_value().real)
circle1.add_updater(i_tracker)
self.add(circle1)
self.play(i.set_value,complex(-3.5,-3.5),rate_func=linear,run_time=2)
self.wait()
Output