2016-07-19 14:37:18 -07:00
|
|
|
|
2018-03-30 18:42:32 -07:00
|
|
|
from animation.animation import Animation
|
2018-03-30 18:19:23 -07:00
|
|
|
from constants import *
|
2018-03-31 15:11:35 -07:00
|
|
|
from continual_animation.continual_animation import ContinualAnimation
|
|
|
|
from mobject.tex_mobject import TexMobject
|
|
|
|
from mobject.vectorized_mobject import VGroup
|
|
|
|
from mobject.vectorized_mobject import VMobject
|
|
|
|
from mobject.vectorized_mobject import VectorizedPoint
|
|
|
|
from scene.scene import Scene
|
|
|
|
from topics.geometry import BackgroundRectangle
|
2018-03-30 18:19:23 -07:00
|
|
|
from utils.bezier import interpolate
|
|
|
|
from utils.config_ops import digest_config
|
2016-07-19 14:37:18 -07:00
|
|
|
|
2016-07-21 11:00:45 -07:00
|
|
|
class DecimalNumber(VMobject):
|
2016-07-19 14:37:18 -07:00
|
|
|
CONFIG = {
|
|
|
|
"num_decimal_points" : 2,
|
2018-01-17 15:18:02 -08:00
|
|
|
"digit_to_digit_buff" : 0.05,
|
2018-01-23 10:53:24 -08:00
|
|
|
"show_ellipsis" : False,
|
2018-01-29 12:05:59 -08:00
|
|
|
"unit" : None, #Aligned to bottom unless it starts with "^"
|
2018-01-24 18:50:44 +01:00
|
|
|
"include_background_rectangle" : False,
|
2016-07-19 14:37:18 -07:00
|
|
|
}
|
2017-10-12 17:38:25 -07:00
|
|
|
def __init__(self, number, **kwargs):
|
2018-01-24 18:50:44 +01:00
|
|
|
VMobject.__init__(self, **kwargs)
|
|
|
|
self.number = number
|
|
|
|
ndp = self.num_decimal_points
|
|
|
|
|
|
|
|
#Build number string
|
|
|
|
if isinstance(number, complex):
|
|
|
|
num_string = '%.*f%s%.*fi'%(
|
|
|
|
ndp, number.real,
|
|
|
|
"-" if number.imag < 0 else "+",
|
|
|
|
ndp, abs(number.imag)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
num_string = '%.*f'%(ndp, number)
|
|
|
|
negative_zero_string = "-%.*f"%(ndp, 0.)
|
|
|
|
if num_string == negative_zero_string:
|
|
|
|
num_string = num_string[1:]
|
|
|
|
self.add(*[
|
|
|
|
TexMobject(char, **kwargs)
|
2016-07-21 11:00:45 -07:00
|
|
|
for char in num_string
|
2018-01-24 18:50:44 +01:00
|
|
|
])
|
2018-01-17 15:18:02 -08:00
|
|
|
|
2018-01-24 18:50:44 +01:00
|
|
|
#Add non-numerical bits
|
2018-01-17 15:18:02 -08:00
|
|
|
if self.show_ellipsis:
|
|
|
|
self.add(TexMobject("\\dots"))
|
2018-01-23 10:53:24 -08:00
|
|
|
|
2018-01-23 12:04:56 -08:00
|
|
|
|
2018-01-18 18:20:38 -08:00
|
|
|
if num_string.startswith("-"):
|
2016-07-19 14:37:18 -07:00
|
|
|
minus = self.submobjects[0]
|
|
|
|
minus.next_to(
|
|
|
|
self.submobjects[1], LEFT,
|
|
|
|
buff = self.digit_to_digit_buff
|
|
|
|
)
|
|
|
|
|
2018-01-23 10:53:24 -08:00
|
|
|
if self.unit != None:
|
2018-01-29 12:17:25 -08:00
|
|
|
self.unit_sign = TexMobject(self.unit)
|
|
|
|
self.add(self.unit_sign)
|
2018-01-23 10:53:24 -08:00
|
|
|
|
2016-07-19 14:37:18 -07:00
|
|
|
self.arrange_submobjects(
|
|
|
|
buff = self.digit_to_digit_buff,
|
|
|
|
aligned_edge = DOWN
|
|
|
|
)
|
2018-01-23 10:53:24 -08:00
|
|
|
|
2018-01-23 13:42:18 -08:00
|
|
|
#Handle alignment of parts that should be aligned
|
|
|
|
#to the bottom
|
|
|
|
for i, c in enumerate(num_string):
|
|
|
|
if c == "-" and len(num_string) > i+1:
|
|
|
|
self[i].align_to(self[i+1], alignment_vect = UP)
|
2018-01-29 12:17:25 -08:00
|
|
|
if self.unit and self.unit.startswith("^"):
|
|
|
|
self.unit_sign.align_to(self, UP)
|
2018-01-23 13:42:18 -08:00
|
|
|
#
|
|
|
|
if self.include_background_rectangle:
|
2018-01-24 08:55:26 -08:00
|
|
|
self.add_background_rectangle()
|
2018-01-23 10:53:24 -08:00
|
|
|
|
2018-01-24 08:55:26 -08:00
|
|
|
def add_background_rectangle(self):
|
|
|
|
#TODO, is this the best way to handle
|
|
|
|
#background rectangles?
|
|
|
|
self.background_rectangle = BackgroundRectangle(self)
|
|
|
|
self.submobjects = [
|
|
|
|
self.background_rectangle,
|
|
|
|
VGroup(*self.submobjects)
|
|
|
|
]
|
|
|
|
return self
|
2018-01-24 18:50:44 +01:00
|
|
|
|
|
|
|
class Integer(DecimalNumber):
|
2017-05-16 14:29:14 -07:00
|
|
|
CONFIG = {
|
2018-01-24 18:50:44 +01:00
|
|
|
"num_decimal_points" : 0,
|
2017-05-16 14:29:14 -07:00
|
|
|
}
|
2017-05-12 14:55:30 -07:00
|
|
|
|
2017-08-05 20:47:06 -07:00
|
|
|
class ChangingDecimal(Animation):
|
2016-07-19 14:37:18 -07:00
|
|
|
CONFIG = {
|
2017-10-26 21:30:24 -07:00
|
|
|
"num_decimal_points" : None,
|
2018-01-17 15:18:02 -08:00
|
|
|
"show_ellipsis" : None,
|
2017-08-05 20:47:06 -07:00
|
|
|
"position_update_func" : None,
|
2018-01-29 12:05:59 -08:00
|
|
|
"tracked_mobject" : None,
|
2016-07-19 14:37:18 -07:00
|
|
|
}
|
2018-01-09 17:11:40 -08:00
|
|
|
def __init__(self, decimal_number_mobject, number_update_func, **kwargs):
|
2016-07-19 14:37:18 -07:00
|
|
|
digest_config(self, kwargs, locals())
|
2018-01-23 12:04:56 -08:00
|
|
|
self.decimal_number_config = dict(
|
|
|
|
decimal_number_mobject.initial_config
|
2016-07-19 14:37:18 -07:00
|
|
|
)
|
2018-01-23 12:04:56 -08:00
|
|
|
for attr in "num_decimal_points", "show_ellipsis":
|
|
|
|
value = getattr(self, attr)
|
|
|
|
if value is not None:
|
|
|
|
self.decimal_number_config[attr] = value
|
2018-01-24 08:55:26 -08:00
|
|
|
if hasattr(self.decimal_number_mobject, "background_rectangle"):
|
|
|
|
self.decimal_number_config["include_background_rectangle"] = True
|
2018-01-09 17:11:40 -08:00
|
|
|
if self.tracked_mobject:
|
2018-01-17 22:18:05 -08:00
|
|
|
dmc = decimal_number_mobject.get_center()
|
|
|
|
tmc = self.tracked_mobject.get_center()
|
|
|
|
self.diff_from_tracked_mobject = dmc - tmc
|
2018-01-09 17:11:40 -08:00
|
|
|
Animation.__init__(self, decimal_number_mobject, **kwargs)
|
2017-08-05 20:47:06 -07:00
|
|
|
|
|
|
|
def update_mobject(self, alpha):
|
|
|
|
self.update_number(alpha)
|
|
|
|
self.update_position()
|
|
|
|
|
|
|
|
def update_number(self, alpha):
|
2018-01-09 17:11:40 -08:00
|
|
|
decimal = self.decimal_number_mobject
|
2017-10-12 17:38:25 -07:00
|
|
|
new_number = self.number_update_func(alpha)
|
2017-08-08 21:42:23 -07:00
|
|
|
new_decimal = DecimalNumber(
|
2018-01-23 12:04:56 -08:00
|
|
|
new_number, **self.decimal_number_config
|
2017-08-08 21:42:23 -07:00
|
|
|
)
|
2018-01-23 12:04:56 -08:00
|
|
|
new_decimal.match_height(decimal)
|
|
|
|
new_decimal.move_to(decimal)
|
|
|
|
new_decimal.match_style(decimal)
|
2018-01-17 22:17:05 -08:00
|
|
|
|
|
|
|
decimal.submobjects = new_decimal.submobjects
|
|
|
|
decimal.number = new_number
|
2017-08-05 20:47:06 -07:00
|
|
|
|
|
|
|
def update_position(self):
|
|
|
|
if self.position_update_func is not None:
|
2018-01-09 17:11:40 -08:00
|
|
|
self.position_update_func(self.decimal_number_mobject)
|
2017-08-05 20:47:06 -07:00
|
|
|
elif self.tracked_mobject is not None:
|
2018-01-09 17:11:40 -08:00
|
|
|
self.decimal_number_mobject.move_to(self.tracked_mobject.get_center() + self.diff_from_tracked_mobject)
|
2017-08-05 20:47:06 -07:00
|
|
|
|
2018-01-16 15:51:19 -08:00
|
|
|
class ChangeDecimalToValue(ChangingDecimal):
|
2018-01-16 15:49:24 -08:00
|
|
|
def __init__(self, decimal_number_mobject, target_number, **kwargs):
|
|
|
|
start_number = decimal_number_mobject.number
|
|
|
|
func = lambda alpha : interpolate(start_number, target_number, alpha)
|
|
|
|
ChangingDecimal.__init__(self, decimal_number_mobject, func, **kwargs)
|
|
|
|
|
2017-09-06 20:18:19 -07:00
|
|
|
class ContinualChangingDecimal(ContinualAnimation):
|
2018-01-09 17:11:40 -08:00
|
|
|
def __init__(self, decimal_number_mobject, number_update_func, **kwargs):
|
|
|
|
self.anim = ChangingDecimal(decimal_number_mobject, number_update_func, **kwargs)
|
|
|
|
ContinualAnimation.__init__(self, decimal_number_mobject, **kwargs)
|
2017-09-06 20:18:19 -07:00
|
|
|
|
|
|
|
def update_mobject(self, dt):
|
|
|
|
self.anim.update(self.internal_time)
|
|
|
|
|
2017-08-05 20:47:06 -07:00
|
|
|
|
2017-09-06 20:18:19 -07:00
|
|
|
|
2017-08-05 20:47:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-14 13:53:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-19 14:37:18 -07:00
|
|
|
|
|
|
|
|