Added apply_matrix_transformation to LinearTransformationScene

This commit is contained in:
Grant Sanderson 2018-04-06 13:07:02 -07:00
parent bc229f4f29
commit 96d261840d

View file

@ -36,10 +36,12 @@ X_COLOR = GREEN_C
Y_COLOR = RED_C Y_COLOR = RED_C
Z_COLOR = BLUE_D Z_COLOR = BLUE_D
class VectorScene(Scene): class VectorScene(Scene):
CONFIG = { CONFIG = {
"basis_vector_stroke_width": 6 "basis_vector_stroke_width": 6
} }
def add_plane(self, animate=False, **kwargs): def add_plane(self, animate=False, **kwargs):
plane = NumberPlane(**kwargs) plane = NumberPlane(**kwargs)
if animate: if animate:
@ -76,17 +78,18 @@ class VectorScene(Scene):
self.play(Write(coords)) self.play(Write(coords))
return coords return coords
def get_basis_vectors(self): def get_basis_vectors(self, i_hat_color=X_COLOR, j_hat_color=Y_COLOR):
return [ return VGroup(*[
Vector( Vector(
vect, color = color, vect,
color=color,
stroke_width=self.basis_vector_stroke_width stroke_width=self.basis_vector_stroke_width
) )
for vect, color in [ for vect, color in [
([1, 0], X_COLOR), ([1, 0], i_hat_color),
([0, 1], Y_COLOR) ([0, 1], j_hat_color)
]
] ]
])
def get_basis_vector_labels(self, **kwargs): def get_basis_vector_labels(self, **kwargs):
i_hat, j_hat = self.get_basis_vectors() i_hat, j_hat = self.get_basis_vectors()
@ -252,6 +255,7 @@ class VectorScene(Scene):
)) ))
self.remove(dots) self.remove(dots)
class LinearTransformationScene(VectorScene): class LinearTransformationScene(VectorScene):
CONFIG = { CONFIG = {
"include_background_plane": True, "include_background_plane": True,
@ -269,16 +273,18 @@ class LinearTransformationScene(VectorScene):
}, },
"show_coordinates": False, "show_coordinates": False,
"show_basis_vectors": True, "show_basis_vectors": True,
"basis_vector_stroke_width": 6,
"i_hat_color": X_COLOR, "i_hat_color": X_COLOR,
"j_hat_color": Y_COLOR, "j_hat_color": Y_COLOR,
"leave_ghost_vectors": False, "leave_ghost_vectors": False,
"t_matrix": [[3, 0], [1, 2]], "t_matrix": [[3, 0], [1, 2]],
} }
def setup(self): def setup(self):
if hasattr(self, "has_already_setup"): if hasattr(self, "has_already_setup"):
return return
self.has_already_setup = True self.has_already_setup = True
##^This is to not break all the old Scenes # ^This is to not break all the old Scenes
self.background_mobjects = [] self.background_mobjects = []
self.foreground_mobjects = [] self.foreground_mobjects = []
self.transformable_mobjects = [] self.transformable_mobjects = []
@ -299,15 +305,12 @@ class LinearTransformationScene(VectorScene):
self.plane = NumberPlane(**self.foreground_plane_kwargs) self.plane = NumberPlane(**self.foreground_plane_kwargs)
self.add_transformable_mobject(self.plane) self.add_transformable_mobject(self.plane)
if self.show_basis_vectors: if self.show_basis_vectors:
self.i_hat, self.j_hat = [ self.basis_vectors = self.get_basis_vectors(
self.add_vector( i_hat_color=self.i_hat_color,
coords, color, animate = False, stroke_width = 6 j_hat_color=self.j_hat_color,
) )
for coords, color in [ self.moving_vectors += list(self.basis_vectors)
((1, 0), self.i_hat_color), self.i_hat, self.j_hat = self.basis_vectors
((0, 1), self.j_hat_color),
]
]
def add_special_mobjects(self, mob_list, *mobs_to_add): def add_special_mobjects(self, mob_list, *mobs_to_add):
for mobject in mobs_to_add: for mobject in mobs_to_add:
@ -378,7 +381,10 @@ class LinearTransformationScene(VectorScene):
self.title = title self.title = title
return self return self
def get_matrix_transformation(self, transposed_matrix): def get_matrix_transformation(self, matrix):
return self.get_transposed_matrix_transformation(np.array(matrix).T)
def get_transposed_matrix_transformation(self, transposed_matrix):
transposed_matrix = np.array(transposed_matrix) transposed_matrix = np.array(transposed_matrix)
if transposed_matrix.shape == (2, 2): if transposed_matrix.shape == (2, 2):
new_matrix = np.identity(3) new_matrix = np.identity(3)
@ -418,8 +424,11 @@ class LinearTransformationScene(VectorScene):
) )
return self.get_piece_movement(self.transformable_labels) return self.get_piece_movement(self.transformable_labels)
def apply_matrix(self, matrix, **kwargs):
self.apply_transposed_matrix(np.array(matrix).T, **kwargs)
def apply_transposed_matrix(self, transposed_matrix, **kwargs): def apply_transposed_matrix(self, transposed_matrix, **kwargs):
func = self.get_matrix_transformation(transposed_matrix) func = self.get_transposed_matrix_transformation(transposed_matrix)
if "path_arc" not in kwargs: if "path_arc" not in kwargs:
net_rotation = np.mean([ net_rotation = np.mean([
angle_of_vector(func(RIGHT)), angle_of_vector(func(RIGHT)),
@ -451,14 +460,3 @@ class LinearTransformationScene(VectorScene):
for f_mob in self.foreground_mobjects for f_mob in self.foreground_mobjects
] + added_anims ] + added_anims
self.play(*anims, **kwargs) self.play(*anims, **kwargs)