mirror of
https://github.com/3b1b/manim.git
synced 2025-07-31 05:52:34 +08:00
ShowMultiplication implemented
This commit is contained in:
@ -11,8 +11,8 @@ from helpers import *
|
|||||||
|
|
||||||
|
|
||||||
class NumberLineScene(Scene):
|
class NumberLineScene(Scene):
|
||||||
def construct(self):
|
def construct(self, **number_line_config):
|
||||||
self.number_line = NumberLine()
|
self.number_line = NumberLine(**number_line_config)
|
||||||
self.displayed_numbers = self.number_line.default_numbers_to_display()
|
self.displayed_numbers = self.number_line.default_numbers_to_display()
|
||||||
self.number_mobs = self.number_line.get_number_mobjects(*self.displayed_numbers)
|
self.number_mobs = self.number_line.get_number_mobjects(*self.displayed_numbers)
|
||||||
self.add(self.number_line, *self.number_mobs)
|
self.add(self.number_line, *self.number_mobs)
|
||||||
|
@ -10,7 +10,7 @@ from animation import *
|
|||||||
from mobject import *
|
from mobject import *
|
||||||
from constants import *
|
from constants import *
|
||||||
from region import *
|
from region import *
|
||||||
from scene import Scene
|
from scene import Scene, NumberLineScene
|
||||||
from script_wrapper import command_line_create_scene
|
from script_wrapper import command_line_create_scene
|
||||||
|
|
||||||
MOVIE_PREFIX = "matrix_as_transform_2d/"
|
MOVIE_PREFIX = "matrix_as_transform_2d/"
|
||||||
@ -18,7 +18,70 @@ MOVIE_PREFIX = "matrix_as_transform_2d/"
|
|||||||
def matrix_to_string(matrix):
|
def matrix_to_string(matrix):
|
||||||
return "--".join(["-".join(map(str, row)) for row in matrix])
|
return "--".join(["-".join(map(str, row)) for row in matrix])
|
||||||
|
|
||||||
|
def matrix_mobject(matrix):
|
||||||
|
return text_mobject(
|
||||||
|
"""
|
||||||
|
\\left(
|
||||||
|
\\begin{array}{%s}
|
||||||
|
%d & %d \\\\
|
||||||
|
%d & %d
|
||||||
|
\\end{array}
|
||||||
|
\\right)
|
||||||
|
"""%tuple(["c"*matrix.shape[1]] + list(matrix.flatten())),
|
||||||
|
size = "\\Huge"
|
||||||
|
)
|
||||||
|
|
||||||
|
class ShowMultiplication(NumberLineScene):
|
||||||
|
args_list = [
|
||||||
|
(2, False),
|
||||||
|
(0.5, False),
|
||||||
|
(-3, False),
|
||||||
|
(-3, True),
|
||||||
|
(2, True),
|
||||||
|
]
|
||||||
|
@staticmethod
|
||||||
|
def args_to_string(num, show_original_line):
|
||||||
|
end_string = "WithCopiedOriginalLine" if show_original_line else ""
|
||||||
|
return str(num) + end_string
|
||||||
|
|
||||||
|
def construct(self, num, show_original_line):
|
||||||
|
NumberLineScene.construct(self, density = abs(num)*DEFAULT_POINT_DENSITY_1D)
|
||||||
|
if show_original_line:
|
||||||
|
self.copy_original_line()
|
||||||
|
kwargs = {
|
||||||
|
"run_time" : 2.0,
|
||||||
|
"interpolation_function" : straight_path if num > 0 else counterclockwise_path
|
||||||
|
}
|
||||||
|
self.dither()
|
||||||
|
new_number_line = deepcopy(self.number_line)
|
||||||
|
new_number_line.stretch(num, 0)
|
||||||
|
self.play(
|
||||||
|
Transform(self.number_line, new_number_line, **kwargs),
|
||||||
|
*[
|
||||||
|
ApplyFunction(
|
||||||
|
lambda m : m.do_in_place(m.stretch, 1.0/num, 0).stretch(num, 0),
|
||||||
|
mobject,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
for mobject in self.number_mobs
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.dither()
|
||||||
|
|
||||||
|
|
||||||
|
def copy_original_line(self):
|
||||||
|
copied_line = deepcopy(self.number_line)
|
||||||
|
copied_num_mobs = deepcopy(self.number_mobs)
|
||||||
|
self.play(
|
||||||
|
ApplyFunction(
|
||||||
|
lambda m : m.shift(DOWN).highlight("green"),
|
||||||
|
copied_line
|
||||||
|
), *[
|
||||||
|
ApplyMethod(mob.shift, DOWN)
|
||||||
|
for mob in copied_num_mobs
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.dither()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -58,28 +121,31 @@ class TransformScene2D(Scene):
|
|||||||
)
|
)
|
||||||
self.add(self.x_arrow, self.y_arrow)
|
self.add(self.x_arrow, self.y_arrow)
|
||||||
self.number_plane.filter_out(
|
self.number_plane.filter_out(
|
||||||
lambda (x, y, z) : (0 < x) and (x < 1) and (y < 0.1)
|
lambda (x, y, z) : (0 < x) and (x < 1) and (abs(y) < 0.1)
|
||||||
)
|
)
|
||||||
self.number_plane.filter_out(
|
self.number_plane.filter_out(
|
||||||
lambda (x, y, z) : (0 < y) and (y < 1) and (x < 0.1)
|
lambda (x, y, z) : (0 < y) and (y < 1) and (abs(x) < 0.1)
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
class ShowMatrixTransform(TransformScene2D):
|
class ShowMatrixTransform(TransformScene2D):
|
||||||
args_list = [
|
args_list = [
|
||||||
([[1, 2], [3, 4]], True),
|
([[1, 2], [3, 4]], True, False),
|
||||||
([[1, 3], [-2, 0]], False),
|
([[1, 3], [-2, 0]], False, False),
|
||||||
([[1, 3], [-2, 0]], True),
|
([[1, 3], [-2, 0]], True, True),
|
||||||
([[0, -1], [1, 0]], True),
|
([[0, -1], [1, 0]], True, False),
|
||||||
([[0, -1], [1, 0]], False),
|
([[0, -1], [1, 0]], False, False),
|
||||||
|
([[-1, 0], [0, -1]], True, False),
|
||||||
|
([[-1, 0], [0, -1]], False, False),
|
||||||
]
|
]
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def args_to_string(matrix, with_background):
|
def args_to_string(matrix, with_background, show_matrix):
|
||||||
background_string = "WithBackground" if with_background else "WithoutBackground"
|
background_string = "WithBackground" if with_background else "WithoutBackground"
|
||||||
return matrix_to_string(matrix) + background_string
|
show_string = "ShowingMatrix" if show_matrix else ""
|
||||||
|
return matrix_to_string(matrix) + background_string + show_string
|
||||||
|
|
||||||
def construct(self, matrix, with_background):
|
def construct(self, matrix, with_background, show_matrix):
|
||||||
matrix = np.array(matrix)
|
matrix = np.array(matrix)
|
||||||
number_plane_config = {
|
number_plane_config = {
|
||||||
"density_factor" : self.get_density_factor(matrix)
|
"density_factor" : self.get_density_factor(matrix)
|
||||||
@ -87,13 +153,16 @@ class ShowMatrixTransform(TransformScene2D):
|
|||||||
if with_background:
|
if with_background:
|
||||||
self.add_background()
|
self.add_background()
|
||||||
number_plane_config["use_faded_lines"] = False
|
number_plane_config["use_faded_lines"] = False
|
||||||
self.add_number_plane(**number_plane_config)
|
self.add_number_plane(**number_plane_config)
|
||||||
if with_background:
|
|
||||||
self.add_x_y_arrows()
|
self.add_x_y_arrows()
|
||||||
|
else:
|
||||||
|
self.add_number_plane(**number_plane_config)
|
||||||
|
if show_matrix:
|
||||||
|
self.add(matrix_mobject(matrix).to_corner(UP+LEFT))
|
||||||
def func(mobject):
|
def func(mobject):
|
||||||
mobject.points[:, :2] = np.dot(mobject.points[:, :2], np.transpose(matrix))
|
mobject.points[:, :2] = np.dot(mobject.points[:, :2], np.transpose(matrix))
|
||||||
return mobject
|
return mobject
|
||||||
TransformScene2D.construct(self)
|
|
||||||
self.dither()
|
self.dither()
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"run_time" : 2.0,
|
"run_time" : 2.0,
|
||||||
@ -112,7 +181,7 @@ class ShowMatrixTransform(TransformScene2D):
|
|||||||
Mobject.align_data(arrow, new_arrow)
|
Mobject.align_data(arrow, new_arrow)
|
||||||
arrow.add_tip()
|
arrow.add_tip()
|
||||||
new_arrow.add_tip()
|
new_arrow.add_tip()
|
||||||
anims.append(Transform(arrow, new_arrow))
|
anims.append(Transform(arrow, new_arrow, **kwargs))
|
||||||
self.play(*anims)
|
self.play(*anims)
|
||||||
self.dither()
|
self.dither()
|
||||||
|
|
||||||
@ -145,6 +214,5 @@ class ShowMatrixTransform(TransformScene2D):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
command_line_create_scene(MOVIE_PREFIX)
|
command_line_create_scene(MOVIE_PREFIX)
|
Reference in New Issue
Block a user