2016-07-12 10:36:04 -07:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from scene import Scene
|
2016-07-13 13:09:18 -07:00
|
|
|
from mobject import Mobject
|
2016-07-12 10:36:04 -07:00
|
|
|
from mobject.vectorized_mobject import VMobject
|
|
|
|
from mobject.tex_mobject import TexMobject, TextMobject
|
2016-07-13 13:09:18 -07:00
|
|
|
from animation.transform import ApplyPointwiseFunction, Transform, \
|
2016-07-15 18:16:06 -07:00
|
|
|
ApplyMethod, FadeOut, ApplyFunction
|
|
|
|
from animation.simple_animations import ShowCreation, Write
|
2016-07-18 11:50:26 -07:00
|
|
|
from topics.number_line import NumberPlane, Axes
|
|
|
|
from topics.geometry import Vector, Line, Circle, Arrow, Dot
|
2016-07-12 10:36:04 -07:00
|
|
|
|
|
|
|
from helpers import *
|
2016-07-18 11:50:26 -07:00
|
|
|
from eola.matrix import Matrix, VECTOR_LABEL_SCALE_VAL, vector_coordinate_label
|
2016-07-12 10:36:04 -07:00
|
|
|
|
2016-07-13 22:03:31 -07:00
|
|
|
|
2016-07-15 18:16:06 -07:00
|
|
|
X_COLOR = GREEN_C
|
|
|
|
Y_COLOR = RED_C
|
2016-07-16 13:34:21 -07:00
|
|
|
Z_COLOR = BLUE_D
|
2016-07-15 18:16:06 -07:00
|
|
|
|
2016-07-13 22:03:31 -07:00
|
|
|
|
2016-07-12 10:36:04 -07:00
|
|
|
class LinearTransformationScene(Scene):
|
|
|
|
CONFIG = {
|
|
|
|
"include_background_plane" : True,
|
|
|
|
"include_foreground_plane" : True,
|
|
|
|
"foreground_plane_kwargs" : {
|
|
|
|
"x_radius" : 2*SPACE_WIDTH,
|
|
|
|
"y_radius" : 2*SPACE_HEIGHT,
|
2016-07-13 13:09:18 -07:00
|
|
|
"secondary_line_ratio" : 0
|
|
|
|
},
|
|
|
|
"background_plane_kwargs" : {
|
|
|
|
"color" : GREY,
|
|
|
|
"secondary_color" : DARK_GREY,
|
|
|
|
"axes_color" : GREY,
|
2016-07-12 10:36:04 -07:00
|
|
|
},
|
|
|
|
"show_coordinates" : False,
|
|
|
|
"show_basis_vectors" : True,
|
2016-07-18 11:50:26 -07:00
|
|
|
"i_hat_color" : X_COLOR,
|
|
|
|
"j_hat_color" : Y_COLOR,
|
2016-07-12 10:36:04 -07:00
|
|
|
}
|
|
|
|
def setup(self):
|
|
|
|
self.background_mobjects = []
|
2016-07-12 15:16:20 -07:00
|
|
|
self.transformable_mobject = []
|
|
|
|
self.moving_vectors = []
|
|
|
|
|
2016-07-12 10:36:04 -07:00
|
|
|
self.background_plane = NumberPlane(
|
|
|
|
**self.background_plane_kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.show_coordinates:
|
|
|
|
self.background_plane.add_coordinates()
|
|
|
|
if self.include_background_plane:
|
2016-07-13 13:09:18 -07:00
|
|
|
self.add_background_mobject(self.background_plane)
|
2016-07-12 10:36:04 -07:00
|
|
|
if self.include_foreground_plane:
|
|
|
|
self.plane = NumberPlane(**self.foreground_plane_kwargs)
|
2016-07-13 13:09:18 -07:00
|
|
|
self.add_transformable_mobject(self.plane)
|
2016-07-12 10:36:04 -07:00
|
|
|
if self.show_basis_vectors:
|
2016-07-12 15:16:20 -07:00
|
|
|
self.add_vector((1, 0), self.i_hat_color)
|
|
|
|
self.add_vector((0, 1), self.j_hat_color)
|
2016-07-12 10:36:04 -07:00
|
|
|
|
2016-07-13 13:09:18 -07:00
|
|
|
def add_background_mobject(self, *mobjects):
|
2016-07-12 10:36:04 -07:00
|
|
|
for mobject in mobjects:
|
|
|
|
if mobject not in self.background_mobjects:
|
|
|
|
self.background_mobjects.append(mobject)
|
|
|
|
self.add(mobject)
|
|
|
|
|
2016-07-13 13:09:18 -07:00
|
|
|
def add_transformable_mobject(self, *mobjects):
|
2016-07-12 10:36:04 -07:00
|
|
|
for mobject in mobjects:
|
2016-07-12 15:16:20 -07:00
|
|
|
if mobject not in self.transformable_mobject:
|
|
|
|
self.transformable_mobject.append(mobject)
|
2016-07-12 10:36:04 -07:00
|
|
|
self.add(mobject)
|
|
|
|
|
2016-07-12 15:16:20 -07:00
|
|
|
def add_vector(self, coords, color = YELLOW):
|
|
|
|
vector = Vector(self.background_plane.num_pair_to_point(coords))
|
|
|
|
vector.highlight(color)
|
|
|
|
self.moving_vectors.append(vector)
|
|
|
|
return vector
|
|
|
|
|
2016-07-12 10:36:04 -07:00
|
|
|
def apply_matrix(self, matrix, **kwargs):
|
2016-07-12 15:16:20 -07:00
|
|
|
matrix = np.array(matrix)
|
|
|
|
if matrix.shape == (2, 2):
|
|
|
|
new_matrix = np.identity(3)
|
|
|
|
new_matrix[:2, :2] = matrix
|
|
|
|
matrix = new_matrix
|
|
|
|
elif matrix.shape != (3, 3):
|
|
|
|
raise "Matrix has bad dimensions"
|
|
|
|
transpose = np.transpose(matrix)
|
|
|
|
|
|
|
|
def func(point):
|
|
|
|
return np.dot(point, transpose)
|
|
|
|
|
|
|
|
new_vectors = [
|
|
|
|
Vector(func(v.get_end()), color = v.get_stroke_color())
|
|
|
|
for v in self.moving_vectors
|
|
|
|
]
|
|
|
|
self.play(
|
|
|
|
ApplyPointwiseFunction(
|
|
|
|
func,
|
|
|
|
VMobject(*self.transformable_mobject),
|
|
|
|
**kwargs
|
|
|
|
),
|
|
|
|
Transform(
|
|
|
|
VMobject(*self.moving_vectors),
|
|
|
|
VMobject(*new_vectors),
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
)
|
2016-07-12 10:36:04 -07:00
|
|
|
|
2016-07-13 13:09:18 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-16 13:34:21 -07:00
|
|
|
class VectorScene(Scene):
|
|
|
|
def add_plane(self, animate = False, **kwargs):
|
|
|
|
plane = NumberPlane(**kwargs)
|
|
|
|
if animate:
|
|
|
|
self.play(ShowCreation(plane, submobject_mode = "lagged_start"))
|
|
|
|
self.add(plane)
|
|
|
|
return plane
|
|
|
|
|
2016-07-18 11:50:26 -07:00
|
|
|
def add_axes(self, animate = False, color = WHITE, **kwargs):
|
|
|
|
axes = Axes(color = color)
|
|
|
|
if animate:
|
|
|
|
self.play(ShowCreation(axes, submobject_mode = "one_at_a_time"))
|
|
|
|
self.add(axes)
|
|
|
|
return axes
|
|
|
|
|
2016-07-16 13:34:21 -07:00
|
|
|
def add_vector(self, vector, animate = True, color = YELLOW):
|
|
|
|
arrow = Vector(vector, color = color)
|
|
|
|
if animate:
|
|
|
|
self.play(ShowCreation(arrow, submobject_mode = "one_at_a_time"))
|
|
|
|
self.add(arrow)
|
|
|
|
return arrow
|
|
|
|
|
2016-07-18 11:50:26 -07:00
|
|
|
def get_basis_vectors(self):
|
|
|
|
i_hat = Vector([1, 0], color = X_COLOR)
|
|
|
|
j_hat = Vector([0, 1], color = Y_COLOR)
|
|
|
|
return i_hat, j_hat
|
|
|
|
|
2016-07-16 13:34:21 -07:00
|
|
|
def label_vector(self, vector, label, animate = True,
|
|
|
|
direction = "left", rotate = False,
|
|
|
|
color = WHITE, add_to_vector = True,
|
|
|
|
buff_factor = 1.5):
|
|
|
|
if len(label) == 1:
|
|
|
|
label = "\\vec{\\textbf{%s}}"%label
|
|
|
|
label = TexMobject(label)
|
|
|
|
label.highlight(color)
|
|
|
|
label.scale(VECTOR_LABEL_SCALE_VAL)
|
|
|
|
if rotate:
|
|
|
|
label.rotate(vector.get_angle())
|
|
|
|
|
|
|
|
vector_vect = vector.get_end() - vector.get_start()
|
|
|
|
if direction is "left":
|
|
|
|
rot_angle = -np.pi/2
|
|
|
|
else:
|
|
|
|
rot_angle = np.pi/2
|
|
|
|
label.shift(-buff_factor*label.get_boundary_point(
|
|
|
|
rotate_vector(vector_vect, rot_angle)
|
|
|
|
))
|
|
|
|
label.shift(vector.get_center())
|
|
|
|
|
|
|
|
if add_to_vector:
|
|
|
|
vector.add(label)
|
|
|
|
if animate:
|
|
|
|
self.play(Write(label, run_time = 1))
|
|
|
|
self.add(label)
|
|
|
|
return label
|
|
|
|
|
2016-07-15 18:16:06 -07:00
|
|
|
def position_x_coordinate(self, x_coord, x_line, vector):
|
2016-07-18 11:50:26 -07:00
|
|
|
x_coord.next_to(x_line, -np.sign(vector[1])*UP)
|
2016-07-15 18:16:06 -07:00
|
|
|
x_coord.highlight(X_COLOR)
|
|
|
|
return x_coord
|
|
|
|
|
|
|
|
def position_y_coordinate(self, y_coord, y_line, vector):
|
2016-07-18 11:50:26 -07:00
|
|
|
y_coord.next_to(y_line, np.sign(vector[0])*RIGHT)
|
2016-07-15 18:16:06 -07:00
|
|
|
y_coord.highlight(Y_COLOR)
|
|
|
|
return y_coord
|
|
|
|
|
|
|
|
def coords_to_vector(self, vector, coords_start = 2*RIGHT+2*UP, cleanup = True):
|
|
|
|
starting_mobjects = list(self.mobjects)
|
|
|
|
array = Matrix(vector)
|
|
|
|
array.shift(coords_start)
|
|
|
|
arrow = Vector(vector)
|
|
|
|
x_line = Line(ORIGIN, vector[0]*RIGHT)
|
|
|
|
y_line = Line(x_line.get_end(), arrow.get_end())
|
|
|
|
x_line.highlight(X_COLOR)
|
|
|
|
y_line.highlight(Y_COLOR)
|
|
|
|
x_coord, y_coord = array.get_mob_matrix().flatten()
|
|
|
|
|
|
|
|
self.play(Write(array, run_time = 1))
|
|
|
|
self.dither()
|
|
|
|
self.play(ApplyFunction(
|
|
|
|
lambda x : self.position_x_coordinate(x, x_line, vector),
|
|
|
|
x_coord
|
|
|
|
))
|
|
|
|
self.play(ShowCreation(x_line))
|
|
|
|
self.play(
|
|
|
|
ApplyFunction(
|
|
|
|
lambda y : self.position_y_coordinate(y, y_line, vector),
|
|
|
|
y_coord
|
|
|
|
),
|
|
|
|
FadeOut(array.get_brackets())
|
|
|
|
)
|
|
|
|
self.play(ShowCreation(y_line))
|
|
|
|
self.play(ShowCreation(arrow, submobject_mode = "one_at_a_time"))
|
|
|
|
self.dither()
|
|
|
|
if cleanup:
|
|
|
|
self.clear()
|
|
|
|
self.add(*starting_mobjects)
|
|
|
|
|
|
|
|
def vector_to_coords(self, vector, integer_labels = True, cleanup = True):
|
|
|
|
starting_mobjects = list(self.mobjects)
|
|
|
|
show_creation = False
|
|
|
|
if isinstance(vector, Arrow):
|
|
|
|
arrow = vector
|
|
|
|
vector = arrow.get_end()[:2]
|
|
|
|
else:
|
|
|
|
arrow = Vector(vector)
|
|
|
|
show_creation = True
|
|
|
|
array = vector_coordinate_label(arrow, integer_labels = integer_labels)
|
|
|
|
x_line = Line(ORIGIN, vector[0]*RIGHT)
|
|
|
|
y_line = Line(x_line.get_end(), arrow.get_end())
|
|
|
|
x_line.highlight(X_COLOR)
|
|
|
|
y_line.highlight(Y_COLOR)
|
|
|
|
x_coord, y_coord = array.get_mob_matrix().flatten()
|
|
|
|
x_coord_start = self.position_x_coordinate(
|
|
|
|
x_coord.copy(), x_line, vector
|
|
|
|
)
|
|
|
|
y_coord_start = self.position_y_coordinate(
|
|
|
|
y_coord.copy(), y_line, vector
|
|
|
|
)
|
|
|
|
brackets = array.get_brackets()
|
|
|
|
|
|
|
|
if show_creation:
|
|
|
|
self.play(ShowCreation(arrow, submobject_mode = "one_at_a_time"))
|
|
|
|
self.play(
|
|
|
|
ShowCreation(x_line),
|
|
|
|
Write(x_coord_start),
|
|
|
|
run_time = 1
|
|
|
|
)
|
|
|
|
self.play(
|
|
|
|
ShowCreation(y_line),
|
|
|
|
Write(y_coord_start),
|
|
|
|
run_time = 1
|
|
|
|
)
|
|
|
|
self.dither()
|
|
|
|
self.play(
|
|
|
|
Transform(x_coord_start, x_coord),
|
|
|
|
Transform(y_coord_start, y_coord),
|
|
|
|
Write(brackets),
|
|
|
|
run_time = 1
|
|
|
|
)
|
|
|
|
self.dither()
|
|
|
|
|
|
|
|
self.remove(x_coord_start, y_coord_start)
|
|
|
|
self.add(x_coord, y_coord)
|
|
|
|
if cleanup:
|
|
|
|
self.clear()
|
|
|
|
self.add(*starting_mobjects)
|
2016-07-18 11:50:26 -07:00
|
|
|
return array, x_line, y_line
|
|
|
|
|
|
|
|
def show_ghost_movement(self, vector):
|
|
|
|
if isinstance(vector, Arrow):
|
|
|
|
vector = vector.get_end() - vector.get_start()
|
|
|
|
elif len(vector) == 2:
|
|
|
|
vector = np.append(np.array(vector), 0.0)
|
|
|
|
x_max = int(SPACE_WIDTH + abs(vector[0]))
|
|
|
|
y_max = int(SPACE_HEIGHT + abs(vector[1]))
|
|
|
|
dots = VMobject(*[
|
|
|
|
Dot(x*RIGHT + y*UP)
|
|
|
|
for x in range(-x_max, x_max)
|
|
|
|
for y in range(-y_max, y_max)
|
|
|
|
])
|
|
|
|
dots.set_fill(BLACK, opacity = 0)
|
|
|
|
dots_halfway = dots.copy().shift(vector/2).set_fill(WHITE, 1)
|
|
|
|
dots_end = dots.copy().shift(vector)
|
|
|
|
|
|
|
|
self.play(Transform(
|
|
|
|
dots, dots_halfway, rate_func = rush_into
|
|
|
|
))
|
|
|
|
self.play(Transform(
|
|
|
|
dots, dots_end, rate_func = rush_from
|
|
|
|
))
|
|
|
|
self.remove(dots)
|
2016-07-15 18:16:06 -07:00
|
|
|
|
|
|
|
|
2016-07-13 13:09:18 -07:00
|
|
|
|
|
|
|
|
2016-07-12 10:36:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|