Created
March 27, 2022 14:35
-
-
Save behackl/84e3fecc89342d375fb3ae1c806f6d56 to your computer and use it in GitHub Desktop.
Code for manimation of all Dyck paths with 14 steps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from manim import * | |
from combpyter import DyckPaths, DyckPath | |
def draw_path(path: DyckPath, plane: NumberPlane | None = None) -> VGroup: | |
if plane is None: | |
plane = NumberPlane(x_range=[0, len(path), 1], y_range=[0, path.height(), 1]) | |
height_profile = path.height_profile() | |
mobs = [] | |
for ind in range(len(height_profile) - 1): | |
mobs.extend([ | |
Dot(plane.c2p(ind, height_profile[ind])), | |
Line( | |
plane.c2p(ind, height_profile[ind]), | |
plane.c2p(ind + 1, height_profile[ind + 1]), | |
buff=0, | |
) | |
]) | |
mobs.append(Dot(plane.c2p(len(path), 0))) | |
path_mobject = VGroup(*mobs) | |
return VGroup(plane, path_mobject) | |
class DyckPathIterator(Scene): | |
def construct(self): | |
self.camera.background_color = "#455D3E" | |
title = VGroup( | |
Text("Combinatorial Generation:", color=WHITE, font="Gill Sans"), | |
Text("Dyck paths", color=WHITE, font="Gill Sans") | |
).arrange(DOWN).scale_to_fit_width(config.frame_width - 1) | |
subtitle = Tex(r"$C_7 = \frac{1}{7+1} \binom{2\cdot 7}{7} = 429$ Dyck paths with 14 steps", color=WHITE).scale_to_fit_width(config.frame_width - 2) | |
VGroup(title, subtitle).arrange(DOWN, buff=2) | |
self.play(Write(title)) | |
self.wait(1) | |
self.play(Write(subtitle)) | |
self.wait(1) | |
N = 7 | |
paths = list(DyckPaths(N)) | |
plane = NumberPlane(x_range=[0, 2*N, 1], y_range=[0, N, 1]) | |
plane.scale_to_fit_width(config.frame_width - 1) | |
all_path_mobjects = [] | |
for path in paths: | |
plane, path_mobject = draw_path(path, plane) | |
for subm in path_mobject.submobjects: | |
if isinstance(subm, Dot): | |
path_mobject.remove(subm) | |
all_path_mobjects.append(path_mobject) | |
all_path_mobjects = VGroup(*all_path_mobjects).arrange_in_grid(24, 20, buff=4).set_style(stroke_width=2, stroke_opacity=0.05) | |
all_path_mobjects.scale(min((config.frame_width - 1) / all_path_mobjects.width, (config.frame_height - 1) / all_path_mobjects.height)) | |
self.play(FadeIn(all_path_mobjects), FadeOut(title, subtitle, shift=DOWN)) | |
plane, path_mobject = draw_path(paths[0], plane) | |
self.play(Create(path_mobject), all_path_mobjects[0].animate.set_style(stroke_opacity=0.33)) | |
for ind, next_path in enumerate(paths[1:]): | |
plane, next_path_mobject = draw_path(next_path, plane) | |
self.play( | |
Transform(path_mobject, next_path_mobject, rate_func=smooth), | |
all_path_mobjects[ind+1].animate.set_style(stroke_opacity=0.33), | |
run_time=0.2 | |
) | |
self.wait(0.5) | |
self.play(FadeOut(path_mobject, shift=DOWN)) | |
self.wait() | |
self.play(FadeOut(all_path_mobjects)) | |
self.wait(0.15) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment