From e3b95276fa5835a168d649cbb5d2a6209718b640 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Fri, 3 Feb 2023 17:48:51 -0800 Subject: [PATCH] Update TexTransformExample, and pull out TexIndexing --- example_scenes.py | 84 +++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/example_scenes.py b/example_scenes.py index 9aa3e108..127e438d 100644 --- a/example_scenes.py +++ b/example_scenes.py @@ -174,16 +174,17 @@ class TexTransformExample(Scene): self.add(lines[0]) # The animation TransformMatchingStrings will line up parts # of the source and target which have matching substring strings. - # Here, giving it a little path_arc makes each part sort of - # rotate into their final positions, which feels appropriate - # for the idea of rearranging an equation + # Here, giving it a little path_arc makes each part rotate into + # their final positions, which feels appropriate for the idea of + # rearranging an equation self.play( TransformMatchingStrings( lines[0].copy(), lines[1], # matched_keys specifies which substring should # line up. If it's not specified, the animation - # will try its best, but may not quite give the - # intended effect + # will align the longest matching substrings. + # In this case, the substring "^2 = C^2" would + # trip it up matched_keys=["A^2", "B^2", "C^2"], # When you want a substring from the source # to go to a non-equal substring from the target, @@ -206,25 +207,57 @@ class TexTransformExample(Scene): ), ) self.wait(2) - - # You can also index into Tex mobject (or other StringMobjects) - # by substrings and regular expressions - top_equation = lines[0] - low_equation = lines[3] - - self.play(LaggedStartMap(FlashAround, low_equation["C"], lag_ratio=0.5)) - self.play(LaggedStartMap(FlashAround, low_equation["B"], lag_ratio=0.5)) - self.play(LaggedStartMap(FlashAround, top_equation[re.compile(r"\w\^2")])) - self.play(Indicate(low_equation[R"\sqrt"])) - self.wait() self.play(LaggedStartMap(FadeOut, lines, shift=2 * RIGHT)) + # TransformMatchingShapes will try to line up all pieces of a + # source mobject with those of a target, regardless of the + # what Mobject type they are. + source = Text("the morse code", height=1) + target = Text("here come dots", height=1) + saved_source = source.copy() + + self.play(Write(source)) + self.wait() + kw = dict(run_time=3, path_arc=PI / 2) + self.play(TransformMatchingShapes(source, target, **kw)) + self.wait() + self.play(TransformMatchingShapes(target, saved_source, **kw)) + self.wait() + + +class TexIndexing(Scene): + def construct(self): + # You can index into Tex mobject (or other StringMobjects) by substrings + equation = Tex(R"e^{\pi i} = -1", font_size=144) + + self.add(equation) + self.play(FlashAround(equation["e"])) + self.wait() + self.play(Indicate(equation[R"\pi"])) + self.wait() + self.play(TransformFromCopy( + equation[R"e^{\pi i}"].copy().set_opacity(0.5), + equation["-1"], + path_arc=-PI / 2, + run_time=3 + )) + self.play(FadeOut(equation)) + + # Or regular expressions + equation = Tex("A^2 + B^2 = C^2", font_size=144) + + self.play(Write(equation)) + for part in equation[re.compile(r"\w\^2")]: + self.play(FlashAround(part)) + self.wait() + self.play(FadeOut(equation)) + # Indexing by substrings like this may not work when # the order in which Latex draws symbols does not match # the order in which they show up in the string. # For example, here the infinity is drawn before the sigma # so we don't get the desired behavior. - equation = Tex(R"\sum_{n = 1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}") + equation = Tex(R"\sum_{n = 1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}", font_size=72) self.play(FadeIn(equation)) self.play(equation[R"\infty"].animate.set_color(RED)) # Doesn't hit the infinity self.wait() @@ -236,27 +269,14 @@ class TexTransformExample(Scene): equation = Tex( R"\sum_{n = 1}^\infty {1 \over n^2} = {\pi^2 \over 6}", # Explicitly mark "\infty" as a substring you might want to access - isolate=[R"\infty"] + isolate=[R"\infty"], + font_size=72 ) self.play(FadeIn(equation)) self.play(equation[R"\infty"].animate.set_color(RED)) # Got it! self.wait() self.play(FadeOut(equation)) - # TransformMatchingShapes will try to line up all pieces of a - # source mobject with those of a target, regardless of the - # what Mobject type they are. - source = Text("the morse code", height=1) - target = Text("here come dots", height=1) - - self.play(Write(source)) - self.wait() - kw = dict(run_time=3, path_arc=PI / 2) - self.play(TransformMatchingShapes(source, target, **kw)) - self.wait() - self.play(TransformMatchingShapes(target, source, **kw)) - self.wait() - class UpdatersExample(Scene): def construct(self):