A community-maintained Python framework for creating mathematical animations.
throwing darts
Ben Hambrecht5cc5b204085a
diff --git a/active_projects/eop/chapter1.py b/active_projects/eop/chapter1.pyindex 6e62cf9f7..6552a47e2 100644--- a/active_projects/eop/chapter1.py+++ b/active_projects/eop/chapter1.py@@ -1121,7 +1121,7 @@ class RowOfDice(VGroup):-class ShowUncertainty(PiCreatureScene):+class ShowUncertainty1(PiCreatureScene):def throw_a_die(self):@@ -1132,21 +1132,28 @@ class ShowUncertainty(PiCreatureScene):new_hist = self.hist_from_tallies()self.play(- ApplyMethod(face.submobjects[0].set_fill, {"opacity": 1}, rate_func = there_and_back),- Transform(self.dice_histogram, new_hist)+ ApplyMethod(face.submobjects[0].set_fill, {"opacity": 1},+ rate_func = there_and_back,+ run_time = 0.3,+ ),+ )+ self.play(+ Transform(self.dice_histogram, new_hist,+ run_time = 0.5))def hist_from_tallies(self):- hist = Histogram(0.5 * np.ones(6), self.tallies,+ x_scale = self.row_of_dice.get_width() / np.size(self.tallies)+ hist = Histogram(np.ones(6), self.tallies,mode = "widths",x_labels = "none",- y_scale = 0.5+ y_scale = 0.5,+ x_scale = x_scale)hist.next_to(self.row_of_dice, UP)- hist.stretch_to_fit_width(self.row_of_dice.get_width())return histdef construct(self):@@ -1154,18 +1161,58 @@ class ShowUncertainty(PiCreatureScene):self.row_of_dice = RowOfDice().scale(0.5).move_to(3 * DOWN)self.add(self.row_of_dice)- self.tallies = np.ones(6)+ self.tallies = np.zeros(6)self.dice_histogram = self.hist_from_tallies()self.add(self.dice_histogram)for i in range(30):self.throw_a_die()+ self.wait()++class ShowUncertainty2(PiCreatureScene):+ def throw_darts(self, n, run_time = 1):++ points = np.random.normal(+ loc = self.dartboard.get_center(),+ scale = 0.6 * np.ones(3),+ size = (n,3)+ )+ points[:,2] = 0+ dots = VGroup()+ for point in points:+ dot = Dot(point, radius = 0.04, fill_opacity = 0.7)+ dots.add(dot)+ self.add(dot)++ self.play(+ LaggedStart(FadeIn, dots, lag_ratio = 0.01, run_time = run_time)+ )+++ def construct(self):++ self.dartboard = ImageMobject("dartboard").scale(2)+ dartboard_circle = Circle(+ radius = self.dartboard.get_width() / 2,+ fill_color = BLACK,+ fill_opacity = 0.5,+ stroke_color = WHITE,+ stroke_width = 5+ )+ self.dartboard.add(dartboard_circle)++ self.add(self.dartboard)++ self.throw_darts(5,5)+ self.throw_darts(20,5)+ self.throw_darts(100,5)+ self.throw_darts(1000,5)