mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
first variants of generalized Pascal's Triangle
This commit is contained in:
parent
34d4d4316e
commit
5ab540a044
1 changed files with 286 additions and 136 deletions
|
@ -1,7 +1,7 @@
|
|||
from big_ol_pile_of_manim_imports import *
|
||||
from once_useful_constructs.combinatorics import *
|
||||
|
||||
nb_levels = 50
|
||||
nb_levels = 5
|
||||
|
||||
dev_x_step = 2
|
||||
dev_y_step = 5
|
||||
|
@ -13,14 +13,164 @@ def rainbow_color(alpha):
|
|||
index = int(alpha * nb_colors)
|
||||
return rainbow[index]
|
||||
|
||||
def graded_color(n,k):
|
||||
if n != 0:
|
||||
alpha = float(k)/n
|
||||
else:
|
||||
alpha = 0.5
|
||||
color = interpolate_color(RED, BLUE, alpha)
|
||||
return color
|
||||
|
||||
|
||||
def graded_square(n,k):
|
||||
return Square(
|
||||
side_length = 1,
|
||||
fill_color = graded_color(n,k),
|
||||
fill_opacity = 1,
|
||||
stroke_width = 1
|
||||
)
|
||||
|
||||
def graded_binomial(n,k):
|
||||
return Integer(
|
||||
choose(n,k),
|
||||
color = graded_color(n,k)
|
||||
)
|
||||
|
||||
def split_square(n,k):
|
||||
width = 1
|
||||
height = 1
|
||||
|
||||
proportion = float(choose(n,k)) / 2**n
|
||||
|
||||
lower_height = proportion * height
|
||||
upper_height = (1 - proportion) * height
|
||||
lower_rect = Rectangle(
|
||||
width = width,
|
||||
height = lower_height,
|
||||
fill_color = RED,
|
||||
fill_opacity = 1.0,
|
||||
stroke_color = WHITE,
|
||||
stroke_width = 3
|
||||
)
|
||||
upper_rect = Rectangle(
|
||||
width = width,
|
||||
height = upper_height,
|
||||
fill_color = BLUE,
|
||||
fill_opacity = 1.0,
|
||||
stroke_color = WHITE,
|
||||
stroke_width = 3
|
||||
)
|
||||
upper_rect.next_to(lower_rect,UP,buff = 0)
|
||||
square = VGroup(lower_rect, upper_rect).move_to(ORIGIN)
|
||||
return square
|
||||
|
||||
|
||||
class BuildNewPascalRow(Transform):
|
||||
|
||||
def __init__(self,mobject, duplicate_row = None, **kwargs):
|
||||
if mobject.__class__ != GeneralizedPascalsTriangle and mobject.__class__ != PascalsTriangle:
|
||||
raise("Transform BuildNewPascalRow only works on members of (Generalized)PascalsTriangle!")
|
||||
|
||||
n = mobject.nrows - 1
|
||||
lowest_row_copy1 = mobject.get_lowest_row()
|
||||
lowest_row_copy2 = duplicate_row
|
||||
|
||||
start_mob = VGroup(lowest_row_copy1, lowest_row_copy2)
|
||||
|
||||
new_pt = mobject.copy()
|
||||
new_pt.nrows += 1
|
||||
new_pt.generate_points()
|
||||
# align with original (copy got centered on screen)
|
||||
c1 = new_pt.coords_to_mobs[0][0].get_center()
|
||||
c2 = mobject.coords_to_mobs[0][0].get_center()
|
||||
print c1, c2
|
||||
v = c2 - c1
|
||||
new_pt.shift(v)
|
||||
|
||||
new_row_left_copy = VGroup(*[
|
||||
new_pt.coords_to_mobs[n+1][k]
|
||||
for k in range(0,n+1)
|
||||
])
|
||||
|
||||
new_row_right_copy = VGroup(*[
|
||||
new_pt.coords_to_mobs[n+1][k]
|
||||
for k in range(1,n+2)
|
||||
]).copy()
|
||||
|
||||
target_mob = VGroup(new_row_left_copy, new_row_right_copy)
|
||||
|
||||
Transform.__init__(self, start_mob, target_mob, **kwargs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class SimplePascal(Scene):
|
||||
|
||||
def build_new_pascal_row(self,old_pt):
|
||||
|
||||
lowest_row_copy = old_pt.get_lowest_row().copy()
|
||||
self.add(lowest_row_copy)
|
||||
|
||||
n = old_pt.nrows - 1
|
||||
lowest_row_copy1 = old_pt.get_lowest_row()
|
||||
lowest_row_copy2 = lowest_row_copy1.copy()
|
||||
|
||||
|
||||
start_mob = VGroup(lowest_row_copy1, lowest_row_copy2)
|
||||
self.add(start_mob)
|
||||
|
||||
new_pt = old_pt.copy()
|
||||
cell_height = old_pt.height / old_pt.nrows
|
||||
cell_width = old_pt.width / old_pt.nrows
|
||||
new_pt.nrows += 1
|
||||
new_pt.height = new_pt.nrows * cell_height
|
||||
new_pt.width = new_pt.nrows * cell_width
|
||||
|
||||
new_pt.generate_points()
|
||||
# align with original (copy got centered on screen)
|
||||
c1 = new_pt.coords_to_mobs[0][0].get_center()
|
||||
c2 = old_pt.coords_to_mobs[0][0].get_center()
|
||||
v = c2 - c1
|
||||
new_pt.shift(v)
|
||||
|
||||
new_row_left_copy = VGroup(*[
|
||||
new_pt.coords_to_mobs[n+1][k]
|
||||
for k in range(0,n+1)
|
||||
])
|
||||
|
||||
new_row_right_copy = VGroup(*[
|
||||
new_pt.coords_to_mobs[n+1][k]
|
||||
for k in range(1,n+2)
|
||||
]).copy()
|
||||
|
||||
target_mob = VGroup(new_row_left_copy, new_row_right_copy)
|
||||
self.play(Transform(start_mob, target_mob))
|
||||
|
||||
return new_pt
|
||||
|
||||
|
||||
class SampleScene(Scene):
|
||||
|
||||
def construct(self):
|
||||
|
||||
triangle = Polygon()
|
||||
self.add(triangle)
|
||||
self.wait()
|
||||
cell_height = 1
|
||||
cell_width = 1
|
||||
nrows = 1
|
||||
pt = GeneralizedPascalsTriangle(
|
||||
nrows = nrows,
|
||||
height = nrows * cell_height,
|
||||
width = nrows * cell_width,
|
||||
submob_class = graded_square,
|
||||
portion_to_fill = 0.9
|
||||
)
|
||||
pt.shift(3 * UP)
|
||||
self.add(pt)
|
||||
lowest_row_copy = pt.get_lowest_row().copy()
|
||||
self.add(lowest_row_copy)
|
||||
#self.play(BuildNewPascalRow(pt, duplicate_row = lowest_row_copy))
|
||||
for i in range(7):
|
||||
pt = self.build_new_pascal_row(pt)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue