From 6abb612a95136addb0a4ab2dfee22448d1d35839 Mon Sep 17 00:00:00 2001 From: Sid Limaye Date: Fri, 12 Mar 2021 14:40:27 -0800 Subject: [PATCH] removed unnecessary stuff --- assorted_scenes/pascal_triangle.py | 45 ++++++++---------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/assorted_scenes/pascal_triangle.py b/assorted_scenes/pascal_triangle.py index 54fd30f..a02b04e 100644 --- a/assorted_scenes/pascal_triangle.py +++ b/assorted_scenes/pascal_triangle.py @@ -1,6 +1,5 @@ from manim import * -import math -import itertools +import math, itertools, functools def int_binom(n: int, k: int) -> int: if (k < 0) or (k > n): @@ -8,6 +7,13 @@ def int_binom(n: int, k: int) -> int: else: return(math.factorial(n) // (math.factorial(k) * math.factorial(n-k))) +def centroid(*iterables): + sum = functools.reduce((lambda x,y: x + y), iterables) + return(sum / float(len(iterables))) + +def center_centroid(*iterables): + return(centroid(*map((lambda x : x.get_center()), iterables))) + class VRow(VGroup): def __init__(self, *vmobjects, spacing = 1 * RIGHT, **kwargs): super().__init__(**kwargs) @@ -37,8 +43,8 @@ def construct(self): # Build Pascal's Triangle: tex_wrap = (lambda x: MathTex(str(x))) tex_coeff = {i: {j: tex_wrap(int_binom(i, j)) for j in range(0, i + 1)} for i in range(1, 5)} - rows = [VRow(*tex_coeff[i].values(), spacing = 2 * RIGHT) for i in tex_coeff.keys()] - triangle = VRow(*rows, spacing = 2 * DOWN) + rows = [VGroup(*tex_coeff[i].values()).arrange_submobjects(RIGHT, buff = 2) for i in tex_coeff.keys()] + triangle = VGroup(*rows).arrange_submobjects(DOWN, buff = 2) for i in tex_coeff.keys(): self.play(Write(tex_coeff[i][0]), Write(tex_coeff[i][i])) self.wait(1) @@ -47,7 +53,7 @@ def construct(self): self.wait(1) self.play(FadeIn(add_rig), Write(tex_coeff[i][1])) for j in range(1, i - 1): - self.play(add_rig.animate.shift(2 * RIGHT)) + self.play(add_rig.animate.shift(center_centroid(tex_coeff[i-1][j], tex_coeff[i-1][j+1]) - add_rig.submobjects[0].get_center())) self.play(Write(tex_coeff[i][j + 1])) self.play(FadeOut(add_rig)) @@ -59,35 +65,6 @@ def build_addition_rig(self, left_el, right_el): add_rig = VGroup(*[plus, arrow]) return(add_rig) -class SumAnimationWithZeros(Scene): - def construct(self): - # Build Pascal's Triangle: - tex_wrap = (lambda x: MathTex(str(x))) - tex_coeff = [{j: tex_wrap(int_binom(i, j)) for j in range(-1, i + 2)} for i in range(0, 4)] - rows = [VRow(*tex_coeff[i].values(), spacing = 2 * RIGHT) for i in range(len(tex_coeff))] - triangle = VRow(*rows, spacing = 2 * DOWN) - - self.play(Write(tex_coeff[0][0])) - self.wait(1) - for i in range(1, len(rows)): - add_rig = self.build_addition_rig(tex_coeff[i - 1][-1], tex_coeff[i - 1][0]) - self.play(FadeIn(tex_coeff[i - 1][-1]), FadeIn(tex_coeff[i - 1][i])) - self.wait(1) - self.play(FadeIn(add_rig), Write(tex_coeff[i][0])) - for j in range(1, i + 1): - self.play(add_rig.animate.shift(2 * RIGHT)) - self.play(Write(tex_coeff[i][j])) - self.play(FadeOut(tex_coeff[i - 1][-1]), FadeOut(tex_coeff[i - 1][i]), FadeOut(add_rig)) - - - def build_addition_rig(self, left_el, right_el): - plus = MathTex("+") - plus.move_to((left_el.get_center() + right_el.get_center()) / 2.0) - arrow = Arrow(start = 0.75 * UP, end = 0.75 * DOWN) - arrow.next_to(plus, DOWN * 1.5) - add_rig = VGroup(*[plus, arrow]) - return(add_rig) - class PascalMonomials(Scene): def construct(self): coeff_data = [(int_binom(4, i), i) for i in range(0, 5)]