From c44fa6ccfbad029ec02385fc74c6196d1f5dea9c Mon Sep 17 00:00:00 2001 From: Ben Hambrecht Date: Fri, 6 Apr 2018 13:58:49 +0200 Subject: [PATCH] added coins and tallies --- active_projects/eop/area_coin_toss.py | 149 +++++++++++++++++++++++++- active_projects/eop/chapter1.py | 23 ++++ active_projects/eop/pascal.py | 6 +- 3 files changed, 172 insertions(+), 6 deletions(-) diff --git a/active_projects/eop/area_coin_toss.py b/active_projects/eop/area_coin_toss.py index 35e988bf..004f218d 100644 --- a/active_projects/eop/area_coin_toss.py +++ b/active_projects/eop/area_coin_toss.py @@ -2,11 +2,73 @@ from big_ol_pile_of_manim_imports import * from eop.pascal import * -WIDTH = 6 -HEIGHT = 0.25 -COLOR_HEADS = YELLOW -COLOR_TAILS = BLUE -NB_ROWS = 28 +WIDTH = 12 +HEIGHT = 1 +GRADE_COLOR_1 = COLOR_HEADS = RED +GRADE_COLOR_2 = COLOR_TAILS = BLUE +NB_ROWS = 3 + + +class Coin(Circle): + CONFIG = { + "radius": 0.2, + "stroke_width": 3, + "stroke_color": WHITE, + "fill_opacity": 1, + "symbol": "\euro", + } + + def __init__(self, **kwargs): + Circle.__init__(self,**kwargs) + self.symbol_mob = TextMobject(self.symbol, stroke_color = self.stroke_color) + self.symbol_mob.scale_to_fit_height(0.5*self.get_height()).move_to(self) + self.add(self.symbol_mob) + + +class Heads(Coin): + CONFIG = { + "fill_color": COLOR_HEADS, + "symbol": "H", + } + + +class Tails(Coin): + CONFIG = { + "fill_color": COLOR_TAILS, + "symbol": "T", + } + +class CoinStack(VGroup): + CONFIG = { + "spacing": 0.1, + "size": 5, + "face": Heads, + } + + def generate_points(self): + for n in range(self.size): + coin = self.face() + coin.shift(n * self.spacing * RIGHT) + self.add(coin) + +class HeadsStack(CoinStack): + CONFIG = { "face": Heads } + +class TailsStack(CoinStack): + CONFIG = { "face": Tails } + +class TallyStack(VGroup): + + def __init__(self,h,t,**kwargs): + self.nb_heads = h + self.nb_tails = t + VGroup.__init__(self,**kwargs) + + def generate_points(self): + stack1 = HeadsStack(size = self.nb_heads) + stack2 = TailsStack(size = self.nb_tails) + stack2.next_to(stack1, RIGHT, buff = SMALL_BUFF) + self.add(stack1, stack2) class AreaSplittingScene(Scene): @@ -37,9 +99,15 @@ class AreaSplittingScene(Scene): def construct(self): + # Draw the bricks + + brick_wall = VGroup() rect_row = self.create_rect_row(0) rect_row.move_to(3.5*UP + 0*HEIGHT*DOWN) self.add(rect_row) + brick_wall.add(rect_row) + brick_dict = {"0": {"0": rect_row.submobjects[0]}} + for n in range(NB_ROWS): # copy and shift new_rect_row = rect_row.copy() @@ -50,16 +118,87 @@ class AreaSplittingScene(Scene): #split split_row = self.split_rect_row(new_rect_row) self.play(FadeIn(split_row)) + self.remove(new_rect_row) self.wait() # merge rect_row = self.create_rect_row(n+1) rect_row.move_to(3.5*UP + (n+1)*HEIGHT*DOWN) self.play(FadeIn(rect_row)) + brick_wall.add(rect_row) + self.remove(split_row) self.wait() + # add to brick dict + rect_dict = {} + i = 0 + for rect in rect_row.submobjects: + rect_dict[str(i)] = rect + print "added rect for (", n+1, ",", i, ")" + i += 1 + + brick_dict[str(n+1)] = rect_dict + + + for nrow_str,rect_row_dict in brick_dict.iteritems(): + for i_str, rect in rect_row_dict.iteritems(): + pos = rect.get_center() + nrow = int(nrow_str) + i = int(i_str) + print nrow, i + tally = TallyStack(nrow - i, i) + tally.move_to(pos) + self.add(tally) + + + + self.play( + brick_wall.set_fill, {"opacity" : 0.2} + ) + + + # Draw the branches + +# for n in NB_ROWS: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + self.wait() + + + diff --git a/active_projects/eop/chapter1.py b/active_projects/eop/chapter1.py index baa5cfee..4af9c46b 100644 --- a/active_projects/eop/chapter1.py +++ b/active_projects/eop/chapter1.py @@ -101,3 +101,26 @@ class CoinFlipScene(Scene): + + + + + + + + + + + + + + + + + + + + + + + diff --git a/active_projects/eop/pascal.py b/active_projects/eop/pascal.py index 2ea554b6..688d3a5e 100644 --- a/active_projects/eop/pascal.py +++ b/active_projects/eop/pascal.py @@ -6,6 +6,10 @@ nb_levels = 5 dev_x_step = 2 dev_y_step = 5 +GRADE_COLOR_1 = RED +GRADE_COLOR_2 = BLUE + + def rainbow_color(alpha): nb_colors = 100 rainbow = color_gradient([RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE], nb_colors) @@ -18,7 +22,7 @@ def graded_color(n,k): alpha = float(k)/n else: alpha = 0.5 - color = interpolate_color(RED, BLUE, alpha) + color = interpolate_color(GRADE_COLOR_1, GRADE_COLOR_2, alpha) return color