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