2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.geometry import Line
|
|
|
|
from manimlib.mobject.numbers import DecimalNumber
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
|
|
|
from manimlib.utils.bezier import interpolate
|
|
|
|
from manimlib.utils.config_ops import digest_config
|
2019-02-06 21:32:42 -08:00
|
|
|
from manimlib.utils.config_ops import merge_dicts_recursively
|
2020-06-05 19:24:35 -07:00
|
|
|
from manimlib.utils.iterables import list_difference_update
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.simple_functions import fdiv
|
|
|
|
from manimlib.utils.space_ops import normalize
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-06 21:16:26 -08:00
|
|
|
class NumberLine(Line):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-05-09 14:04:31 -07:00
|
|
|
"color": LIGHT_GREY,
|
2020-06-05 19:24:35 -07:00
|
|
|
"stroke_width": 2,
|
2020-06-05 17:57:21 -07:00
|
|
|
# List of 2 or 3 elements, x_min, x_max, step_size
|
|
|
|
"x_range": [-8, 8, 1],
|
|
|
|
# How big is one one unit of this number line in terms of absolute spacial distance
|
2018-04-06 13:58:59 -07:00
|
|
|
"unit_size": 1,
|
2020-06-05 19:24:35 -07:00
|
|
|
"width": None,
|
2019-02-06 21:16:26 -08:00
|
|
|
"include_ticks": True,
|
2018-04-06 13:58:59 -07:00
|
|
|
"tick_size": 0.1,
|
2020-06-05 17:57:21 -07:00
|
|
|
"longer_tick_multiple": 1.5,
|
|
|
|
"tick_offset": 0,
|
2019-02-06 21:16:26 -08:00
|
|
|
# Change name
|
2020-06-05 17:57:21 -07:00
|
|
|
"numbers_with_elongated_ticks": [],
|
2018-05-09 14:04:31 -07:00
|
|
|
"include_numbers": False,
|
2020-06-05 17:57:21 -07:00
|
|
|
"line_to_number_direction": DOWN,
|
2018-04-06 13:58:59 -07:00
|
|
|
"line_to_number_buff": MED_SMALL_BUFF,
|
|
|
|
"include_tip": False,
|
2020-06-05 17:57:21 -07:00
|
|
|
"tip_config": {}, # TODO
|
2018-08-25 18:32:09 -07:00
|
|
|
"decimal_number_config": {
|
|
|
|
"num_decimal_places": 0,
|
2020-06-05 17:57:21 -07:00
|
|
|
"height": 0.25,
|
2019-02-11 22:14:00 -08:00
|
|
|
},
|
|
|
|
"exclude_zero_from_default_numbers": False,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2018-01-22 14:41:05 -08:00
|
|
|
|
2020-06-05 17:57:21 -07:00
|
|
|
def __init__(self, x_range=None, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_config(self, kwargs)
|
2020-06-05 19:24:35 -07:00
|
|
|
if x_range is None:
|
|
|
|
x_range = self.x_range
|
|
|
|
if len(x_range) == 2:
|
|
|
|
x_range = [*x_range, 1]
|
2020-06-05 17:57:21 -07:00
|
|
|
|
2020-06-05 19:24:35 -07:00
|
|
|
x_min, x_max, x_step = x_range
|
2020-06-05 17:57:21 -07:00
|
|
|
# A lot of old scenes pass in x_min or x_max explicitly,
|
|
|
|
# so this is just here to keep those workin
|
|
|
|
self.x_min = kwargs.get("x_min", x_min)
|
|
|
|
self.x_max = kwargs.get("x_max", x_max)
|
|
|
|
self.x_step = kwargs.get("x_step", x_step)
|
|
|
|
|
|
|
|
super().__init__(self.x_min * RIGHT, self.x_max * RIGHT, **kwargs)
|
2020-06-05 19:24:35 -07:00
|
|
|
if self.width:
|
|
|
|
self.set_width(self.width)
|
|
|
|
else:
|
|
|
|
self.scale(self.unit_size)
|
2020-06-05 17:57:21 -07:00
|
|
|
self.center()
|
2019-02-06 21:16:26 -08:00
|
|
|
|
2017-08-24 23:10:53 -07:00
|
|
|
if self.include_tip:
|
|
|
|
self.add_tip()
|
2019-02-11 22:14:00 -08:00
|
|
|
if self.include_ticks:
|
2020-06-05 17:57:21 -07:00
|
|
|
self.add_ticks()
|
2018-05-09 14:04:31 -07:00
|
|
|
if self.include_numbers:
|
|
|
|
self.add_numbers()
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2020-06-05 17:57:21 -07:00
|
|
|
def get_tick_range(self):
|
|
|
|
if self.include_tip:
|
|
|
|
x_max = self.x_max
|
|
|
|
else:
|
|
|
|
x_max = self.x_max + self.x_step
|
|
|
|
return np.arange(self.x_min, x_max, self.x_step)
|
|
|
|
|
|
|
|
def add_ticks(self):
|
|
|
|
ticks = VGroup()
|
|
|
|
for x in self.get_tick_range():
|
|
|
|
size = self.tick_size
|
|
|
|
if x in self.numbers_with_elongated_ticks:
|
|
|
|
size *= self.longer_tick_multiple
|
|
|
|
ticks.add(self.get_tick(x, size))
|
|
|
|
self.add(ticks)
|
|
|
|
self.ticks = ticks
|
2015-09-30 14:22:17 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
def get_tick(self, x, size=None):
|
|
|
|
if size is None:
|
|
|
|
size = self.tick_size
|
|
|
|
result = Line(size * DOWN, size * UP)
|
2019-02-06 21:16:26 -08:00
|
|
|
result.rotate(self.get_angle())
|
2017-08-07 16:17:35 -07:00
|
|
|
result.move_to(self.number_to_point(x))
|
2019-02-06 21:16:26 -08:00
|
|
|
result.match_style(self)
|
2017-08-07 16:17:35 -07:00
|
|
|
return result
|
|
|
|
|
2016-07-15 18:16:06 -07:00
|
|
|
def get_tick_marks(self):
|
2020-06-05 17:57:21 -07:00
|
|
|
return self.tick_marks
|
2015-08-01 11:34:33 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
def number_to_point(self, number):
|
2018-04-06 13:58:59 -07:00
|
|
|
alpha = float(number - self.x_min) / (self.x_max - self.x_min)
|
2020-06-05 17:57:21 -07:00
|
|
|
return interpolate(self.get_start(), self.get_end(), alpha)
|
2015-09-25 19:43:53 -07:00
|
|
|
|
2015-09-30 14:22:17 -07:00
|
|
|
def point_to_number(self, point):
|
2020-06-05 17:57:21 -07:00
|
|
|
start, end = self.get_start_and_end()
|
|
|
|
unit_vect = normalize(end - start)
|
2018-08-22 14:48:42 -07:00
|
|
|
proportion = fdiv(
|
2020-06-05 17:57:21 -07:00
|
|
|
np.dot(point - start, unit_vect),
|
|
|
|
np.dot(end - start, unit_vect),
|
2017-02-16 13:03:26 -08:00
|
|
|
)
|
2018-08-22 14:48:42 -07:00
|
|
|
return interpolate(self.x_min, self.x_max, proportion)
|
2015-09-30 14:22:17 -07:00
|
|
|
|
2019-04-09 18:51:18 -07:00
|
|
|
def n2p(self, number):
|
|
|
|
"""Abbreviation for number_to_point"""
|
|
|
|
return self.number_to_point(number)
|
|
|
|
|
|
|
|
def p2n(self, point):
|
|
|
|
"""Abbreviation for point_to_number"""
|
|
|
|
return self.point_to_number(point)
|
|
|
|
|
2019-02-06 21:16:26 -08:00
|
|
|
def get_unit_size(self):
|
|
|
|
return (self.x_max - self.x_min) / self.get_length()
|
|
|
|
|
2020-06-05 17:57:21 -07:00
|
|
|
def get_number_mobject(self, x,
|
2019-02-06 21:16:26 -08:00
|
|
|
number_config=None,
|
|
|
|
direction=None,
|
|
|
|
buff=None):
|
2020-06-05 17:57:21 -07:00
|
|
|
if number_config is None:
|
|
|
|
number_config = {}
|
2019-02-06 21:32:42 -08:00
|
|
|
number_config = merge_dicts_recursively(
|
2020-06-05 17:57:21 -07:00
|
|
|
self.decimal_number_config, number_config
|
2019-02-06 21:32:42 -08:00
|
|
|
)
|
2019-03-23 10:52:25 -07:00
|
|
|
if direction is None:
|
2020-06-05 17:57:21 -07:00
|
|
|
direction = self.line_to_number_direction
|
|
|
|
if buff is None:
|
|
|
|
buff = self.line_to_number_buff
|
2019-02-06 21:16:26 -08:00
|
|
|
|
2020-06-05 17:57:21 -07:00
|
|
|
num_mob = DecimalNumber(x, **number_config)
|
2019-02-06 21:16:26 -08:00
|
|
|
num_mob.next_to(
|
2020-06-05 17:57:21 -07:00
|
|
|
self.number_to_point(x),
|
2019-02-06 21:16:26 -08:00
|
|
|
direction=direction,
|
|
|
|
buff=buff
|
|
|
|
)
|
2020-06-05 19:24:35 -07:00
|
|
|
if x < 0 and self.line_to_number_direction[0] == 0:
|
2020-06-05 17:57:21 -07:00
|
|
|
# Align without the minus sign
|
|
|
|
num_mob.shift(num_mob[0].get_width() * LEFT / 2)
|
2019-02-06 21:16:26 -08:00
|
|
|
return num_mob
|
|
|
|
|
2020-06-05 19:24:35 -07:00
|
|
|
def add_numbers(self, x_values=None, excluding=None, **kwargs):
|
|
|
|
if x_values is None:
|
2020-06-05 17:57:21 -07:00
|
|
|
x_values = self.get_tick_range()
|
2020-06-05 19:24:35 -07:00
|
|
|
if excluding is not None:
|
|
|
|
x_values = list_difference_update(x_values, excluding)
|
2018-02-22 23:02:54 +01:00
|
|
|
|
2020-06-05 17:57:21 -07:00
|
|
|
self.numbers = VGroup()
|
|
|
|
for x in x_values:
|
|
|
|
self.numbers.add(self.get_number_mobject(x, **kwargs))
|
2018-05-09 14:04:31 -07:00
|
|
|
self.add(self.numbers)
|
2020-06-05 19:24:35 -07:00
|
|
|
return self.numbers
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
class UnitInterval(NumberLine):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2020-06-05 17:57:21 -07:00
|
|
|
"x_range": [0, 1, 0.1],
|
|
|
|
"unit_size": 10,
|
2018-04-06 13:58:59 -07:00
|
|
|
"numbers_with_elongated_ticks": [0, 1],
|
2019-03-25 16:43:16 -07:00
|
|
|
"decimal_number_config": {
|
|
|
|
"num_decimal_places": 1,
|
|
|
|
}
|
2015-09-25 19:43:53 -07:00
|
|
|
}
|