mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Update TexTransformExample
This commit is contained in:
parent
60c03831fb
commit
6259d1c897
1 changed files with 40 additions and 33 deletions
|
@ -112,7 +112,7 @@ class SquareToCircle(Scene):
|
||||||
class TexTransformExample(Scene):
|
class TexTransformExample(Scene):
|
||||||
def construct(self):
|
def construct(self):
|
||||||
kw = {
|
kw = {
|
||||||
"substrings_to_isolate": ["B", "C", "="]
|
"isolate": ["B", "C", "=", "(", ")"]
|
||||||
}
|
}
|
||||||
lines = VGroup(
|
lines = VGroup(
|
||||||
# Surrounding substrings with double braces
|
# Surrounding substrings with double braces
|
||||||
|
@ -123,11 +123,10 @@ class TexTransformExample(Scene):
|
||||||
TexMobject("{{A^2}} + {{B^2}} = {{C^2}}"),
|
TexMobject("{{A^2}} + {{B^2}} = {{C^2}}"),
|
||||||
TexMobject("{{A^2}} = {{C^2}} - {{B^2}}"),
|
TexMobject("{{A^2}} = {{C^2}} - {{B^2}}"),
|
||||||
# Alternatively, you can pass in the keyword argument
|
# Alternatively, you can pass in the keyword argument
|
||||||
# substrings_to_isolate with a list of strings that
|
# isolate with a list of strings that should be out as
|
||||||
# should be broken out as their own submobject. So
|
# their own submobject. So both lines below are equivalent
|
||||||
# both lines below are equivalent to what you'd get
|
# to what you'd get by wrapping every instance of "B", "C"
|
||||||
# by wrapping every instance of "B", "C" and "=" with
|
# "=", "(" and ")" with double braces
|
||||||
# double braces
|
|
||||||
TexMobject("{{A^2}} = (C + B)(C - B)", **kw),
|
TexMobject("{{A^2}} = (C + B)(C - B)", **kw),
|
||||||
TexMobject("A = \\sqrt{(C + B)(C - B)}", **kw)
|
TexMobject("A = \\sqrt{(C + B)(C - B)}", **kw)
|
||||||
)
|
)
|
||||||
|
@ -139,6 +138,7 @@ class TexTransformExample(Scene):
|
||||||
"C": GREEN,
|
"C": GREEN,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
play_kw = {"run_time": 2}
|
||||||
self.add(lines[0])
|
self.add(lines[0])
|
||||||
# The animation TransformMatchingTex will line up parts
|
# The animation TransformMatchingTex will line up parts
|
||||||
# of the source and target which have matching tex strings.
|
# of the source and target which have matching tex strings.
|
||||||
|
@ -150,58 +150,65 @@ class TexTransformExample(Scene):
|
||||||
lines[0].copy(), lines[1],
|
lines[0].copy(), lines[1],
|
||||||
path_arc=90 * DEGREES,
|
path_arc=90 * DEGREES,
|
||||||
),
|
),
|
||||||
|
**play_kw
|
||||||
)
|
)
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
# Now, we could try this again on the next line...
|
# Now, we could try this again on the next line...
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingTex(lines[1].copy(), lines[2]),
|
TransformMatchingTex(lines[1].copy(), lines[2]),
|
||||||
|
**play_kw
|
||||||
)
|
)
|
||||||
self.wait()
|
self.wait()
|
||||||
# ...and this looks nice enough, but since there's no tex
|
# ...and this looks nice enough, but since there's no tex
|
||||||
# in lines[2] which matches "C^2" or "B^2", those terms fade
|
# in lines[2] which matches "C^2" or "B^2", those terms fade
|
||||||
# out to nothing while the C and B terms fade in from nothing.
|
# out to nothing while the C and B terms fade in from nothing.
|
||||||
# If, however, we want the C to go to C, and B to go to B, but
|
# If, however, we want the C^2 to go to C, and B^2 to go to B,
|
||||||
# we don't want to think about breaking up the tex string
|
# we can specify that with a key map.
|
||||||
# differently, we could instead try TransformMatchingShapes,
|
|
||||||
# which will line up parts of the source and target which
|
|
||||||
# have matching shapes, regardless of where they fall in the
|
|
||||||
# mobject family heirarchies.
|
|
||||||
self.play(FadeOut(lines[2]))
|
|
||||||
self.play(
|
|
||||||
TransformMatchingShapes(lines[1].copy(), lines[2]),
|
|
||||||
)
|
|
||||||
# That's almost what we want, but if you were finicky you
|
|
||||||
# might complain that all the exponents from lines[1] got
|
|
||||||
# to the 2 in A^2, since that's the only part of lines[2]
|
|
||||||
# which matches the shape of a 2. In this case, one option
|
|
||||||
# would be to use TransformMatchingTex on the left-hand-side,
|
|
||||||
# but TransformMatchingShapes on the right-hand-side
|
|
||||||
eq_index = lines[1].index_of_part_by_tex("=")
|
|
||||||
self.play(FadeOut(lines[2]))
|
self.play(FadeOut(lines[2]))
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingTex(
|
TransformMatchingTex(
|
||||||
lines[1][:eq_index].copy(),
|
lines[1].copy(), lines[2],
|
||||||
lines[2][:eq_index],
|
key_map={
|
||||||
),
|
"C^2": "C",
|
||||||
TransformMatchingShapes(
|
"B^2": "B",
|
||||||
lines[1][eq_index:].copy(),
|
}
|
||||||
lines[2][eq_index:],
|
|
||||||
),
|
),
|
||||||
|
**play_kw
|
||||||
)
|
)
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
# And to finish off, a simple TransformMatchingShapes will do,
|
# And to finish off, a simple TransformMatchingShapes will do,
|
||||||
# though maybe we really want that exponent from A^2 to turn
|
# though maybe we really want that exponent from A^2 to turn
|
||||||
# into the square root, so we set fade_transform_mismatches to
|
# into the square root, we could use a key_map again. Or,
|
||||||
# True so that parts with mis-matching shapes transform into
|
# if we set fade_transform_mismatches to True, then it will
|
||||||
# each other.
|
# line up mismatching submobjects and have them transform
|
||||||
|
# into each other
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingShapes(
|
TransformMatchingTex(
|
||||||
lines[2].copy(), lines[3],
|
lines[2].copy(), lines[3],
|
||||||
fade_transform_mismatches=True,
|
fade_transform_mismatches=True,
|
||||||
),
|
),
|
||||||
|
**play_kw
|
||||||
)
|
)
|
||||||
|
self.wait(3)
|
||||||
|
self.play(FadeOut(lines, RIGHT))
|
||||||
|
|
||||||
|
# Alternatively, if you don't want to think about breaking up
|
||||||
|
# the tex strings deliberately, you can TransformMatchingShapes,
|
||||||
|
# which will try to line up all pieces of a source mobject with
|
||||||
|
# those of a target, regardless of the submobject hierarchy in
|
||||||
|
# each one, according to whether those pieces have the same
|
||||||
|
# shape (as best it can).
|
||||||
|
source = TextMobject("the morse code")
|
||||||
|
target = TextMobject("here come dots")
|
||||||
|
|
||||||
|
self.play(Write(source))
|
||||||
|
self.wait()
|
||||||
|
kw = {"run_time": 3, "path_arc": PI / 2}
|
||||||
|
self.play(TransformMatchingShapes(source, target, **kw))
|
||||||
|
self.wait()
|
||||||
|
self.play(TransformMatchingShapes(target, source, **kw))
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue