Skip to content

Instantly share code, notes, and snippets.

@uwezi
Created September 9, 2024 18:24
Show Gist options
  • Save uwezi/0bccd47bc7a28db9e1fee00c53ac319b to your computer and use it in GitHub Desktop.
Save uwezi/0bccd47bc7a28db9e1fee00c53ac319b to your computer and use it in GitHub Desktop.
[TransformByGlyphMap] Code by dudewaldo4 on Discord. #manim #mathtex #transform #TransformByGlyphMap
# https://discord.com/channels/581738731934056449/976376734935048223/1161709114154569819
# https://www.youtube.com/watch?v=Bg999qefIic
def ir(a,b): # inclusive range, useful for TransformByGlyphMap
return list(range(a,b+1))
class TransformByGlyphMap(AnimationGroup):
def __init__(self, mobA, mobB, *glyph_map, replace=True, from_copy=False, show_indices=False, **kwargs):
# replace=False does not work properly
if from_copy:
self.mobA = mobA.copy()
self.replace = True
else:
self.mobA = mobA
self.replace = replace
self.mobB = mobB
self.glyph_map = glyph_map
self.show_indices = show_indices
animations = []
mentioned_from_indices = []
mentioned_to_indices = []
for from_indices, to_indices in self.glyph_map:
print(from_indices, to_indices)
if len(from_indices) == 0 and len(to_indices) == 0:
self.show_indices = True
continue
elif len(to_indices) == 0:
animations.append(FadeOut(
VGroup(*[self.mobA[0][i] for i in from_indices]),
shift = self.mobB.get_center()-self.mobA.get_center()
))
elif len(from_indices) == 0:
animations.append(FadeIn(
VGroup(*[self.mobB[0][j] for j in to_indices]),
shift = self.mobB.get_center() - self.mobA.get_center()
))
else:
animations.append(Transform(
VGroup(*[self.mobA[0][i].copy() if i in mentioned_from_indices else self.mobA[0][i] for i in from_indices]),
VGroup(*[self.mobB[0][j] for j in to_indices]),
replace_mobject_with_target_in_scene=self.replace
))
mentioned_from_indices.extend(from_indices)
mentioned_to_indices.extend(to_indices)
print(mentioned_from_indices, mentioned_to_indices)
remaining_from_indices = list(set(range(len(self.mobA[0]))) - set(mentioned_from_indices))
remaining_from_indices.sort()
remaining_to_indices = list(set(range(len(self.mobB[0]))) - set(mentioned_to_indices))
remaining_to_indices.sort()
print(remaining_from_indices, remaining_to_indices)
if len(remaining_from_indices) == len(remaining_to_indices) and not self.show_indices:
for from_index, to_index in zip(remaining_from_indices, remaining_to_indices):
animations.append(Transform(
self.mobA[0][from_index],
self.mobB[0][to_index],
replace_mobject_with_target_in_scene=self.replace
))
super().__init__(*animations, **kwargs)
else:
print(f"From indices: {len(remaining_from_indices)} To indices: {len(remaining_to_indices)}")
print("Showing indices...")
super().__init__(
Create(index_labels(self.mobA[0], color=PINK)),
FadeIn(self.mobB.next_to(self.mobA, DOWN), shift=DOWN),
Create(index_labels(self.mobB[0], color=PINK)),
Wait(5),
lag_ratio=0.5
)
class CisCube(Scene):
def construct(self):
expA = MathTex("\\left( \\cos(x) + i\\sin(x) \\right)^3")
expB = MathTex("\\cos(3x) + i\\sin(3x)")
self.add(expA)
self.play(TransformByGlyphMap(expA,expB,
([16], [4]),
([16], [13]),
([0,15], [])
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment