A community-maintained Python framework for creating mathematical animations.
TransitionFromPathsToBoundaries
Grant Sandersonad330cc95b7b
diff --git a/active_projects/WindingNumber_G.py b/active_projects/WindingNumber_G.pyindex e6e093fd8..73ed02b96 100644--- a/active_projects/WindingNumber_G.py+++ b/active_projects/WindingNumber_G.py@@ -2127,97 +2127,217 @@ class PathContainingZero(InputOutputScene, PiCreatureScene):self.play(morty.change, "hooray")self.wait(3)-class BackToEquationSolving(AltTeacherStudentsScene):- def construct(self):- self.teacher_says(- "Back to solving \\\\ equations"- )- self.change_all_student_modes("hooray")- self.play(*[- ApplyMethod(pi.look_at, self.screen)- for pi in self.pi_creatures- ])- self.wait(3)--class SearchSpacePerimeterVsArea(EquationSolver2d):+class TransitionFromPathsToBoundaries(ColorMappedObjectsScene):CONFIG = {"func" : plane_func_by_wind_spec(- (-3, -1.3, 2), (0.1, 0.2, 1), (2.8, -2, 1)- ),- "num_iterations" : 15,- "display_in_parallel" : False,- "use_fancy_lines" : True,+ (-2, 0, 2), (2, 0, 1)+ )}def construct(self):- self.force_skipping()- EquationSolver2d.construct(self)- self.revert_to_original_skipping_status()+ ColorMappedObjectsScene.construct(self)- all_parts = VGroup(*self.get_mobjects())- path_parts = VGroup()- non_path_parts = VGroup()- for part in all_parts:- if part.get_background_image_file() is not None:- path_parts.add(part)- else:- non_path_parts.add(part)- path_parts.save_state()- path_parts.generate_target()- for path_target in path_parts.target:- if isinstance(path_target, Line):- path_target.rotate(-path_target.get_angle())- path_parts.target.arrange_submobjects(DOWN, buff = MED_SMALL_BUFF)- alt_path_parts = path_parts.copy()- size = lambda m : m.get_height() + m.get_width()- alt_path_parts.submobjects.sort(- lambda m1, m2 : -cmp(size(m1), size(m2))- )+ #Setup paths+ squares, joint_rect = self.get_squares_and_joint_rect()+ left_square, right_square = squares- full_rect = SurroundingRectangle(- path_parts,- stroke_width = 0,- fill_color = WHITE,- fill_opacity = 1,- background_image_file = path_parts[0].background_image_file+ path1, path2 = paths = VGroup(*[+ Line(square.get_corner(UP+LEFT), square.get_corner(UP+RIGHT))+ for square in squares+ ])+ joint_path = Line(path1.get_start(), path2.get_end())++ for mob in it.chain(paths, [joint_path]):+ mob.set_stroke(WHITE, 4)+ mob.color_using_background_image(self.background_image_file)++ dot = self.get_dot_and_add_continual_animations()++ #Setup path braces+ for mob, tex in (path1, "x"), (path2, "y"), (joint_path, "x+y"):+ mob.brace = Brace(mob, DOWN)+ label = TextMobject("Winding =", "$%s$"%tex)+ label.next_to(mob.brace, DOWN)+ mob.brace.add(label)++ #Setup region labels++ for square, tex in (left_square, "x"), (right_square, "y"), (joint_rect, "x+y \\, ?"):+ square.label = TextMobject("Winding = ", "$%s$"%tex)+ square.label.move_to(square)++ #Add paths+ self.position_dot(path1.get_start())+ for path in path1, path2:+ self.position_dot(path.get_start())+ self.play(+ MoveAlongPath(dot, path.copy()),+ ShowCreation(path),+ run_time = 2+ )+ self.play(GrowFromCenter(path.brace))+ self.wait()+ self.position_dot(joint_path.get_start())+ self.play(+ MoveAlongPath(dot, joint_path, run_time = 3),+ FadeOut(VGroup(path1.brace, path2.brace)),+ FadeIn(joint_path.brace),)- full_rect.save_state()- full_rect.stretch(0, 1, about_edge = UP)+ self.wait()+ #Add regionsself.play(- FadeOut(non_path_parts),- path_parts.set_stroke, {"width" : 1},+ FadeOut(paths),+ FadeOut(joint_path.brace),+ dot.move_to, path1.get_start()+ )+ for square in squares:+ self.position_dot(square.points[0])+ kwargs = {+ "run_time" : 4,+ "rate_func" : bezier([0, 0, 1, 1]),+ }+ self.play(+ MoveAlongPath(dot, square.copy(), **kwargs),+ ShowCreation(square, **kwargs),+ Write(square.label, run_time = 2),+ )+ self.wait()+ self.play(+ dot.move_to, joint_rect.points[0],+ FadeOut(squares),+ FadeIn(joint_rect),)- self.remove(all_parts)- for x in range(2):- alt_path_parts.save_state()- self.play(LaggedStart(- FadeIn, alt_path_parts,- rate_func = there_and_back,- lag_ratio = 0.3,- run_time = 3,- remover = True- ))- alt_path_parts.restore()+ self.position_dot(joint_rect.points[0])self.play(- full_rect.restore,- run_time = 2,+ Transform(left_square.label[0], joint_rect.label[0]),+ Transform(+ left_square.label[1], joint_rect.label[1][0],+ path_arc = TAU/6+ ),+ FadeIn(joint_rect.label[1][1]),+ FadeIn(joint_rect.label[1][3]),+ FadeOut(right_square.label[0]),+ Transform(+ right_square.label[1], joint_rect.label[1][2],+ path_arc = TAU/6+ ),+ MoveAlongPath(+ dot, joint_rect,+ run_time = 6,+ rate_func = bezier([0, 0, 1, 1])+ ))self.wait()- self.play(FadeOut(full_rect))- self.wait()++ ###++ def get_squares_and_joint_rect(self):+ squares = VGroup(*[+ Square(side_length = 4).next_to(ORIGIN, vect, buff = 0)+ for vect in LEFT, RIGHT+ ])+ joint_rect = SurroundingRectangle(squares, buff = 0)+ for mob in it.chain(squares, [joint_rect]):+ mob.set_stroke(WHITE, 4)+ mob.color_using_background_image(self.background_image_file)+ return squares, joint_rect++ def get_dot_and_add_continual_animations(self):+ #Define important functions for updates+ get_output = lambda : self.func(tuple(dot.get_center()[:2]))+ get_output_color = lambda : rgba_to_color(point_to_rgba(get_output()))+ get_output_rev = lambda : -point_to_rev(get_output())+ self.get_output_rev = get_output_rev++ self.start_rev = 0+ self.curr_winding = 0+ def get_total_winding(dt = 0):+ rev = (get_output_rev() - self.start_rev)%1+ possible_windings = [+ np.floor(self.curr_winding)+k+rev+ for k in -1, 0, 1+ ]+ i = np.argmin([abs(pw - self.curr_winding) for pw in possible_windings])+ self.curr_winding = possible_windings[i]+ return self.curr_winding+++ #Setup dot, arrow and label+ dot = self.dot = Dot(radius = 0.1)+ dot.set_stroke(WHITE, 1)+ update_dot_color = ContinualUpdateFromFunc(+ dot, lambda d : d.set_fill(get_output_color())+ )++ label = DecimalNumber(0, num_decimal_points = 1)+ label_upadte = ContinualChangingDecimal(+ label, get_total_winding,+ position_update_func = lambda l : l.next_to(dot, UP+LEFT, SMALL_BUFF)+ )++ arrow_length = 0.75+ arrow = Vector(arrow_length*RIGHT)+ arrow.set_stroke(WHITE, 1)+ def arrow_update_func(arrow):+ arrow.set_fill(get_output_color(), 1)+ arrow.rotate(-TAU*get_output_rev() - arrow.get_angle())+ arrow.scale(arrow_length/arrow.get_length())+ arrow.shift(dot.get_center() - arrow.get_start())+ return arrow+ update_arrow = ContinualUpdateFromFunc(arrow, arrow_update_func)++ self.add(update_arrow, update_dot_color, label_upadte)+ return dot++ def position_dot(self, point):+ self.dot.move_to(point)+ self.start_rev = self.get_output_rev()+ self.curr_winding = 0++class BreakDownLoopWithNonzeroWinding(TransitionFromPathsToBoundaries):+ def construct(self):+ zero_point = 2*LEFT++ squares, joint_rect = self.get_squares_and_joint_rect()+ left_square, right_square = squares+ VGroup(squares, joint_rect).shift(SMALL_BUFF*UP)++ dot = self.get_dot_and_add_continual_animations()++ for rect in (left_square, "x"), (right_square, "y"), (joint_rect, "3"):+ pass++++class BackToEquationSolving(AltTeacherStudentsScene):+ def construct(self):+ self.teacher_says(+ "Back to solving \\\\ equations"+ )+ self.change_all_student_modes("hooray")+ self.play(*[+ ApplyMethod(pi.look_at, self.screen)+ for pi in self.pi_creatures+ ])+ self.wait(3)class MonomialTerm(PathContainingZero):CONFIG = {"non_renormalized_func" : plane_func_from_complex_func(lambda z : z**5),+ "full_func_label" : "f(x) = x^5","func_label" : "x^5","loop_radius" : 1.1,"label_buff" : 0.3,+ "label_move_to_corner" : ORIGIN,+ "should_end_with_rescaling" : True,}def construct(self):self.setup_planes()self.relabel_planes()self.add_function_label()self.show_winding()+ if self.should_end_with_rescaling:+ self.rescale_output_plane()def relabel_planes(self):for plane in self.input_plane, self.output_plane:@@ -2237,6 +2357,7 @@ class MonomialTerm(PathContainingZero):point = plane.coords_to_point(x, 0)label.next_to(point, DOWN, MED_SMALL_BUFF)plane.add(label)+ self.add_foreground_mobject(label)tick = Line(SMALL_BUFF*DOWN, SMALL_BUFF*UP)tick.move_to(point)plane.add(tick)@@ -2248,13 +2369,15 @@ class MonomialTerm(PathContainingZero):point = plane.coords_to_point(0, y)label.next_to(point, LEFT, MED_SMALL_BUFF)plane.add(label)+ self.add_foreground_mobject(label)tick = Line(SMALL_BUFF*LEFT, SMALL_BUFF*RIGHT)tick.move_to(point)plane.add(tick)+ self.add(self.input_plane, self.output_plane)def add_function_label(self):- label = TexMobject("f(x) = %s"%self.func_label)- label.add_background_rectangle(opacity = 1)+ label = TexMobject(self.full_func_label)+ label.add_background_rectangle(opacity = 1, buff = SMALL_BUFF)arrow = Arrow(2*LEFT, 2*RIGHT, path_arc = -TAU/3,use_rectangular_stem = False@@ -2290,6 +2413,7 @@ class MonomialTerm(PathContainingZero):return UpdateFromFunc(label, lambda m : m.move_to((1+buff)*point_func() - buff*origin,+ self.label_move_to_corner))x = TexMobject("x")@@ -2310,15 +2434,21 @@ class MonomialTerm(PathContainingZero):update_out_dot,update_x,update_fx,- Succession(- morty.change, "pondering",- Blink(morty),- Animation(morty, run_time = 4),- Blink(morty),- Animation(morty, run_time = 4),- Blink(morty),- )+ ApplyMethod(morty.change, "pondering", out_dot),+ )+ self.play(+ FadeOut(VGroup(dot, out_dot, x, fx)))+ self.loop = loop+ self.out_loop = out_loop++ def rescale_output_plane(self):+ output_stuff = VGroup(self.output_plane, self.output_coloring)+ self.play(*map(FadeOut, [self.loop, self.out_loop]))+ self.play(+ output_stuff.scale, 3.0/50, run_time = 2+ )+ self.wait()###@@ -2328,17 +2458,148 @@ class MonomialTerm(PathContainingZero):class PolynomialTerms(MonomialTerm):CONFIG = {"non_renormalized_func" : plane_func_from_complex_func(lambda z : z**5 - z - 1),- "func_label" : "x^5 - x - 1",+ "full_func_label" : "f(x) = x^5 - x - 1",+ "func_label" : "x^5 + \\cdots","loop_radius" : 2.0,- "label_buff" : 0.3,+ "label_buff" : 0.15,+ "label_move_to_corner" : DOWN+LEFT,+ "should_end_with_rescaling" : False,}+ def construct(self):+ self.pi_creature.change("pondering", VectorizedPoint(ORIGIN))+ MonomialTerm.construct(self)+ self.cinch_loop()+ # self.sweep_through_loop_interior()def relabel_planes(self):self.output_plane.x_radius = 50self.output_plane.y_radius = 50- self.output_plane.numbers_to_show = range(-45, 50, 10)+ self.output_plane.numbers_to_show = range(-45, 50, 15)MonomialTerm.relabel_planes(self)+ def sweep_through_loop_interior(self):+ loop = self.loop+ morty = self.pi_creature++ line, line_target = [+ Line(+ loop.get_left(), loop.get_right(),+ path_arc = u*TAU/2,+ n_arc_anchors = 40,+ background_image_file = self.input_coloring.background_image_file ,+ stroke_width = 4,+ )+ for u in -1, 1+ ]+ out_line = line.copy()+ update_out_line = UpdateFromFunc(+ out_line,+ lambda m : m.set_points(line.points).apply_function(self.point_function),+ )++ self.play(+ Transform(+ line, line_target,+ run_time = 10,+ rate_func = there_and_back+ ),+ update_out_line,+ morty.change, "hooray"+ )+ self.wait()++ def cinch_loop(self):+ loop = self.loop+ out_loop = self.out_loop+ morty = self.pi_creature++ update_out_loop = UpdateFromFunc(+ out_loop,+ lambda m : m.set_points(loop.points).apply_function(self.point_function)+ )++ self.add(+ loop.copy().set_stroke(width = 1),+ out_loop.copy().set_stroke(width = 1),+ )+ self.play(+ ApplyMethod(+ loop.scale, 0, {"about_point" : self.input_plane.coords_to_point(0.2, 1)},+ run_time = 12,+ rate_func = bezier([0, 0, 1, 1])+ ),+ update_out_loop,+ morty.change, "hooray"+ )+ self.wait()++class SearchSpacePerimeterVsArea(EquationSolver2d):+ CONFIG = {+ "func" : plane_func_by_wind_spec(+ (-3, -1.3, 2), (0.1, 0.2, 1), (2.8, -2, 1)+ ),+ "num_iterations" : 15,+ "display_in_parallel" : False,+ "use_fancy_lines" : True,+ }+ def construct(self):+ self.force_skipping()+ EquationSolver2d.construct(self)+ self.revert_to_original_skipping_status()++ all_parts = VGroup(*self.get_mobjects())+ path_parts = VGroup()+ non_path_parts = VGroup()+ for part in all_parts:+ if part.get_background_image_file() is not None:+ path_parts.add(part)+ else:+ non_path_parts.add(part)+ path_parts.save_state()+ path_parts.generate_target()+ for path_target in path_parts.target:+ if isinstance(path_target, Line):+ path_target.rotate(-path_target.get_angle())+ path_parts.target.arrange_submobjects(DOWN, buff = MED_SMALL_BUFF)+ alt_path_parts = path_parts.copy()+ size = lambda m : m.get_height() + m.get_width()+ alt_path_parts.submobjects.sort(+ lambda m1, m2 : -cmp(size(m1), size(m2))+ )++ full_rect = SurroundingRectangle(+ path_parts,+ stroke_width = 0,+ fill_color = WHITE,+ fill_opacity = 1,+ background_image_file = path_parts[0].background_image_file+ )+ full_rect.save_state()+ full_rect.stretch(0, 1, about_edge = UP)++ self.play(+ FadeOut(non_path_parts),+ path_parts.set_stroke, {"width" : 1},+ )+ self.remove(all_parts)+ for x in range(2):+ alt_path_parts.save_state()+ self.play(LaggedStart(+ FadeIn, alt_path_parts,+ rate_func = there_and_back,+ lag_ratio = 0.3,+ run_time = 3,+ remover = True+ ))+ alt_path_parts.restore()+ self.play(+ full_rect.restore,+ run_time = 2,+ )+ self.wait()+ self.play(FadeOut(full_rect))+ self.wait()+class EndingCredits(Scene):def construct(self):text = TextMobject(