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
926f3515bf
commit
e59b3d2ac0
1 changed files with 36 additions and 76 deletions
|
@ -155,110 +155,70 @@ class TextExample(Scene):
|
||||||
|
|
||||||
class TexTransformExample(Scene):
|
class TexTransformExample(Scene):
|
||||||
def construct(self):
|
def construct(self):
|
||||||
to_isolate = ["B", "C", "=", "(", ")"]
|
# Tex to color map
|
||||||
|
t2c = {
|
||||||
|
"A": BLUE,
|
||||||
|
"B": TEAL,
|
||||||
|
"C": GREEN,
|
||||||
|
}
|
||||||
|
# Configuration to pass along to each Tex mobject
|
||||||
|
kw = dict(font_size=72, t2c=t2c)
|
||||||
lines = VGroup(
|
lines = VGroup(
|
||||||
# Passing in muliple arguments to Tex will result
|
Tex("A^2 + B^2 = C^2", **kw),
|
||||||
# in the same expression as if those arguments had
|
Tex("A^2 = C^2 - B^2", **kw),
|
||||||
# been joined together, except that the submobject
|
Tex("A^2 = (C + B)(C - B)", **kw),
|
||||||
# hierarchy of the resulting mobject ensure that the
|
Tex(R"A = \sqrt{(C + B)(C - B)}", **kw),
|
||||||
# Tex mobject has a subject corresponding to
|
|
||||||
# each of these strings. For example, the Tex mobject
|
|
||||||
# below will have 5 subjects, corresponding to the
|
|
||||||
# expressions [A^2, +, B^2, =, C^2]
|
|
||||||
OldTex("A^2", "+", "B^2", "=", "C^2"),
|
|
||||||
# Likewise here
|
|
||||||
OldTex("A^2", "=", "C^2", "-", "B^2"),
|
|
||||||
# Alternatively, you can pass in the keyword argument
|
|
||||||
# "isolate" with a list of strings that should be out as
|
|
||||||
# their own submobject. So the line below is equivalent
|
|
||||||
# to the commented out line below it.
|
|
||||||
OldTex("A^2 = (C + B)(C - B)", isolate=["A^2", *to_isolate]),
|
|
||||||
# OldTex("A^2", "=", "(", "C", "+", "B", ")", "(", "C", "-", "B", ")"),
|
|
||||||
OldTex("A = \\sqrt{(C + B)(C - B)}", isolate=["A", *to_isolate])
|
|
||||||
)
|
)
|
||||||
lines.arrange(DOWN, buff=LARGE_BUFF)
|
lines.arrange(DOWN, buff=LARGE_BUFF)
|
||||||
for line in lines:
|
|
||||||
line.set_color_by_tex_to_color_map({
|
|
||||||
"A": BLUE,
|
|
||||||
"B": TEAL,
|
|
||||||
"C": GREEN,
|
|
||||||
})
|
|
||||||
|
|
||||||
play_kw = {"run_time": 2}
|
|
||||||
self.add(lines[0])
|
self.add(lines[0])
|
||||||
# The animation TransformMatchingTex will line up parts
|
# The animation TransformMatchingStrings will line up parts
|
||||||
# of the source and target which have matching tex strings.
|
# of the source and target which have matching substring strings.
|
||||||
# Here, giving it a little path_arc makes each part sort of
|
# Here, giving it a little path_arc makes each part sort of
|
||||||
# rotate into their final positions, which feels appropriate
|
# rotate into their final positions, which feels appropriate
|
||||||
# for the idea of rearranging an equation
|
# for the idea of rearranging an equation
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingTex(
|
TransformMatchingStrings(
|
||||||
lines[0].copy(), lines[1],
|
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
|
||||||
|
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,
|
||||||
|
# use the key map.
|
||||||
|
key_map={"+": "-"},
|
||||||
path_arc=90 * DEGREES,
|
path_arc=90 * DEGREES,
|
||||||
),
|
),
|
||||||
**play_kw
|
|
||||||
)
|
)
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
# Now, we could try this again on the next line...
|
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingTex(lines[1].copy(), lines[2]),
|
TransformMatchingStrings(
|
||||||
**play_kw
|
|
||||||
)
|
|
||||||
self.wait()
|
|
||||||
# ...and this looks nice enough, but since there's no tex
|
|
||||||
# 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.
|
|
||||||
# If, however, we want the C^2 to go to C, and B^2 to go to B,
|
|
||||||
# we can specify that with a key map.
|
|
||||||
self.play(FadeOut(lines[2]))
|
|
||||||
self.play(
|
|
||||||
TransformMatchingTex(
|
|
||||||
lines[1].copy(), lines[2],
|
lines[1].copy(), lines[2],
|
||||||
key_map={
|
|
||||||
"C^2": "C",
|
|
||||||
"B^2": "B",
|
|
||||||
}
|
|
||||||
),
|
),
|
||||||
**play_kw
|
|
||||||
)
|
)
|
||||||
self.wait()
|
self.wait()
|
||||||
|
|
||||||
# And to finish off, a simple TransformMatchingShapes would work
|
|
||||||
# just fine. But perhaps we want that exponent on A^2 to transform into
|
|
||||||
# the square root symbol. At the moment, lines[2] treats the expression
|
|
||||||
# A^2 as a unit, so we might create a new version of the same line which
|
|
||||||
# separates out just the A. This way, when TransformMatchingTex lines up
|
|
||||||
# all matching parts, the only mismatch will be between the "^2" from
|
|
||||||
# new_line2 and the "\sqrt" from the final line. By passing in,
|
|
||||||
# transform_mismatches=True, it will transform this "^2" part into
|
|
||||||
# the "\sqrt" part.
|
|
||||||
new_line2 = OldTex("A^2 = (C + B)(C - B)", isolate=["A", *to_isolate])
|
|
||||||
new_line2.replace(lines[2])
|
|
||||||
new_line2.match_style(lines[2])
|
|
||||||
|
|
||||||
self.play(
|
self.play(
|
||||||
TransformMatchingTex(
|
TransformMatchingStrings(
|
||||||
new_line2, lines[3],
|
lines[2].copy(), lines[3],
|
||||||
transform_mismatches=True,
|
matched_keys=["="],
|
||||||
|
key_map={"2": R"\sqrt"},
|
||||||
|
path_arc=-30 * DEGREES,
|
||||||
),
|
),
|
||||||
**play_kw
|
|
||||||
)
|
)
|
||||||
self.wait(3)
|
self.wait(2)
|
||||||
self.play(FadeOut(lines, RIGHT))
|
self.play(LaggedStartMap(FadeOut, lines, shift=2 * RIGHT))
|
||||||
|
|
||||||
# Alternatively, if you don't want to think about breaking up
|
# TransformMatchingShapes will try to line up all pieces of a
|
||||||
# the tex strings deliberately, you can TransformMatchingShapes,
|
# source mobject with those of a target, regardless of the
|
||||||
# which will try to line up all pieces of a source mobject with
|
# what Mobject type they are.
|
||||||
# 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 = Text("the morse code", height=1)
|
source = Text("the morse code", height=1)
|
||||||
target = Text("here come dots", height=1)
|
target = Text("here come dots", height=1)
|
||||||
|
|
||||||
self.play(Write(source))
|
self.play(Write(source))
|
||||||
self.wait()
|
self.wait()
|
||||||
kw = {"run_time": 3, "path_arc": PI / 2}
|
kw = dict(run_time=3, path_arc=PI / 2)
|
||||||
self.play(TransformMatchingShapes(source, target, **kw))
|
self.play(TransformMatchingShapes(source, target, **kw))
|
||||||
self.wait()
|
self.wait()
|
||||||
self.play(TransformMatchingShapes(target, source, **kw))
|
self.play(TransformMatchingShapes(target, source, **kw))
|
||||||
|
|
Loading…
Add table
Reference in a new issue