Skip to content

Commit

Permalink
removed unnecessary stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sidlim committed Mar 12, 2021
1 parent d38a371 commit 6abb612
Showing 1 changed file with 11 additions and 34 deletions.
45 changes: 11 additions & 34 deletions assorted_scenes/pascal_triangle.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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):
return(0)
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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand All @@ -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)]
Expand Down

0 comments on commit 6abb612

Please sign in to comment.