def construct(self): formula = TexMobject(*([ "{%d \\over %d} \\cdot" % (wallis_numer(n), wallis_denom(n)) for n in range(1, self.n_terms + 1) ] + ["\\cdots"])) result = TexMobject("=", "{\\pi \\over 2}") result.scale(2) pi = result[-1][0] pi.set_color(YELLOW) circle = Circle(color=YELLOW) circle.surround(pi) question = TexMobject("?", color=YELLOW) question.scale(2).next_to(circle, RIGHT, buff=0.4) result_group = VGroup(result, circle, question) result_group.next_to(formula, DOWN) group = VGroup(formula, result_group) group.center().set_width(10) bg_rect = BackgroundRectangle(group, fill_opacity=0.5, buff=0.2) random_walks = VGroup(*[ RandomWalk1DArrow(random_walk_string(30), step_size=0.5) for k in range(self.n_walks) ]) for k, walk in enumerate(random_walks): walk.shift(random.randrange(-5, 5) * 2 * walk.step_size * UP) random_walks.center() text = TextMobject(self.part).scale(3).set_color(YELLOW) text.to_corner(RIGHT + DOWN) self.add(random_walks) self.add(FullScreenFadeRectangle()) self.add(bg_rect, group, text)
def get_order_n_self(self, order): if order == 0: result = self.get_seed_shape() else: lower_order = self.get_order_n_self(order - 1) subparts = [lower_order.copy() for x in range(self.num_subparts)] self.arrange_subparts(*subparts) result = VGroup(*subparts) result.set_height(self.height) result.center() return result
def get_order_n_self(self, order): if order == 0: result = self.get_seed_shape() else: lower_order = self.get_order_n_self(order - 1) subparts = [ lower_order.copy() for x in range(self.num_subparts) ] self.arrange_subparts(*subparts) result = VGroup(*subparts) result.scale_to_fit_height(self.height) result.center() return result
def setup(self): pos_axis = NumberLine(x_min=-4.5, x_max=2.5, color=WHITE) pos_axis.rotate(np.pi / 2) pos_axis.add_tip() time_axis = NumberLine(x_min=0, x_max=9.5, color=WHITE) time_axis.add_tip() vec = pos_axis.number_to_point(0) - time_axis.number_to_point(0) time_axis.shift(vec) pos_text = TextMobject("位置") pos_text.next_to(pos_axis.number_to_point(2.5), LEFT) time_text = TextMobject("时间") time_text.next_to(time_axis.number_to_point(9.5), DOWN) axes_group = VGroup(pos_axis, time_axis, pos_text, time_text) axes_group.center() title_pq = TexMobject("P_4", "570", "Q_4") title_pq.scale(1.5) title_pq.to_corner(UP + RIGHT) for part, color in zip(title_pq, [ORANGE, BLACK, GREEN]): part.set_color(color) r_arrow = TexMobject("\\rightarrow") l_arrow = TexMobject("\\leftarrow") for arrow in (r_arrow, l_arrow): arrow.scale(1.5) arrow.move_to(title_pq[1]) sequence_p, sequence_q = sequences = ["UDUUDUDD", "DDUDDUDD"] colors = [ORANGE, GREEN] walk_p, walk_q = walks = [ RandomWalk1DArrow(sequence, up_color = color, down_color = color) \ .move_start_to(pos_axis.number_to_point(0)) for sequence, color in zip(sequences, colors) ] parts_p, parts_q = [walk.split_at(3) for walk in walks] self.axes_group = axes_group self.title_pq = title_pq self.walk_p = walk_p self.walk_q = walk_q self.parts_p = parts_p self.parts_q = parts_q self.r_arrow = r_arrow self.l_arrow = l_arrow
def construct(self): # Chart on the left colors = [WHITE, ORANGE, GREEN] titles = VGroup(*[ TexMobject(text).set_color(color) for text, color in zip(["n", "p_n", "q_n"], colors) ]) contents = VGroup(*[ VGroup(*[ TexMobject("%d" % num) for num in [k, central_binomial_coeff(k), central_binomial_coeff(k)] ]) for k in range(8) ]) titles.arrange_submobjects(RIGHT, buff=1) for num, line in enumerate(contents): for k, element in enumerate(line): buff = 0.6 + 0.8 * num element.next_to(titles[k], DOWN, aligned_edge=LEFT, buff=buff) element.set_color(colors[k]) sep_line = Line(ORIGIN, 4.5 * RIGHT, stroke_width=5) sep_line.next_to(titles, DOWN) chart = VGroup(titles, contents, sep_line) chart.set_height(7) chart.center().to_edge(LEFT) self.add(chart) # Figures on the right std_zero_pos_axis = NumberLine(x_min=-2, x_max=2, color=GREY, unit_size=0.25, tick_size=0.05) std_zero_pos_axis.rotate(np.pi / 2) std_nocross_pos_axis = NumberLine(x_min=-4, x_max=4, color=GREY, unit_size=0.25, tick_size=0.05) std_nocross_pos_axis.rotate(np.pi / 2) std_time_axis = NumberLine(x_min=0, x_max=5.5, color=GREY, unit_size=0.25, tick_size=0.05) std_zero_axes = VGroup(std_zero_pos_axis, std_time_axis) std_nocross_axes = VGroup(std_nocross_pos_axis, std_time_axis) zero_walks = VGroup() for sequence in ["UUDD", "UDUD", "UDDU", "DDUU", "DUDU", "DUUD"]: axes = std_zero_axes.copy() zero_walk = RandomWalk1DArrow(sequence, step_size=0.25) zero_walk.move_start_to(axes[0].number_to_point(0)) zero_walks.add(VGroup(axes, zero_walk)) zero_walks.arrange_submobjects_in_grid(2, 3, buff=0.5) zero_rect = SurroundingRectangle(zero_walks, color=ORANGE, buff=0.4) zero_walks.add(zero_rect) nocross_walks = VGroup() for sequence in ["DDDD", "DDUD", "DDDU", "UUUU", "UUDU", "UUUD"]: axes = std_nocross_axes.copy() nocross_walk = RandomWalk1DArrow(sequence, step_size=0.25) nocross_walk.move_start_to(axes[0].number_to_point(0)) nocross_walks.add(VGroup(axes, nocross_walk)) nocross_walks.arrange_submobjects_in_grid(2, 3, buff=0.5) nocross_rect = SurroundingRectangle(nocross_walks, color=GREEN, buff=0.4) nocross_walks.add(nocross_rect) relation = TexMobject("p_2", "=", "q_2", "=", "6") relation[0].set_color(ORANGE) relation[2].set_color(GREEN) relation.scale(1.5) figure = VGroup(zero_walks, relation, nocross_walks) figure.arrange_submobjects(DOWN) figure.set_height(7) figure.center().to_edge(RIGHT) self.add(figure) self.wait()
def add_figure(self): # Main figure wallis_rects = WallisRectangles(order=10) order = wallis_rects.get_order() # Variables and braces on the left left_rects = [ wallis_rects.get_rectangle(k, 0) for k in [0, 1, 2, 3, -1] ] left_braces = [ Brace(rect, direction=LEFT, buff=0.1) for rect in left_rects ] left_lengths = [ TexMobject(symbol) for symbol in ["a_0", "a_1", "a_2", "a_3", "a_{n-1}"] ] left_lengths[-1].scale(0.7) for brace, length in zip(left_braces, left_lengths): brace.put_at_tip(length, buff=0.15) left_vdots = TexMobject("\\vdots") left_vdots.scale(2) left_vdots.move_to( (left_braces[-1].get_center() + left_braces[-2].get_center()) / 2) # Variables and braces on the bottom down_rects = [ wallis_rects.get_rectangle(0, k) for k in [0, 1, 2, 3, -1] ] down_braces = [ Brace(rect, direction=DOWN, buff=0.1) for rect in down_rects ] down_lengths = [ TexMobject(symbol) for symbol in ["a_0", "a_1", "a_2", "a_3", "a_{n-1}"] ] down_lengths[-1].scale(0.7) for brace, length in zip(down_braces, down_lengths): brace.put_at_tip(length, buff=0.15) down_cdots = TexMobject("\\cdots") down_cdots.scale(2) down_cdots.move_to( (down_braces[-1].get_center() + down_braces[-2].get_center()) / 2) # The quarter circle quarter_circle = Sector( outer_radius=wallis_rects.get_height(), stroke_color=GREY, stroke_width=5.70, fill_opacity=0, ) quarter_circle.move_arc_center_to(wallis_rects.get_bottom_left()) # Add everthing figure_group = VGroup( wallis_rects, VGroup(*left_braces), VGroup(*left_lengths), left_vdots, VGroup(*down_braces), VGroup(*down_lengths), down_cdots, quarter_circle, ) figure_group.center().to_edge(LEFT, buff=0.15) self.add(figure_group)