def construct(self): square = Square(side_length=5, fill_color=YELLOW, fill_opacity=1) label = TextMobject("Text at an angle") label_bg = BackgroundRectangle(label, fill_opacity=1) label_group = VGroup(label_bg, label) label_group.rotate(TAU / 8) label2 = TextMobject("Boxed text", color=BLACK) label2_bg = SurroundingRectangle(label2, color=BLUE, fill_color=RED, fill_opacity=.5) label2_group = VGroup(label2, label2_bg) label2_group.next_to(label_group, DOWN) label3 = TextMobject("Rainbow") label3.scale(2) label3.set_color_by_gradient(RED, GREEN, BLUE) label3.to_edge(DOWN) self.add(square) self.play(FadeIn(label_group)) self.play(FadeIn(label2_group)) self.play(FadeIn(label3)) self.wait(2)
def __init__(self, **kwargs): super().__init__(**kwargs) body = Cube(side_length=1) for dim, scale_factor in enumerate(self.body_dimensions): body.stretch(scale_factor, dim=dim) body.set_width(self.width) body.set_fill(self.shaded_body_color, opacity=1) body.sort(lambda p: p[2]) body[-1].set_fill(self.body_color) screen_plate = body.copy() keyboard = VGroup(*[ VGroup(*[ Square(**self.key_color_kwargs) for x in range(12 - y % 2) ]).arrange(RIGHT, buff=SMALL_BUFF) for y in range(4) ]).arrange(DOWN, buff=MED_SMALL_BUFF) keyboard.stretch_to_fit_width( self.keyboard_width_to_body_width * body.get_width(), ) keyboard.stretch_to_fit_height( self.keyboard_height_to_body_height * body.get_height(), ) keyboard.next_to(body, OUT, buff=0.1 * SMALL_BUFF) keyboard.shift(MED_SMALL_BUFF * UP) body.add(keyboard) screen_plate.stretch(self.screen_thickness / self.body_dimensions[2], dim=2) screen = Rectangle( stroke_width=0, fill_color=BLACK, fill_opacity=1, ) screen.replace(screen_plate, stretch=True) screen.scale_in_place(self.screen_width_to_screen_plate_width) screen.next_to(screen_plate, OUT, buff=0.1 * SMALL_BUFF) screen_plate.add(screen) screen_plate.next_to(body, UP, buff=0) screen_plate.rotate( self.open_angle, RIGHT, about_point=screen_plate.get_bottom() ) self.screen_plate = screen_plate self.screen = screen axis = Line( body.get_corner(UP + LEFT + OUT), body.get_corner(UP + RIGHT + OUT), color=BLACK, stroke_width=2 ) self.axis = axis self.add(body, screen_plate, axis) self.rotate(5 * np.pi / 12, LEFT, about_point=ORIGIN) self.rotate(np.pi / 6, DOWN, about_point=ORIGIN)
def __init__(self, **kwargs): if not hasattr(self, "args"): self.args = serialize_args([]) if not hasattr(self, "config"): self.config = serialize_config({ **kwargs, }) super().__init__(**kwargs) body = Cube(side_length=1) for dim, scale_factor in enumerate(self.body_dimensions): body.stretch(scale_factor, dim=dim) body.set_width(self.width) body.set_fill(self.shaded_body_color, opacity=1) body.sort(lambda p: p[2]) body[-1].set_fill(self.body_color) screen_plate = body.copy() keyboard = VGroup(*[ VGroup( *[Square(**self.key_color_kwargs) for x in range(12 - y % 2)]).arrange(RIGHT, buff=SMALL_BUFF) for y in range(4) ]).arrange(DOWN, buff=MED_SMALL_BUFF) keyboard.stretch_to_fit_width( self.keyboard_width_to_body_width * body.get_width(), ) keyboard.stretch_to_fit_height( self.keyboard_height_to_body_height * body.get_height(), ) keyboard.next_to(body, OUT, buff=0.1 * SMALL_BUFF) keyboard.shift(MED_SMALL_BUFF * UP) body.add(keyboard) screen_plate.stretch(self.screen_thickness / self.body_dimensions[2], dim=2) screen = Rectangle( stroke_width=0, fill_color=BLACK, fill_opacity=1, ) screen.replace(screen_plate, stretch=True) screen.scale_in_place(self.screen_width_to_screen_plate_width) screen.next_to(screen_plate, OUT, buff=0.1 * SMALL_BUFF) screen_plate.add(screen) screen_plate.next_to(body, UP, buff=0) screen_plate.rotate(self.open_angle, RIGHT, about_point=screen_plate.get_bottom()) self.screen_plate = screen_plate self.screen = screen axis = Line(body.get_corner(UP + LEFT + OUT), body.get_corner(UP + RIGHT + OUT), color=BLACK, stroke_width=2) self.axis = axis self.add(body, screen_plate, axis) self.rotate(5 * np.pi / 12, LEFT, about_point=ORIGIN) self.rotate(np.pi / 6, DOWN, about_point=ORIGIN)
def add_remark(self): nl_text = TextMobject("数轴") nl_arrow = Arrow(ORIGIN, UP).match_height(nl_text) nl_remark = VGroup(nl_arrow, nl_text) nl_remark.scale(0.8) nl_remark.set_color(LIGHT_GREY) nl_remark.arrange_submobjects(RIGHT, buff = 0.1) nl_remark.next_to(self.axes.coords_to_point(0, 0), DOWN, buff = 0.1) nl_remark.to_edge(LEFT, buff = 0.15) frac_remark = TextMobject("圆内分数为圆心横坐标") frac_remark.scale(0.6) frac_remark.to_corner(DL, buff = 0.15) farey_sum_remark = TexMobject( "\\text{Farey Sum: }", "\\dfrac{a}{b} \\oplus \\dfrac{c}{d}", "=", "\\dfrac{a+c}{b+d}" ) farey_sum_remark[1].set_color(YELLOW) farey_sum_remark[-1].set_color(PINK) farey_sum_remark.to_corner(DR, buff = 0.15) self.add(nl_remark, frac_remark, farey_sum_remark)
def scroll_through_patrons(self): logo_box = Square(side_length=2.5) logo_box.to_corner(DOWN + LEFT, buff=MED_LARGE_BUFF) total_width = FRAME_X_RADIUS - logo_box.get_right()[0] black_rect = Rectangle( fill_color=BLACK, fill_opacity=1, stroke_width=3, stroke_color=BLACK, width=FRAME_WIDTH, height=0.6 * FRAME_HEIGHT, ) black_rect.to_edge(UP, buff=0) line = DashedLine(FRAME_X_RADIUS * LEFT, FRAME_X_RADIUS * RIGHT) line.move_to(ORIGIN) thanks = TextMobject(self.thanks_words) thanks.scale(0.9) thanks.next_to(black_rect.get_bottom(), UP, SMALL_BUFF) thanks.set_color(YELLOW) underline = Line(LEFT, RIGHT) underline.match_width(thanks) underline.scale(1.1) underline.next_to(thanks, DOWN, SMALL_BUFF) thanks.add(underline) changed_patron_names = map( self.modify_patron_name, self.specific_patrons, ) patrons = VGroup(*map( TextMobject, changed_patron_names, )) patrons.scale(self.patron_scale_val) for patron in patrons: if patron.get_width() > self.max_patron_width: patron.set_width(self.max_patron_width) columns = VGroup(*[ VGroup(*patrons[i::self.n_patron_columns]) for i in range(self.n_patron_columns) ]) for column in columns: for n, name in enumerate(column): name.shift(n * self.name_y_spacing * DOWN) columns.arrange( RIGHT, buff=LARGE_BUFF, aligned_edge=UP, ) max_width = FRAME_WIDTH - 1 if columns.get_width() > max_width: columns.set_width(max_width) underline.match_width(columns) # thanks.to_edge(RIGHT, buff=MED_SMALL_BUFF) columns.next_to(underline, DOWN, buff=2) columns.generate_target() columns.target.to_edge(DOWN, buff=2) vect = columns.target.get_center() - columns.get_center() distance = get_norm(vect) wait_time = 20 always_shift( columns, direction=normalize(vect), rate=(distance / wait_time) ) self.add(columns, black_rect, line, thanks) self.wait(wait_time)
def construct(self): # Add title title = self.title = TextMobject("Clicky Stuffs") title.scale(1.5) title.to_edge(UP, buff=MED_SMALL_BUFF) pi_creatures = VGroup(Randolph(), Mortimer()) for pi, vect in zip(pi_creatures, [LEFT, RIGHT]): pi.set_height(title.get_height()) pi.change_mode("thinking") pi.look(DOWN) pi.next_to(title, vect, buff=MED_LARGE_BUFF) self.add(title, pi_creatures) # Set the top of the screen logo_box = Square(side_length=2.5) logo_box.to_corner(DOWN + LEFT, buff=MED_LARGE_BUFF) black_rect = Rectangle( fill_color=BLACK, fill_opacity=1, stroke_width=3, stroke_color=BLACK, width=FRAME_WIDTH, height=0.6 * FRAME_HEIGHT, ) black_rect.to_edge(UP, buff=0) line = DashedLine(FRAME_X_RADIUS * LEFT, FRAME_X_RADIUS * RIGHT) line.move_to(ORIGIN) # Add thanks thanks = TextMobject(self.thanks_words) thanks.scale(0.9) thanks.next_to(black_rect.get_bottom(), UP, SMALL_BUFF) thanks.set_color(YELLOW) underline = Line(LEFT, RIGHT) underline.match_width(thanks) underline.scale(1.1) underline.next_to(thanks, DOWN, SMALL_BUFF) thanks.add(underline) # Build name list file_name = os.path.join(get_directories()["data"], "patrons.txt") with open(file_name, "r") as fp: names = [ self.modify_patron_name(name.strip()) for name in fp.readlines() ] if self.randomize_order: random.shuffle(names) else: names.sort() name_labels = VGroup(*map(TextMobject, names)) name_labels.scale(self.patron_scale_val) for label in name_labels: if label.get_width() > self.max_patron_width: label.set_width(self.max_patron_width) columns = VGroup(*[ VGroup(*name_labels[i::self.n_patron_columns]) for i in range(self.n_patron_columns) ]) column_x_spacing = 0.5 + max([c.get_width() for c in columns]) for i, column in enumerate(columns): for n, name in enumerate(column): name.shift(n * self.name_y_spacing * DOWN) name.align_to(ORIGIN, LEFT) column.move_to(i * column_x_spacing * RIGHT, UL) columns.center() max_width = FRAME_WIDTH - 1 if columns.get_width() > max_width: columns.set_width(max_width) underline.match_width(columns) columns.next_to(underline, DOWN, buff=3) # Set movement columns.generate_target() distance = columns.get_height() + 2 wait_time = self.scroll_time frame = self.camera.frame frame_shift = ApplyMethod( frame.shift, distance * DOWN, run_time=wait_time, rate_func=linear, ) blink_anims = [] blank_mob = Mobject() for x in range(wait_time): if random.random() < 0.25: blink_anims.append(Blink(random.choice(pi_creatures))) else: blink_anims.append(Animation(blank_mob)) blinks = Succession(*blink_anims) static_group = VGroup(black_rect, line, thanks, pi_creatures, title) static_group.fix_in_frame() self.add(columns, static_group) self.play(frame_shift, blinks)
def construct_infinite_sum(self): for n in range(1, 11): # Get highlighted terms highlighted_terms = self.get_highlighted_terms(n) # Update highlight rectangles if n == 1: rects = self.get_highlight_rectangles(1) self.play(ShowCreation(rects), lag_ratio=0.2) self.wait() else: new_rects = self.get_highlight_rectangles(n) self.play(Transform(rects, new_rects)) if n <= 4: self.wait() # Show the detailed construction of the first four terms if n <= 4: # Make copies of the elements that are going to be moved highlighted_terms_copy = self.get_highlighted_terms( n).deepcopy() times_symbols_copy = self.get_times_symbols().deepcopy() cdots_copy = self.get_cdots_symbol().deepcopy() # Move highlighted terms into position arranged_terms_list = [] for i in range(4): arranged_terms_list.append(highlighted_terms_copy[i]) arranged_terms_list.append(times_symbols_copy[i]) arranged_terms_list.append(cdots_copy) arranged_terms = VGroup(*arranged_terms_list) arranged_terms.arrange_submobjects(RIGHT, buff=0.2) arranged_terms.next_to(self.sum_tex, UP, aligned_edge=RIGHT, buff=0.5) # Move highlighted terms into position anims_list = [] for i in range(4): anims_list.append( ReplacementTransform( self.get_highlighted_terms(n)[i].deepcopy(), arranged_terms[2 * i], lag_ratio=0, run_time=2)) anims_list.append( ReplacementTransform( self.get_times_symbols()[i].deepcopy(), arranged_terms[2 * i + 1], lag_ratio=0, run_time=2)) anims_list.append( ReplacementTransform(self.get_cdots_symbol().deepcopy(), arranged_terms[-1], lag_ratio=0, run_time=2)) self.play(AnimationGroup(*anims_list)) self.wait() if n == 1: self.play(Transform(arranged_terms, self.get_sum_term(n))) else: self.play( Transform(arranged_terms, self.get_sum_term(n)), Write(self.get_plus_symbol(n - 1)), ) self.wait() # And show the result for the remaining terms else: self.play( Transform( VGroup( self.get_highlighted_terms(n).deepcopy(), self.get_times_symbols().deepcopy(), self.get_cdots_symbol().deepcopy(), ), self.get_sum_term(n), lag_ratio=0, ), Write(self.get_plus_symbol(n - 1)), ) # Add \cdots to the end. self.wait() self.play(FadeOut(rects), Write(self.sum_tex[-1][-4:])) self.wait()
def construct(self): self._main_title() text_one = TextMobject("Given a list of items sold") text_two: TextMobject = TextMobject( "Randomly choose items matching this distribution") text_two.next_to(text_one, DOWN) number_line = NumberLine( numbers_with_elongated_ticks=[0, 1], include_numbers=True, x_min=0, x_max=1, unit_size=10, tick_frequency=0.1, # decimal_number_config={"num_decimal_places": 1}, numbers_to_show=[0, 1]) number_line.next_to(text_two, UP) self.play(ShowCreation(text_one)) self.wait() self.play(ShowCreation(text_two)) self.wait(4) apples_text = TextMobject("Apples:") apples_text.set_color(self._apple_colour) apples_text.to_edge(UP) apples_text.align_to(text_two, LEFT) apple_count_text = TextMobject(f"{self._apple_count}") apple_count_text.set_color(self._apple_colour) apple_count_text.next_to(apples_text, RIGHT) banana_text = TextMobject("Bananas:") banana_text.set_color(self._banana_colour) banana_text.next_to(apples_text, DOWN) banana_text.align_to(apples_text, LEFT) banana_count_text = TextMobject(f"{self._banana_count}") banana_count_text.set_color(self._banana_colour) banana_count_text.next_to(banana_text, RIGHT) self.play(Transform(text_one, apples_text)) self.play(ShowCreation(apple_count_text)) self.play(ShowCreation(banana_text), ShowCreation(banana_count_text)) banana_bar = Rectangle( height=0.4, width=number_line.point_to_number(self._banana_fraction * 10) * (number_line.number_to_point(1)[0]), color=self._banana_colour, fill_color=self._banana_colour, fill_opacity=0.75) banana_bar.next_to(banana_count_text, RIGHT + RIGHT) apple_bar = Rectangle( height=0.4, width=number_line.point_to_number(self._apple_fraction * 10) * (number_line.number_to_point(1)[0]), color=self._apple_colour, fill_color=self._apple_colour, fill_opacity=0.75) apple_bar.next_to(banana_bar, UP) apple_bar.align_to(banana_bar, LEFT) self.play(FadeIn(apple_bar), FadeIn(banana_bar)) self.wait(1.5) apple_fraction_text = TextMobject("$\\frac{" + str(self._apple_count) + "}{" + str(self._apple_count + self._banana_count) + "} = " + str(self._apple_fraction) + "$") apple_fraction_text.next_to(apple_bar, RIGHT) banana_fraction_text = TextMobject("$\\frac{" + str(self._banana_count) + "}{" + str(self._apple_count + self._banana_count) + "} = " + str(self._banana_fraction) + "$") banana_fraction_text.next_to(banana_bar, RIGHT) self.play(ShowCreation(apple_fraction_text)) self.play(ShowCreation(banana_fraction_text)) self.wait(2) number_line_map_text = TextMobject( "Map these counts to values between 0 and 1") number_line_map_text.next_to(text_two, UP) self.play(ShowCreation(number_line_map_text)) self.wait(3) self.play(Transform(number_line_map_text, number_line)) apple_num_ln_bar = Rectangle( height=0.4, # width=1 - self._apple_fraction * (number_line.number_to_point(1)[0]), width=number_line.point_to_number(self._apple_fraction * 10) * (number_line.number_to_point(1)[0]), color=self._apple_colour, fill_color=self._apple_colour, fill_opacity=0.25) apple_num_ln_bar.move_to(apple_bar, LEFT) self.add(apple_num_ln_bar) self.wait(2) self.play( ApplyMethod(apple_num_ln_bar.move_to, number_line.number_to_point(0), LEFT)) banana_num_ln_bar = Rectangle( height=0.4, width=number_line.point_to_number(self._banana_fraction * 10) * (number_line.number_to_point(1)[0]), color=self._banana_colour, fill_color=self._banana_colour, fill_opacity=0.25) banana_num_ln_bar.move_to(banana_bar, LEFT) self.add(banana_num_ln_bar) self.wait(2) self.play( ApplyMethod(banana_num_ln_bar.move_to, number_line.number_to_point(1), RIGHT)) text_scale: float = 0.75 get_rnd_full = TextMobject( "Get a random number $n$ between 0 and 1 (uniform distribution)") get_apple_text = TextMobject( f"Apple\\quad if $n <= {self._apple_fraction}$", tex_to_color_map={"Apple": self._apple_colour}) get_banana_text = TextMobject( f"Banana\\quad if $n > {self._apple_fraction}$", tex_to_color_map={"Banana": self._banana_colour}) get_rnd_full.scale(text_scale) get_rnd_full.next_to(text_two, DOWN) get_banana_text.next_to(get_apple_text, DOWN) step_group = VGroup(get_apple_text, get_banana_text) brace = Brace(step_group, LEFT) step_text_d = brace.get_text("$n \\sim U(0, 1)$") step_text_d.scale(text_scale) step_text_d.next_to(get_rnd_full, DOWN + DOWN) step_text_d.shift(LEFT) brace.next_to(step_text_d, RIGHT) step_group.scale(text_scale) step_group.next_to(step_text_d, RIGHT + RIGHT + RIGHT) self.wait(2) self.play(ShowCreation(get_rnd_full)) self.wait(2) self.play(ShowCreation(step_text_d)) self.wait(2) self.play(GrowFromCenter(brace)) self.wait() self.play(ShowCreation(get_apple_text)) self.wait(2) self.play(ShowCreation(get_banana_text)) # random_nos_to_draw = 10 # main_arrow = Arrow(ORIGIN, DOWN * 1.3) # helper_arrow = Arrow(ORIGIN, LEFT * 1.3) # # for i in range(random_nos_to_draw): # num: float = np.random.random_sample(1) # point = number_line.number_to_point(num) # arrow_colour = self._apple_colour if num <= self._apple_fraction else self._banana_colour # arrow_recipient = get_apple_text if num <= self._apple_fraction else get_banana_text # # main_arrow.set_color(arrow_colour) # # if i == 0: # main_arrow.next_to(point, UP) # helper_arrow.next_to(arrow_recipient, RIGHT) # self.play(GrowArrow(main_arrow), GrowArrow(helper_arrow)) # else: # self.play(ApplyMethod(helper_arrow.next_to, arrow_recipient, RIGHT), # ApplyMethod(main_arrow.next_to, point, UP)) # self.wait() # # self.play(FadeOut(main_arrow), FadeOut(helper_arrow)) self.wait()
def scroll_through_patrons(self): logo_box = Square(side_length=2.5) logo_box.to_corner(DOWN + LEFT, buff=MED_LARGE_BUFF) total_width = FRAME_X_RADIUS - logo_box.get_right()[0] black_rect = Rectangle( fill_color=BLACK, fill_opacity=1, stroke_width=3, stroke_color=BLACK, width=FRAME_WIDTH, height=0.6 * FRAME_HEIGHT, ) black_rect.to_edge(UP, buff=0) line = DashedLine(FRAME_X_RADIUS * LEFT, FRAME_X_RADIUS * RIGHT) line.move_to(ORIGIN) thanks = TextMobject(self.thanks_words) thanks.scale(0.9) thanks.next_to(black_rect.get_bottom(), UP, SMALL_BUFF) thanks.set_color(YELLOW) underline = Line(LEFT, RIGHT) underline.match_width(thanks) underline.scale(1.1) underline.next_to(thanks, DOWN, SMALL_BUFF) thanks.add(underline) changed_patron_names = map( self.modify_patron_name, self.specific_patrons, ) patrons = VGroup(*map( TextMobject, changed_patron_names, )) patrons.scale(self.patron_scale_val) for patron in patrons: if patron.get_width() > self.max_patron_width: patron.set_width(self.max_patron_width) columns = VGroup(*[ VGroup(*patrons[i::self.n_patron_columns]) for i in range(self.n_patron_columns) ]) for column in columns: for n, name in enumerate(column): name.shift(n * self.name_y_spacing * DOWN) columns.arrange( RIGHT, buff=LARGE_BUFF, aligned_edge=UP, ) if columns.get_width() > self.max_patron_width: columns.set_width(total_width - 1) thanks.to_edge(RIGHT, buff=MED_SMALL_BUFF) columns.next_to(underline, DOWN, buff=2) columns.generate_target() columns.target.to_edge(DOWN, buff=2) vect = columns.target.get_center() - columns.get_center() distance = get_norm(vect) wait_time = 20 always_shift( columns, direction=normalize(vect), rate=(distance / wait_time) ) self.add(columns, black_rect, line, thanks) self.wait(wait_time)