def setup(self): if hasattr(self, "has_setup"): return self.has_setup = True ##^This is to not break all the old Scenes self.background_mobjects = [] self.foreground_mobjects = [] self.transformable_mobjects = [] self.moving_vectors = [] self.transformable_labels = [] self.moving_mobjects = [] self.t_matrix = np.array(self.t_matrix) self.background_plane = NumberPlane(**self.background_plane_kwargs) if self.show_coordinates: self.background_plane.add_coordinates() if self.include_background_plane: self.add_background_mobject(self.background_plane) if self.include_foreground_plane: 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) for coords, color in [ ((1, 0), self.i_hat_color), ((0, 1), self.j_hat_color), ] ]
def setup(self): if hasattr(self, "has_setup"): return self.has_setup = True ##^This is to not break all the old Scenes self.background_mobjects = [] self.foreground_mobjects = [] self.transformable_mobjects = [] self.moving_vectors = [] self.transformable_labels = [] self.moving_mobjects = [] self.t_matrix = np.array(self.t_matrix) self.background_plane = NumberPlane( **self.background_plane_kwargs ) if self.show_coordinates: self.background_plane.add_coordinates() if self.include_background_plane: self.add_background_mobject(self.background_plane) if self.include_foreground_plane: 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 ) for coords, color in [ ((1, 0), self.i_hat_color), ((0, 1), self.j_hat_color), ] ]
def setup(self): self.background_mobjects = [] self.foreground_mobjects = [] self.transformable_mobjects = [] self.moving_vectors = [] self.transformable_labels = [] self.moving_mobjects = [] self.background_plane = NumberPlane( **self.background_plane_kwargs ) if self.show_coordinates: self.background_plane.add_coordinates() if self.include_background_plane: self.add_background_mobject(self.background_plane) if self.include_foreground_plane: 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 ) for coords, color in [ ((1, 0), self.i_hat_color), ((0, 1), self.j_hat_color), ] ]
def construct(self): def special_rotate(mob): mob.rotate(0.9*np.pi/2, RIGHT) mob.rotate(-np.pi/4, UP) return mob plane = NumberPlane() copies = [ special_rotate(plane.copy().shift(u*n*OUT)) for n in range(1, 3) for u in -1, 1 ] line = Line(4*IN, 4*OUT) self.add(plane) self.play(*[ ApplyFunction(special_rotate, mob, run_time = 3) for mob in plane, line ]) self.dither() for copy in copies: self.play(Transform(plane.copy(), copy)) self.dither()
def construct(self): axes = XYZAxes() axes.highlight(WHITE) plane = NumberPlane() vects = [ Arrow(point, point+(3./27)*(3*x**2-3*y**2)*OUT, color = MAROON_D) for x in range(-4, 5, 2) for y in range(-5, 5, 2) for point in [x*RIGHT + y*UP] ] everybody = Mobject(axes, plane, *vects) self.play(ApplyMethod( everybody.rotate, 0.9*np.pi/2, RIGHT )) self.dither() self.play(ApplyMethod( everybody.rotate, np.pi/2, run_time = 5 ))
def add_plane(self): self.add(NumberPlane().fade())
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
class LinearTransformationScene(VectorScene): CONFIG = { "include_background_plane": True, "include_foreground_plane": True, "foreground_plane_kwargs": { "x_radius": 2 * SPACE_WIDTH, "y_radius": 2 * SPACE_HEIGHT, "secondary_line_ratio": 0 }, "background_plane_kwargs": { "color": GREY, "secondary_color": DARK_GREY, "axes_color": GREY, "stroke_width": 2, }, "show_coordinates": False, "show_basis_vectors": True, "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_setup"): return self.has_setup = True ##^This is to not break all the old Scenes self.background_mobjects = [] self.foreground_mobjects = [] self.transformable_mobjects = [] self.moving_vectors = [] self.transformable_labels = [] self.moving_mobjects = [] self.t_matrix = np.array(self.t_matrix) self.background_plane = NumberPlane(**self.background_plane_kwargs) if self.show_coordinates: self.background_plane.add_coordinates() if self.include_background_plane: self.add_background_mobject(self.background_plane) if self.include_foreground_plane: 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) for coords, color in [ ((1, 0), self.i_hat_color), ((0, 1), self.j_hat_color), ] ] def add_special_mobjects(self, mob_list, *mobs_to_add): for mobject in mobs_to_add: if mobject not in mob_list: mob_list.append(mobject) self.add(mobject) def add_background_mobject(self, *mobjects): self.add_special_mobjects(self.background_mobjects, *mobjects) def add_foreground_mobject(self, *mobjects): self.add_special_mobjects(self.foreground_mobjects, *mobjects) def add_transformable_mobject(self, *mobjects): self.add_special_mobjects(self.transformable_mobjects, *mobjects) def add_moving_mobject(self, mobject, target_mobject=None): mobject.target = target_mobject self.add_special_mobjects(self.moving_mobjects, mobject) def add_unit_square(self, color=YELLOW, opacity=0.3, animate=False): square = Square(color=color, side_length=1) square.shift(-square.get_corner(DOWN + LEFT)) if animate: added_anims = map(Animation, self.moving_vectors) self.play(ShowCreation(square), *added_anims) self.play(square.set_fill, color, opacity, *added_anims) else: square.set_fill(color, opacity) self.add_transformable_mobject(square) self.bring_to_front(*self.moving_vectors) self.square = square return self def add_vector(self, vector, color=YELLOW, **kwargs): vector = VectorScene.add_vector(self, vector, color=color, **kwargs) self.moving_vectors.append(vector) return vector def write_vector_coordinates(self, vector, **kwargs): coords = VectorScene.write_vector_coordinates(self, vector, **kwargs) self.add_foreground_mobject(coords) return coords def add_transformable_label(self, vector, label, new_label=None, **kwargs): label_mob = self.label_vector(vector, label, **kwargs) if new_label: label_mob.target_text = new_label else: label_mob.target_text = "L(%s)" % label_mob.expression label_mob.vector = vector label_mob.kwargs = kwargs if "animate" in label_mob.kwargs: label_mob.kwargs.pop("animate") self.transformable_labels.append(label_mob) return label_mob def add_title(self, title, scale_factor=1.5, animate=False): if not isinstance(title, Mobject): title = TextMobject(title).scale(scale_factor) title.to_edge(UP) title.add_background_rectangle() if animate: self.play(Write(title)) self.add_foreground_mobject(title) self.title = title return self def get_matrix_transformation(self, transposed_matrix): transposed_matrix = np.array(transposed_matrix) if transposed_matrix.shape == (2, 2): new_matrix = np.identity(3) new_matrix[:2, :2] = transposed_matrix transposed_matrix = new_matrix elif transposed_matrix.shape != (3, 3): raise "Matrix has bad dimensions" return lambda point: np.dot(point, transposed_matrix) def get_piece_movement(self, pieces): start = VMobject(*pieces) target = VMobject(*[mob.target for mob in pieces]) if self.leave_ghost_vectors: self.add(start.copy().fade(0.7)) return Transform(start, target, submobject_mode="all_at_once") def get_moving_mobject_movement(self, func): for m in self.moving_mobjects: if m.target is None: m.target = m.copy() target_point = func(m.get_center()) m.target.move_to(target_point) return self.get_piece_movement(self.moving_mobjects) def get_vector_movement(self, func): for v in self.moving_vectors: v.target = Vector(func(v.get_end()), color=v.get_color()) return self.get_piece_movement(self.moving_vectors) def get_transformable_label_movement(self): for l in self.transformable_labels: l.target = self.get_vector_label(l.vector.target, l.target_text, **l.kwargs) return self.get_piece_movement(self.transformable_labels) def apply_transposed_matrix(self, transposed_matrix, **kwargs): func = self.get_matrix_transformation(transposed_matrix) if "path_arc" not in kwargs: net_rotation = np.mean([ angle_of_vector(func(RIGHT)), angle_of_vector(func(UP)) - np.pi / 2 ]) kwargs["path_arc"] = net_rotation self.apply_function(func, **kwargs) def apply_inverse_transpose(self, t_matrix, **kwargs): t_inv = np.linalg.inv(np.array(t_matrix).T).T self.apply_transposed_matrix(t_inv, **kwargs) def apply_nonlinear_transformation(self, function, **kwargs): self.plane.prepare_for_nonlinear_transform() self.apply_function(function, **kwargs) def apply_function(self, function, added_anims=[], **kwargs): if "run_time" not in kwargs: kwargs["run_time"] = 3 anims = [ ApplyPointwiseFunction(function, t_mob) for t_mob in self.transformable_mobjects ] + [ self.get_vector_movement(function), self.get_transformable_label_movement(), self.get_moving_mobject_movement(function), ] + [Animation(f_mob) for f_mob in self.foreground_mobjects] + added_anims self.play(*anims, **kwargs)
class LinearTransformationScene(VectorScene): CONFIG = { "include_background_plane" : True, "include_foreground_plane" : True, "foreground_plane_kwargs" : { "x_radius" : 2*SPACE_WIDTH, "y_radius" : 2*SPACE_HEIGHT, "secondary_line_ratio" : 0 }, "background_plane_kwargs" : { "color" : GREY, "secondary_color" : DARK_GREY, "axes_color" : GREY, "stroke_width" : 2, }, "show_coordinates" : False, "show_basis_vectors" : True, "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_setup"): return self.has_setup = True ##^This is to not break all the old Scenes self.background_mobjects = [] self.foreground_mobjects = [] self.transformable_mobjects = [] self.moving_vectors = [] self.transformable_labels = [] self.moving_mobjects = [] self.t_matrix = np.array(self.t_matrix) self.background_plane = NumberPlane( **self.background_plane_kwargs ) if self.show_coordinates: self.background_plane.add_coordinates() if self.include_background_plane: self.add_background_mobject(self.background_plane) if self.include_foreground_plane: 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 ) for coords, color in [ ((1, 0), self.i_hat_color), ((0, 1), self.j_hat_color), ] ] def add_special_mobjects(self, mob_list, *mobs_to_add): for mobject in mobs_to_add: if mobject not in mob_list: mob_list.append(mobject) self.add(mobject) def add_background_mobject(self, *mobjects): self.add_special_mobjects(self.background_mobjects, *mobjects) def add_foreground_mobject(self, *mobjects): self.add_special_mobjects(self.foreground_mobjects, *mobjects) def add_transformable_mobject(self, *mobjects): self.add_special_mobjects(self.transformable_mobjects, *mobjects) def add_moving_mobject(self, mobject, target_mobject = None): mobject.target = target_mobject self.add_special_mobjects(self.moving_mobjects, mobject) def add_unit_square(self, color = YELLOW, opacity = 0.3, animate = False): square = Square(color = color, side_length = 1) square.shift(-square.get_corner(DOWN+LEFT)) if animate: added_anims = map(Animation, self.moving_vectors) self.play(ShowCreation(square), *added_anims) self.play(square.set_fill, color, opacity, *added_anims) else: square.set_fill(color, opacity) self.add_transformable_mobject(square) self.bring_to_front(*self.moving_vectors) self.square = square return self def add_vector(self, vector, color = YELLOW, **kwargs): vector = VectorScene.add_vector( self, vector, color = color, **kwargs ) self.moving_vectors.append(vector) return vector def write_vector_coordinates(self, vector, **kwargs): coords = VectorScene.write_vector_coordinates(self, vector, **kwargs) self.add_foreground_mobject(coords) return coords def add_transformable_label(self, vector, label, new_label = None, **kwargs): label_mob = self.label_vector(vector, label, **kwargs) if new_label: label_mob.target_text = new_label else: label_mob.target_text = "L(%s)"%label_mob.expression label_mob.vector = vector label_mob.kwargs = kwargs if "animate" in label_mob.kwargs: label_mob.kwargs.pop("animate") self.transformable_labels.append(label_mob) return label_mob def add_title(self, title, scale_factor = 1.5, animate = False): if not isinstance(title, Mobject): title = TextMobject(title).scale(scale_factor) title.to_edge(UP) title.add_background_rectangle() if animate: self.play(Write(title)) self.add_foreground_mobject(title) self.title = title return self def get_matrix_transformation(self, transposed_matrix): transposed_matrix = np.array(transposed_matrix) if transposed_matrix.shape == (2, 2): new_matrix = np.identity(3) new_matrix[:2, :2] = transposed_matrix transposed_matrix = new_matrix elif transposed_matrix.shape != (3, 3): raise "Matrix has bad dimensions" return lambda point: np.dot(point, transposed_matrix) def get_piece_movement(self, pieces): start = VMobject(*pieces) target = VMobject(*[mob.target for mob in pieces]) if self.leave_ghost_vectors: self.add(start.copy().fade(0.7)) return Transform(start, target, submobject_mode = "all_at_once") def get_moving_mobject_movement(self, func): for m in self.moving_mobjects: if m.target is None: m.target = m.copy() target_point = func(m.get_center()) m.target.move_to(target_point) return self.get_piece_movement(self.moving_mobjects) def get_vector_movement(self, func): for v in self.moving_vectors: v.target = Vector(func(v.get_end()), color = v.get_color()) return self.get_piece_movement(self.moving_vectors) def get_transformable_label_movement(self): for l in self.transformable_labels: l.target = self.get_vector_label( l.vector.target, l.target_text, **l.kwargs ) return self.get_piece_movement(self.transformable_labels) def apply_transposed_matrix(self, transposed_matrix, **kwargs): func = self.get_matrix_transformation(transposed_matrix) if "path_arc" not in kwargs: net_rotation = np.mean([ angle_of_vector(func(RIGHT)), angle_of_vector(func(UP))-np.pi/2 ]) kwargs["path_arc"] = net_rotation self.apply_function(func, **kwargs) def apply_inverse_transpose(self, t_matrix, **kwargs): t_inv = np.linalg.inv(np.array(t_matrix).T).T self.apply_transposed_matrix(t_inv, **kwargs) def apply_nonlinear_transformation(self, function, **kwargs): self.plane.prepare_for_nonlinear_transform() self.apply_function(function, **kwargs) def apply_function(self, function, added_anims = [], **kwargs): if "run_time" not in kwargs: kwargs["run_time"] = 3 anims = [ ApplyPointwiseFunction(function, t_mob) for t_mob in self.transformable_mobjects ] + [ self.get_vector_movement(function), self.get_transformable_label_movement(), self.get_moving_mobject_movement(function), ] + [ Animation(f_mob) for f_mob in self.foreground_mobjects ] + added_anims self.play(*anims, **kwargs)