A community-maintained Python framework for creating mathematical animations.
finessing brick row scene, added weather forecast visual
Ben Hambrecht4e1e1e069f53
diff --git a/active_projects/eop/chapter0.py b/active_projects/eop/chapter0.pynew file mode 100644index 000000000..94a9d9df1--- /dev/null+++ b/active_projects/eop/chapter0.py@@ -0,0 +1,82 @@+from big_ol_pile_of_manim_imports import *++class Introduction(TeacherStudentsScene):++ CONFIG = {+ "default_pi_creature_kwargs": {+ "color": MAROON_E,+ "flip_at_start": True,+ },+ }++ def construct(self):+ self.show_series()+ self.show_examples()++ def show_series(self):+ series = VideoSeries(num_videos = 11)+ series.to_edge(UP)+ this_video = series[0]+ this_video.set_color(YELLOW)+ this_video.save_state()+ this_video.set_fill(opacity = 0)+ this_video.center()+ this_video.scale_to_fit_height(FRAME_HEIGHT)+ self.this_video = this_video+++ words = TextMobject(+ "Welcome to \\\\",+ "Essence of Probability"+ )+ words.set_color_by_tex("Essence of Probability", YELLOW)++ self.teacher.change_mode("happy")+ self.play(+ FadeIn(+ series,+ submobject_mode = "lagged_start",+ run_time = 2+ ),+ Blink(self.get_teacher())+ )+ self.teacher_says(words, target_mode = "hooray")+ self.change_student_modes(+ *["hooray"]*3,+ look_at_arg = series[1].get_left(),+ added_anims = [+ ApplyMethod(this_video.restore, run_time = 3),+ ]+ )+ self.play(*[+ ApplyMethod(+ video.shift, 0.5*video.get_height()*DOWN,+ run_time = 3,+ rate_func = squish_rate_func(+ there_and_back, alpha, alpha+0.3+ )+ )+ for video, alpha in zip(series, np.linspace(0, 0.7, len(series)))+ ]+[+ Animation(self.teacher.bubble),+ Animation(self.teacher.bubble.content),+ ])++ self.play(+ FadeOut(self.teacher.bubble),+ FadeOut(self.teacher.bubble.content),+ self.get_teacher().change_mode, "raise_right_hand",+ *[+ ApplyMethod(pi.change_mode, "pondering")+ for pi in self.get_students()+ ]+ )+ self.wait()++ self.series = series+++ def show_examples(self):++ self.wait(10)+ # put examples here in video editordiff --git a/active_projects/eop/chapter1/brick_row_scene.py b/active_projects/eop/chapter1/brick_row_scene.pyindex 120cc788a..9e21cccc0 100644--- a/active_projects/eop/chapter1/brick_row_scene.py+++ b/active_projects/eop/chapter1/brick_row_scene.py@@ -96,7 +96,7 @@ class BrickRowScene(PiCreatureScene):new_tally_left = TallyStack(tally.nb_heads + 1, tally.nb_tails)target_left = tally_targets_left[i]- new_tally_left.move_anchor_to(target_left)+ new_tally_left.move_to(target_left)anims1.append(Transform(tally, new_tally_left))@@ -111,7 +111,7 @@ class BrickRowScene(PiCreatureScene):new_tally_right = TallyStack(tally.nb_heads, tally.nb_tails + 1)target_right = tally_targets_right[i]- new_tally_right.move_anchor_to(target_right)+ new_tally_right.move_to(target_right)anims2.append(Transform(tally, new_tally_right))@@ -147,18 +147,24 @@ class BrickRowScene(PiCreatureScene):self.remove(self.row)self.row = half_merged_row- def merge_tallies(self, direction = UP):- r = self.row.subdiv_level- tally_targets = [- rect.get_center()- for rect in self.row.get_rects_for_level(r)- ]- if np.all(direction == LEFT) or np.all(direction == RIGHT):++ def merge_tallies(self, target_pos = UP):++ r = self.row.subdiv_level++ if np.all(target_pos == DOWN):+ tally_targets = [+ rect.get_center()+ for rect in self.row.get_rects_for_level(r)+ ]+ elif np.all(target_pos == UP):y_pos = self.row.get_center()[1] + 1.2 * 0.5 * self.row.get_height()for target in tally_targets:target[1] = y_pos+ else:+ raise Exception("Invalid target position (either UP or DOWN)")anims = []for (tally, target) in zip(self.tallies[1:], tally_targets[1:-1]):@@ -239,7 +245,13 @@ class BrickRowScene(PiCreatureScene):))- self.play(FlipCoin(randy) )+ # # # # # # # #+ # FIRST FLIP #+ # # # # # # # #++++ self.play(FlipCoin(randy))self.play(SplitRectsInBrickWall(self.row))self.merge_rects_by_subdiv()@@ -454,7 +466,7 @@ class BrickRowScene(PiCreatureScene):# THIRD FLIP ## # # # # # # #- # move row up, leace a copy without tallies below+ # move row up, leave a copy without tallies belownew_row = self.row.copy()self.clear()self.add(randy, self.row, self.tallies)@@ -478,29 +490,218 @@ class BrickRowScene(PiCreatureScene):self.split_tallies_in_two_steps()self.wait()++ self.add_foreground_mobject(self.tallies)+ self.add_foreground_mobject(self.tallies_copy)+++++ self.remove(new_row)+ new_row = self.row++++ self.clear()+ self.add(randy, self.row, old_row)+ self.add_foreground_mobject(self.tallies)+++ self.play(+ self.row.fade, 0.7,+ old_row.fade, 0.7,+ FadeOut(self.tallies),+ )++ # # # # # # # # # # # # # # # # #+ # SHOW SPLITTING WITH OUTCOMES #+ # # # # # # # # # # # # # # # # #++ # # show individual outcomes+ # old_outcomes = old_row.get_outcome_rects_for_level(2, with_labels = True)+ # old_outcomes_copy = old_outcomes.copy()+ # new_outcomes = self.row.get_outcome_rects_for_level(3, with_labels = True)++ # self.play(+ # FadeIn(old_outcomes[0]),+ # FadeIn(old_outcomes_copy[0]),+ # )++ # self.wait()++ # self.play(+ # Transform(old_outcomes_copy[0], new_outcomes[1])+ # )++ # self.wait()++ # self.play(+ # FadeIn(old_outcomes[1:3]),+ # FadeIn(old_outcomes_copy[1:3]),+ # )++ # self.wait()++ # self.play(+ # Transform(old_outcomes_copy[1:3], new_outcomes[2:4])+ # )++ # self.wait()++ # self.merge_rects_by_subdiv()+ # self.wait()++ # self.play(+ # FadeOut(old_row),+ # FadeOut(old_outcomes[0:3]),+ # FadeOut(new_outcomes[1:4]),+ # self.row.fade,0,+ # FadeIn(self.tallies[1]),+ # FadeIn(self.tallies_copy[0]),+ # )++ # # rest of the new row+ # self.play(+ # FadeIn(self.tallies[:1]),+ # FadeIn(self.tallies[2:]),+ # FadeIn(self.tallies_copy[1:])+ # )++ # self.wait()++ # self.merge_tallies(target_pos = DOWN)+ # self.add_foreground_mobject(self.tallies)+ # self.merge_rects_by_coloring()+ # self.wait()+++ # # # # # # # # # # # # # # # #+ # SHOW SPLITTING WITH TALLIES #+ # # # # # # # # # # # # # # # #++ tally_left = TallyStack(2,0)+ tally_left.move_to(old_row.rects[0])+ tally_right = TallyStack(1,1)+ tally_right.move_to(old_row.rects[1])++ rect_left = old_row.rects[0].copy()+ rect_right = old_row.rects[1].copy()++ self.play(+ FadeIn(rect_left),+ FadeIn(rect_right),+ FadeIn(tally_left),+ FadeIn(tally_right)+ )++ rect_left.target = rect_left.copy()+ rect_left.target.stretch(0.5,0)+ left_target_pos = self.row.get_outcome_centers_for_level(3)[1]+ left_v = left_target_pos - rect_left.get_center()+ rect_left.target.move_to(left_target_pos)++ rect_right.target = rect_right.copy()+ rect_right.target.stretch(0.5,0)+ right_target_pos = 0.5 * (self.row.get_outcome_centers_for_level(3)[2] + self.row.get_outcome_centers_for_level(3)[3])+ right_v = right_target_pos - rect_right.get_center()+ rect_right.target.move_to(right_target_pos)++ self.play(+ MoveToTarget(rect_left),+ tally_left.move_to, left_target_pos+ )+ #tally_left.anchor += left_v++ self.wait()++ new_tally_left = TallyStack(2,1)+ #new_tally_left.move_anchor_to(tally_left.anchor)+ new_tally_left.move_to(tally_left)++ self.play(+ Transform(tally_left, new_tally_left)+ )++ self.play(+ MoveToTarget(rect_right),+ tally_right.move_to, right_target_pos+ )+ #tally_right.anchor += right_v++ self.wait()+ new_tally_right = TallyStack(2,1)+ #new_tally_right.move_anchor_to(tally_right.anchor)+ new_tally_right.move_to(tally_right)++ self.play(+ Transform(tally_right, new_tally_right)+ )++ self.wait()+self.merge_rects_by_subdiv()self.wait()- self.merge_tallies()++ self.play(+ FadeOut(old_row),+ self.row.fade,0,+ FadeOut(new_tally_left),+ FadeOut(new_tally_right),+ FadeIn(self.tallies[1]),+ FadeIn(self.tallies_copy[0]),+ )++ # rest of the new row+ self.play(+ FadeIn(self.tallies[:1]),+ FadeIn(self.tallies[2:]),+ FadeIn(self.tallies_copy[1:])+ )++ self.wait()++ self.merge_tallies(target_pos = DOWN)+ self.add_foreground_mobject(self.tallies)self.merge_rects_by_coloring()self.wait()- self.move_tallies_on_top()- return++ # show the 8 individual outcomes+ outcomes = self.row.get_outcome_rects_for_level(3,+ with_labels = True,+ inset = True)+ self.play(FadeOut(self.tallies))+ self.play(LaggedStart(+ FadeIn, outcomes))+ self.wait()+ self.play(LaggedStart(+ FadeOut, outcomes))+ self.play(FadeIn(self.tallies))++ brace1 = Brace(self.row.rects[2], UP)+ brace2 = Brace(self.row.rects[3], UP)+ p1 = TexMobject("{3\over 8}").next_to(brace1, UP)+ p2 = TexMobject("{1\over 8}").next_to(brace2, UP)- # show individual outcomes- outcomes = self.row.get_outcome_rects_for_level(3, with_labels = True)self.play(- LaggedStart(FadeIn, outcomes)+ ShowCreation(brace1),+ ShowCreation(brace2),+ Write(p1),+ Write(p2),)self.wait()+self.play(- LaggedStart(FadeOut, outcomes)+ FadeOut(brace1),+ FadeOut(brace2),+ FadeOut(p1),+ FadeOut(p2),)+ self.wait()-- # # # # # # # # # # # # # #- # # FOURTH FLIP IN DETAIL #- # # # # # # # # # # # # # #+ # # # # # # # #+ # FOURTH FLIP #+ # # # # # # # #@@ -514,12 +715,6 @@ class BrickRowScene(PiCreatureScene):self.remove_foreground_mobject(tally)self.remove(tally)- # delete all the old crap hidden behind the row- # before we can move it- self.remove(*self.mobjects)- self.add(randy) #,self.decimals,self.decimal_copies)--previous_row = self.row.copy()self.add(previous_row)diff --git a/active_projects/eop/chapter1/weather.py b/active_projects/eop/chapter1/weather.pynew file mode 100644index 000000000..398519873--- /dev/null+++ b/active_projects/eop/chapter1/weather.py@@ -0,0 +1,140 @@+from big_ol_pile_of_manim_imports import *+from active_projects.eop.reusable_imports import *+++class WeatherForecast(PiCreatureScene):++ CONFIG = {+ "default_pi_creature_kwargs": {+ "color": MAROON_E,+ "flip_at_start": False,+ },+ }++ def construct(self):++ unit_rect = Rectangle(+ height = 4, width = 4+ )++ p_rain = 0.23+ p_sun = 1 - p_rain+ opacity = 0.7++ rain_rect = unit_rect.copy().stretch(p_rain, 0)+ rain_rect.align_to(unit_rect, LEFT)+ rain_rect.set_fill(color = BLUE, opacity = opacity)+ rain_rect.set_stroke(width = 0)++ sun_rect = unit_rect.copy().stretch(p_sun, 0)+ sun_rect.next_to(rain_rect, RIGHT, buff = 0)+ sun_rect.set_fill(color = YELLOW, opacity = opacity)+ sun_rect.set_stroke(width = 0)++ self.add(unit_rect, rain_rect, sun_rect)++ rain = SVGMobject(file_name = "rain").scale(0.35)+ sun = SVGMobject(file_name = "sun").scale(0.35)++ rain.flip().move_to(rain_rect)+ sun.move_to(sun_rect)++ self.add(rain, sun)++ text_scale = 0.7++ brace_rain = Brace(rain_rect, UP)+ p_rain_label = TextMobject("$p($rain$)=$").scale(text_scale)+ p_rain_decimal = DecimalNumber(p_rain).scale(text_scale)+ p_rain_decimal.next_to(p_rain_label)+ p_rain_whole_label = VGroup(p_rain_label, p_rain_decimal)+ p_rain_whole_label.next_to(brace_rain, UP)++ brace_sun = Brace(sun_rect, DOWN)+ p_sun_label = TextMobject("$p($sun$)=$").scale(text_scale)+ p_sun_decimal = DecimalNumber(p_sun).scale(text_scale)+ p_sun_decimal.next_to(p_sun_label)+ p_sun_whole_label = VGroup(p_sun_label, p_sun_decimal)+ p_sun_whole_label.next_to(brace_sun, DOWN)++ self.add(brace_rain, p_rain_whole_label, brace_sun, p_sun_whole_label)+++++ new_p_rain = 0.68+ new_p_sun = 1 - new_p_rain++ new_rain_rect = unit_rect.copy().stretch(new_p_rain, 0)+ new_rain_rect.align_to(unit_rect, LEFT)+ new_rain_rect.set_fill(color = BLUE, opacity = opacity)+ new_rain_rect.set_stroke(width = 0)++ new_sun_rect = unit_rect.copy().stretch(new_p_sun, 0)+ new_sun_rect.next_to(new_rain_rect, RIGHT, buff = 0)+ new_sun_rect.set_fill(color = YELLOW, opacity = opacity)+ new_sun_rect.set_stroke(width = 0)++ new_rain = SVGMobject(file_name = "rain").scale(0.35)+ new_sun = SVGMobject(file_name = "sun").scale(0.35)++ new_rain.flip().move_to(new_rain_rect)+ new_sun.move_to(new_sun_rect)++ new_brace_rain = Brace(new_rain_rect, UP)+ new_p_rain_label = TextMobject("$p($rain$)=$").scale(text_scale)+ new_p_rain_decimal = DecimalNumber(new_p_rain).scale(text_scale)+ new_p_rain_decimal.next_to(new_p_rain_label)+ new_p_rain_whole_label = VGroup(new_p_rain_label, new_p_rain_decimal)+ new_p_rain_whole_label.next_to(new_brace_rain, UP)+++ new_brace_sun = Brace(new_sun_rect, DOWN)+ new_p_sun_label = TextMobject("$p($sun$)=$").scale(text_scale)+ new_p_sun_decimal = DecimalNumber(new_p_sun).scale(text_scale)+ new_p_sun_decimal.next_to(new_p_sun_label)+ new_p_sun_whole_label = VGroup(new_p_sun_label, new_p_sun_decimal)+ new_p_sun_whole_label.next_to(new_brace_sun, DOWN)++ def rain_update_func(alpha):+ return alpha * new_p_rain + (1 - alpha) * p_rain++ def sun_update_func(alpha):+ return 1 - rain_update_func(alpha)++ run_time = 5++ update_p_rain = ChangingDecimal(+ p_rain_decimal, rain_update_func,+ tracked_mobject = p_rain_label,+ run_time = run_time+ )+ update_p_sun = ChangingDecimal(+ p_sun_decimal, sun_update_func,+ tracked_mobject = p_sun_label,+ run_time = run_time+ )++ self.play(+ Transform(rain_rect, new_rain_rect, run_time = run_time),+ Transform(sun_rect, new_sun_rect, run_time = run_time),+ Transform(rain, new_rain, run_time = run_time),+ Transform(sun, new_sun, run_time = run_time),+ Transform(brace_rain, new_brace_rain, run_time = run_time),+ Transform(brace_sun, new_brace_sun, run_time = run_time),+ Transform(p_rain_label, new_p_rain_label, run_time = run_time),+ Transform(p_sun_label, new_p_sun_label, run_time = run_time),+ update_p_rain,+ update_p_sun+ )+++++++++++diff --git a/active_projects/eop/reusables/brick_row.py b/active_projects/eop/reusables/brick_row.pyindex 881667284..288672a77 100644--- a/active_projects/eop/reusables/brick_row.py+++ b/active_projects/eop/reusables/brick_row.py@@ -10,8 +10,8 @@ class BrickRow(VMobject):"right_color" : BLUE,"height" : 1.0,"width" : 8.0,- "outcome_shrinkage_factor_x" : 1.0,- "outcome_shrinkage_factor_y" : 1.0+ "outcome_shrinkage_factor_x" : 0.95,+ "outcome_shrinkage_factor_y" : 0.95}def __init__(self, n, **kwargs):@@ -94,11 +94,16 @@ class BrickRow(VMobject):return centers- def get_outcome_rects_for_level(self,r, with_labels = False):+ def get_outcome_rects_for_level(self, r, inset = False, with_labels = False):centers = self.get_outcome_centers_for_level(r)- outcome_width = self.outcome_shrinkage_factor_x * float(self.width) / (2 ** r)- outcome_height = self.outcome_shrinkage_factor_y * self.height+ if inset == True:+ outcome_width = self.outcome_shrinkage_factor_x * float(self.width) / (2 ** r)+ outcome_height = self.outcome_shrinkage_factor_y * self.height+ else:+ outcome_width = float(self.width) / (2 ** r)+ outcome_height = self.height+corner_radius = 0 # min(0.1, 0.3 * min(outcome_width, outcome_height))# this scales down the corner radius for very narrow rectsrect = Rectangle( # RoundedRectangle(diff --git a/active_projects/eop/reusables/coin_stacks.py b/active_projects/eop/reusables/coin_stacks.pyindex 16920ffbd..92d78c37a 100644--- a/active_projects/eop/reusables/coin_stacks.py+++ b/active_projects/eop/reusables/coin_stacks.py@@ -94,4 +94,18 @@ class TallyStack(VGroup):self.anchor = new_anchorself.position_decimal_tally(self.decimal_tally)- return self\ No newline at end of file+ return self++++++++++++++