2015-06-10 22:00:35 -07:00
|
|
|
from helpers import *
|
|
|
|
|
2015-12-19 13:06:09 -08:00
|
|
|
from mobject import Mobject1D
|
|
|
|
from mobject.tex_mobject import TexMobject
|
2015-10-27 21:00:50 -07:00
|
|
|
from scene import Scene
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
class NumberLine(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"color" : BLUE,
|
|
|
|
"numerical_radius" : SPACE_WIDTH,
|
2016-03-08 23:13:41 -08:00
|
|
|
"number_at_center" : 0,
|
2015-10-06 15:27:40 -07:00
|
|
|
"unit_length_to_spatial_width" : 1,
|
2015-10-29 13:45:28 -07:00
|
|
|
"tick_size" : 0.1,
|
|
|
|
"tick_frequency" : 0.5,
|
|
|
|
"leftmost_tick" : None,
|
2015-09-28 16:25:18 -07:00
|
|
|
"numbers_with_elongated_ticks" : [0],
|
2015-10-29 13:45:28 -07:00
|
|
|
"longer_tick_multiple" : 2,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
|
|
|
def __init__(self, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_config(self, kwargs)
|
2015-10-08 11:14:55 -07:00
|
|
|
if self.leftmost_tick is None:
|
2015-11-02 19:09:55 -08:00
|
|
|
self.leftmost_tick = -int(self.numerical_radius-self.number_at_center)
|
2015-09-30 14:22:17 -07:00
|
|
|
self.left_num = self.number_at_center - self.numerical_radius
|
|
|
|
self.right_num = self.number_at_center + self.numerical_radius
|
2015-09-25 19:43:53 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
2015-10-06 15:27:40 -07:00
|
|
|
spatial_radius = self.numerical_radius*self.unit_length_to_spatial_width
|
2015-06-10 22:00:35 -07:00
|
|
|
self.add_points([
|
2015-09-25 19:43:53 -07:00
|
|
|
(b*x, 0, 0)
|
2015-10-06 15:27:40 -07:00
|
|
|
for x in np.arange(0, spatial_radius, self.epsilon)
|
2015-09-25 19:43:53 -07:00
|
|
|
for b in [-1, 1]
|
2015-06-10 22:00:35 -07:00
|
|
|
])
|
2015-09-25 19:43:53 -07:00
|
|
|
self.index_of_left = np.argmin(self.points[:,0])
|
|
|
|
self.index_of_right = np.argmax(self.points[:,0])
|
2015-10-06 15:27:40 -07:00
|
|
|
spatial_tick_frequency = self.tick_frequency*self.unit_length_to_spatial_width
|
2015-06-10 22:00:35 -07:00
|
|
|
self.add_points([
|
2015-09-30 14:22:17 -07:00
|
|
|
(x, y, 0)
|
|
|
|
for num in self.get_tick_numbers()
|
2015-06-10 22:00:35 -07:00
|
|
|
for y in np.arange(-self.tick_size, self.tick_size, self.epsilon)
|
2015-09-30 14:22:17 -07:00
|
|
|
for x in [self.number_to_point(num)[0]]
|
2015-06-10 22:00:35 -07:00
|
|
|
])
|
2015-09-25 19:43:53 -07:00
|
|
|
for number in self.numbers_with_elongated_ticks:
|
|
|
|
self.elongate_tick_at(number, self.longer_tick_multiple)
|
2015-09-30 14:22:17 -07:00
|
|
|
self.number_of_points_without_numbers = self.get_num_points()
|
|
|
|
|
|
|
|
def get_tick_numbers(self):
|
|
|
|
return np.arange(self.leftmost_tick, self.right_num, self.tick_frequency)
|
2015-08-01 11:34:33 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
def elongate_tick_at(self, number, multiple = 2):
|
|
|
|
x = self.number_to_point(number)[0]
|
2015-08-01 11:34:33 -07:00
|
|
|
self.add_points([
|
|
|
|
[x, y, 0]
|
|
|
|
for y in np.arange(
|
|
|
|
-multiple*self.tick_size,
|
|
|
|
multiple*self.tick_size,
|
|
|
|
self.epsilon
|
|
|
|
)
|
|
|
|
])
|
|
|
|
return self
|
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
def number_to_point(self, number):
|
|
|
|
return interpolate(
|
2015-12-31 09:23:06 -08:00
|
|
|
self.get_left(),
|
|
|
|
self.get_right(),
|
2015-09-25 19:43:53 -07:00
|
|
|
float(number-self.left_num)/(self.right_num - self.left_num)
|
|
|
|
)
|
|
|
|
|
2015-09-30 14:22:17 -07:00
|
|
|
def point_to_number(self, point):
|
2015-11-02 19:09:55 -08:00
|
|
|
new_point = point-self.get_center()
|
|
|
|
return self.number_at_center + new_point[0]/self.unit_length_to_spatial_width
|
2015-09-30 14:22:17 -07:00
|
|
|
|
|
|
|
def default_numbers_to_display(self):
|
|
|
|
return self.get_tick_numbers()[::2]
|
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def get_vertical_number_offset(self, direction = DOWN):
|
|
|
|
return 4*direction*self.tick_size
|
2015-09-30 14:22:17 -07:00
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def get_number_mobjects(self, *numbers, **kwargs):
|
2015-10-08 11:14:55 -07:00
|
|
|
#TODO, handle decimals
|
2015-09-25 19:43:53 -07:00
|
|
|
if len(numbers) == 0:
|
2015-09-30 14:22:17 -07:00
|
|
|
numbers = self.default_numbers_to_display()
|
|
|
|
result = []
|
2015-09-25 19:43:53 -07:00
|
|
|
for number in numbers:
|
2015-10-28 17:18:50 -07:00
|
|
|
mob = TexMobject(str(int(number)))
|
2015-09-30 14:22:17 -07:00
|
|
|
vert_scale = 2*self.tick_size/mob.get_height()
|
2015-10-06 15:27:40 -07:00
|
|
|
hori_scale = self.tick_frequency*self.unit_length_to_spatial_width/mob.get_width()
|
2015-09-30 14:22:17 -07:00
|
|
|
mob.scale(min(vert_scale, hori_scale))
|
2015-09-25 19:43:53 -07:00
|
|
|
mob.shift(self.number_to_point(number))
|
2016-03-08 23:13:41 -08:00
|
|
|
mob.shift(self.get_vertical_number_offset(**kwargs))
|
2015-09-30 14:22:17 -07:00
|
|
|
result.append(mob)
|
|
|
|
return result
|
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def add_numbers(self, *numbers, **kwargs):
|
|
|
|
self.numbers = self.get_number_mobjects(
|
|
|
|
*numbers, **kwargs
|
|
|
|
)
|
|
|
|
self.add(*self.numbers)
|
2015-10-08 11:14:55 -07:00
|
|
|
return self
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
class UnitInterval(NumberLine):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"numerical_radius" : 0.5,
|
2015-10-06 15:27:40 -07:00
|
|
|
"unit_length_to_spatial_width" : 2*(SPACE_WIDTH-1),
|
2015-10-29 13:45:28 -07:00
|
|
|
"tick_frequency" : 0.1,
|
|
|
|
"leftmost_tick" : 0,
|
|
|
|
"number_at_center" : 0.5,
|
2015-09-25 19:43:53 -07:00
|
|
|
"numbers_with_elongated_ticks" : [0, 1],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-06 15:27:40 -07:00
|
|
|
class NumberPlane(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"color" : BLUE,
|
|
|
|
"x_radius" : SPACE_WIDTH,
|
|
|
|
"y_radius" : SPACE_HEIGHT,
|
|
|
|
"x_unit_to_spatial_width" : 1,
|
2015-11-02 19:09:55 -08:00
|
|
|
"y_unit_to_spatial_height" : 1,
|
2015-10-29 13:45:28 -07:00
|
|
|
"x_line_frequency" : 1,
|
|
|
|
"x_faded_line_frequency" : 0.5,
|
|
|
|
"y_line_frequency" : 1,
|
|
|
|
"y_faded_line_frequency" : 0.5,
|
|
|
|
"fade_factor" : 0.3,
|
|
|
|
"number_scale_factor" : 0.25,
|
|
|
|
"num_pair_at_center" : np.array((0, 0)),
|
2015-10-06 15:27:40 -07:00
|
|
|
}
|
2015-10-28 16:03:33 -07:00
|
|
|
|
2015-10-06 15:27:40 -07:00
|
|
|
def generate_points(self):
|
2015-10-20 21:55:46 -07:00
|
|
|
#TODO, clean this
|
2015-10-06 15:27:40 -07:00
|
|
|
color = self.color
|
|
|
|
faded = Color(rgb = self.fade_factor*np.array(color.get_rgb()))
|
2015-10-20 21:55:46 -07:00
|
|
|
|
|
|
|
freq_color_tuples = [
|
|
|
|
(self.x_line_frequency, self.y_line_frequency, color),
|
|
|
|
(self.x_faded_line_frequency, self.y_faded_line_frequency, faded),
|
2015-10-06 18:41:26 -07:00
|
|
|
]
|
2015-10-20 21:55:46 -07:00
|
|
|
x_vals = []
|
|
|
|
y_vals = []
|
|
|
|
for x_freq, y_freq, color in freq_color_tuples:
|
|
|
|
if not x_freq or not y_freq:
|
2015-10-06 18:41:26 -07:00
|
|
|
continue
|
2015-10-20 21:55:46 -07:00
|
|
|
x_vals = np.array(filter(lambda x : x not in x_vals, np.arange(
|
|
|
|
0, self.x_radius,
|
|
|
|
self.x_unit_to_spatial_width*x_freq
|
|
|
|
)))
|
|
|
|
y_vals = np.array(filter(lambda y : y not in y_vals, np.arange(
|
|
|
|
0, self.y_radius,
|
2015-11-02 19:09:55 -08:00
|
|
|
self.y_unit_to_spatial_height*y_freq
|
2015-10-20 21:55:46 -07:00
|
|
|
)))
|
|
|
|
x_cont_vals = np.arange(
|
|
|
|
0, self.x_radius,
|
|
|
|
self.epsilon/self.x_unit_to_spatial_width
|
|
|
|
)
|
|
|
|
y_cont_vals = np.arange(
|
|
|
|
0, self.y_radius,
|
2015-11-02 19:09:55 -08:00
|
|
|
self.epsilon/self.y_unit_to_spatial_height
|
2015-10-20 21:55:46 -07:00
|
|
|
)
|
|
|
|
for x_sgn, y_sgn in it.product([-1, 1], [-1, 1]):
|
|
|
|
self.add_points(
|
|
|
|
list(it.product(x_sgn*x_vals, y_sgn*y_cont_vals, [0])) + \
|
|
|
|
list(it.product(x_sgn*x_cont_vals, y_sgn*y_vals, [0])),
|
|
|
|
color = color
|
|
|
|
)
|
2015-10-06 15:27:40 -07:00
|
|
|
self.shift(self.get_center_point())
|
|
|
|
|
2015-10-20 21:55:46 -07:00
|
|
|
|
|
|
|
|
2015-10-06 15:27:40 -07:00
|
|
|
def get_center_point(self):
|
|
|
|
return self.num_pair_to_point(self.num_pair_at_center)
|
|
|
|
|
|
|
|
def num_pair_to_point(self, pair):
|
|
|
|
pair = pair + self.num_pair_at_center
|
2015-11-02 19:09:55 -08:00
|
|
|
result = self.get_center()
|
|
|
|
result[0] += pair[0]*self.x_unit_to_spatial_width
|
|
|
|
result[1] += pair[1]*self.y_unit_to_spatial_height
|
|
|
|
return result
|
|
|
|
|
|
|
|
def point_to_num_pair(self, point):
|
|
|
|
new_point = point-self.get_center()
|
|
|
|
center_x, center_y = self.num_pair_at_center
|
|
|
|
x = center_x + point[0]/self.x_unit_to_spatial_width
|
|
|
|
y = center_y + point[1]/self.y_unit_to_spatial_height
|
|
|
|
return x, y
|
2015-10-06 15:27:40 -07:00
|
|
|
|
|
|
|
def get_coordinate_labels(self, x_vals = None, y_vals = None):
|
|
|
|
result = []
|
|
|
|
nudge = 0.1*(DOWN+RIGHT)
|
|
|
|
if x_vals == None and y_vals == None:
|
|
|
|
x_vals = range(-int(self.x_radius), int(self.x_radius))
|
|
|
|
y_vals = range(-int(self.y_radius), int(self.y_radius))
|
|
|
|
for index, vals in zip([0, 1], [x_vals, y_vals]):
|
|
|
|
num_pair = [0, 0]
|
|
|
|
for val in vals:
|
|
|
|
num_pair[index] = val
|
|
|
|
point = self.num_pair_to_point(num_pair)
|
2015-10-28 17:18:50 -07:00
|
|
|
num = TexMobject(str(val))
|
2015-10-06 15:27:40 -07:00
|
|
|
num.scale(self.number_scale_factor)
|
|
|
|
num.shift(point-num.get_corner(UP+LEFT)+nudge)
|
|
|
|
result.append(num)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def add_coordinates(self, x_vals = None, y_vals = None):
|
|
|
|
self.add(*self.get_coordinate_labels(x_vals, y_vals))
|
|
|
|
return self
|
2015-06-27 04:49:10 -07:00
|
|
|
|
2015-10-12 19:39:46 -07:00
|
|
|
def get_vector(self, coords, **kwargs):
|
|
|
|
if len(coords) == 2:
|
|
|
|
coords = tuple(list(coords) + [0])
|
|
|
|
arrow = Arrow(ORIGIN, coords, **kwargs)
|
|
|
|
arrow.remove_tip()
|
|
|
|
arrow.align_data(Line(ORIGIN, SPACE_WIDTH*LEFT))
|
|
|
|
arrow.add_tip()
|
|
|
|
return arrow
|
|
|
|
|
|
|
|
|
2015-11-02 19:09:55 -08:00
|
|
|
class XYZAxes(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-11-02 19:09:55 -08:00
|
|
|
"color" : TEAL,
|
|
|
|
"radius" : SPACE_HEIGHT,
|
|
|
|
"tick_frequency" : 1,
|
|
|
|
}
|
|
|
|
def generate_points(self):
|
|
|
|
self.x_axis = NumberLine(
|
|
|
|
numerical_radius = self.radius,
|
|
|
|
tick_frequency = self.tick_frequency
|
|
|
|
)
|
|
|
|
self.y_axis = self.x_axis.copy().rotate(np.pi/2, OUT)
|
|
|
|
self.z_axis = self.x_axis.copy().rotate(np.pi/2, DOWN)
|
|
|
|
self.digest_mobject_attrs()
|
2016-01-15 11:46:45 -08:00
|
|
|
|
2015-11-02 19:09:55 -08:00
|
|
|
|
|
|
|
|
|
|
|
class SpaceGrid(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-11-02 19:09:55 -08:00
|
|
|
"color" : GREEN,
|
|
|
|
"radius" : SPACE_HEIGHT,
|
|
|
|
"unit_to_spatial_length" : 1,
|
|
|
|
"line_frequency" : 2,
|
|
|
|
}
|
|
|
|
def generate_points(self):
|
|
|
|
line_range = range(-int(self.radius), int(self.radius)+1, self.line_frequency)
|
|
|
|
for i in range(3):
|
|
|
|
perm = np.arange(i, i+3) % 3
|
|
|
|
for a, b in it.product(line_range, line_range):
|
|
|
|
start = np.array([a, b, -self.radius])[perm]
|
|
|
|
end = np.array([a, b, self.radius])[perm]
|
|
|
|
self.add_line(start, end)
|
2015-11-09 10:34:00 -08:00
|
|
|
self.pose_at_angle()
|
2015-11-02 19:09:55 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
class NumberLineScene(Scene):
|
|
|
|
def construct(self, **number_line_config):
|
|
|
|
self.number_line = NumberLine(**number_line_config)
|
|
|
|
self.displayed_numbers = self.number_line.default_numbers_to_display()
|
|
|
|
self.number_mobs = self.number_line.get_number_mobjects(*self.displayed_numbers)
|
|
|
|
self.add(self.number_line, *self.number_mobs)
|
|
|
|
|
|
|
|
def zoom_in_on(self, number, zoom_factor, run_time = 2.0):
|
|
|
|
unit_length_to_spatial_width = self.number_line.unit_length_to_spatial_width*zoom_factor
|
|
|
|
radius = SPACE_WIDTH/unit_length_to_spatial_width
|
|
|
|
tick_frequency = 10**(np.floor(np.log10(radius)))
|
|
|
|
left_tick = tick_frequency*(np.ceil((number-radius)/tick_frequency))
|
|
|
|
new_number_line = NumberLine(
|
|
|
|
numerical_radius = radius,
|
|
|
|
unit_length_to_spatial_width = unit_length_to_spatial_width,
|
|
|
|
tick_frequency = tick_frequency,
|
|
|
|
leftmost_tick = left_tick,
|
|
|
|
number_at_center = number
|
|
|
|
)
|
|
|
|
new_displayed_numbers = new_number_line.default_numbers_to_display()
|
|
|
|
new_number_mobs = new_number_line.get_number_mobjects(*new_displayed_numbers)
|
|
|
|
|
|
|
|
transforms = []
|
|
|
|
additional_mobjects = []
|
2015-11-02 19:09:55 -08:00
|
|
|
squished_new_line = new_number_line.copy()
|
2015-10-27 21:00:50 -07:00
|
|
|
squished_new_line.scale(1.0/zoom_factor)
|
|
|
|
squished_new_line.shift(self.number_line.number_to_point(number))
|
|
|
|
squished_new_line.points[:,1] = self.number_line.number_to_point(0)[1]
|
|
|
|
transforms.append(Transform(squished_new_line, new_number_line))
|
|
|
|
for mob, num in zip(new_number_mobs, new_displayed_numbers):
|
|
|
|
point = Point(self.number_line.number_to_point(num))
|
|
|
|
point.shift(new_number_line.get_vertical_number_offset())
|
|
|
|
transforms.append(Transform(point, mob))
|
|
|
|
for mob in self.mobjects:
|
|
|
|
if mob == self.number_line:
|
2015-11-02 19:09:55 -08:00
|
|
|
new_mob = mob.copy()
|
2015-10-27 21:00:50 -07:00
|
|
|
new_mob.shift(-self.number_line.number_to_point(number))
|
|
|
|
new_mob.stretch(zoom_factor, 0)
|
|
|
|
transforms.append(Transform(mob, new_mob))
|
|
|
|
continue
|
|
|
|
mob_center = mob.get_center()
|
|
|
|
number_under_center = self.number_line.point_to_number(mob_center)
|
|
|
|
new_point = new_number_line.number_to_point(number_under_center)
|
|
|
|
new_point += mob_center[1]*UP
|
|
|
|
if mob in self.number_mobs:
|
|
|
|
transforms.append(Transform(mob, Point(new_point)))
|
2015-10-12 19:39:46 -07:00
|
|
|
else:
|
2015-10-27 21:00:50 -07:00
|
|
|
transforms.append(ApplyMethod(mob.shift, new_point - mob_center))
|
|
|
|
additional_mobjects.append(mob)
|
|
|
|
line_to_hide_pixelation = Line(
|
|
|
|
self.number_line.get_left(),
|
|
|
|
self.number_line.get_right(),
|
|
|
|
color = self.number_line.get_color()
|
|
|
|
)
|
|
|
|
self.add(line_to_hide_pixelation)
|
|
|
|
self.play(*transforms, run_time = run_time)
|
|
|
|
self.clear()
|
|
|
|
self.number_line = new_number_line
|
|
|
|
self.displayed_numbers = new_displayed_numbers
|
|
|
|
self.number_mobs = new_number_mobs
|
|
|
|
self.add(self.number_line, *self.number_mobs)
|
|
|
|
self.add(*additional_mobjects)
|
|
|
|
|
|
|
|
def show_multiplication(self, num, **kwargs):
|
2016-01-01 14:51:16 -08:00
|
|
|
if "path_func" not in kwargs:
|
2015-10-27 21:00:50 -07:00
|
|
|
if num > 0:
|
2016-01-01 14:51:16 -08:00
|
|
|
kwargs["path_func"] = straight_path
|
2015-10-27 21:00:50 -07:00
|
|
|
else:
|
2016-01-01 14:51:16 -08:00
|
|
|
kwargs["path_func"] = counterclockwise_path()
|
2015-10-27 21:00:50 -07:00
|
|
|
self.play(*[
|
|
|
|
ApplyMethod(self.number_line.stretch, num, 0, **kwargs)
|
|
|
|
]+[
|
|
|
|
ApplyMethod(mob.shift, (num-1)*mob.get_center()[0]*RIGHT, **kwargs)
|
|
|
|
for mob in self.number_mobs
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-12 19:39:46 -07:00
|
|
|
|
|
|
|
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2015-06-27 04:49:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
|