Beispiel #1
0
    def update_mobject(self, alpha):
        new_tex = self.tex_list[np.ceil(alpha * len(self.tex_list)) - 1]

        if new_tex != self.curr_tex:
            self.curr_tex = new_tex
            self.mobject = TexMobject(new_tex).shift(self.start_center)
        if not all(self.start_center == self.end_center):
            self.mobject.center().shift(
                (1 - alpha) * self.start_center + alpha * self.end_center
            )
Beispiel #2
0
 def generate_sea_of_zeros(self):
     zero = TexMobject("0")
     self.sea_of_zeros = []
     for n in range(self.nrows):
         for a in range((self.nrows - n) / 2 + 1):
             for k in (n + a + 1, -a - 1):
                 self.coords.append((n, k))
                 mob = zero.copy()
                 mob.shift(self.coords_to_center(n, k))
                 self.coords_to_mobs[n][k] = mob
                 self.add(mob)
     return self
Beispiel #3
0
    def get_coordinate_labels(self, *numbers):
        # TODO: Should merge this with the code from NumberPlane.get_coordinate_labels

        result = VGroup()
        if len(numbers) == 0:
            numbers = list(range(-int(self.x_radius), int(self.x_radius) + 1))
            numbers += [
                complex(0, y) for y in range(-int(self.y_radius),
                                             int(self.y_radius) + 1)
            ]
        for number in numbers:
            if number == complex(0, 0):
                continue
            point = self.number_to_point(number)
            num_str = str(number).replace("j", "i")
            if num_str.startswith("0"):
                num_str = "0"
            elif num_str in ["1i", "-1i"]:
                num_str = num_str.replace("1", "")
            num_mob = TexMobject(num_str)
            num_mob.add_background_rectangle()
            num_mob.set_height(self.written_coordinate_height)
            num_mob.next_to(point, DOWN + LEFT, SMALL_BUFF)
            result.add(num_mob)
        self.coordinate_labels = result
        return result
Beispiel #4
0
    def __init__(self, mobject, direction=DOWN, **kwargs):
        digest_config(self, kwargs, locals())
        angle = -np.arctan2(*direction[:2]) + np.pi
        mobject.rotate(-angle, about_point=ORIGIN)
        left = mobject.get_corner(DOWN + LEFT)
        right = mobject.get_corner(DOWN + RIGHT)
        target_width = right[0] - left[0]

        # Adding int(target_width) qquads gives approximately the right width
        num_quads = np.clip(int(self.width_multiplier * target_width),
                            self.min_num_quads, self.max_num_quads)
        tex_string = "\\underbrace{%s}" % (num_quads * "\\qquad")
        TexMobject.__init__(self, tex_string, **kwargs)
        self.tip_point_index = np.argmin(self.get_all_points()[:, 1])
        self.stretch_to_fit_width(target_width)
        self.shift(left - self.get_corner(UP + LEFT) + self.buff * DOWN)
        for mob in mobject, self:
            mob.rotate(angle, about_point=ORIGIN)
Beispiel #5
0
    def count_mobjects(self,
                       mobjects,
                       mode="highlight",
                       color="red",
                       display_numbers=True,
                       num_offset=DEFAULT_COUNT_NUM_OFFSET,
                       run_time=DEFAULT_COUNT_RUN_TIME):
        """
        Note, leaves final number mobject as "number" attribute

        mode can be "highlight", "show_creation" or "show", otherwise
        a warning is given and nothing is animating during the count
        """
        if len(mobjects) > 50:  #TODO
            raise Exception("I don't know if you should be counting \
                             too many mobjects...")
        if len(mobjects) == 0:
            raise Exception("Counting mobject list of length 0")
        if mode not in ["highlight", "show_creation", "show"]:
            raise Warning("Unknown mode")
        frame_time = run_time / len(mobjects)
        if mode == "highlight":
            self.add(*mobjects)
        for mob, num in zip(mobjects, it.count(1)):
            if display_numbers:
                num_mob = TexMobject(str(num))
                num_mob.center().shift(num_offset)
                self.add(num_mob)
            if mode == "highlight":
                original_color = mob.color
                mob.set_color(color)
                self.wait(frame_time)
                mob.set_color(original_color)
            if mode == "show_creation":
                self.play(ShowCreation(mob, run_time=frame_time))
            if mode == "show":
                self.add(mob)
                self.wait(frame_time)
            if display_numbers:
                self.remove(num_mob)
        if display_numbers:
            self.add(num_mob)
            self.number = num_mob
        return self
Beispiel #6
0
class FlipThroughSymbols(Animation):
    CONFIG = {
        "start_center": ORIGIN,
        "end_center": ORIGIN,
    }

    def __init__(self, tex_list, **kwargs):
        mobject = TexMobject(self.curr_tex).shift(start_center)
        Animation.__init__(self, mobject, **kwargs)

    def update_mobject(self, alpha):
        new_tex = self.tex_list[np.ceil(alpha * len(self.tex_list)) - 1]

        if new_tex != self.curr_tex:
            self.curr_tex = new_tex
            self.mobject = TexMobject(new_tex).shift(self.start_center)
        if not all(self.start_center == self.end_center):
            self.mobject.center().shift(
                (1 - alpha) * self.start_center + alpha * self.end_center
            )
Beispiel #7
0
 def count_regions(self,
                   regions,
                   mode="one_at_a_time",
                   num_offset=DEFAULT_COUNT_NUM_OFFSET,
                   run_time=DEFAULT_COUNT_RUN_TIME,
                   **unused_kwargsn):
     if mode not in ["one_at_a_time", "show_all"]:
         raise Warning("Unknown mode")
     frame_time = run_time / (len(regions))
     for region, count in zip(regions, it.count(1)):
         num_mob = TexMobject(str(count))
         num_mob.center().shift(num_offset)
         self.add(num_mob)
         self.set_color_region(region)
         self.wait(frame_time)
         if mode == "one_at_a_time":
             self.reset_background()
         self.remove(num_mob)
     self.add(num_mob)
     self.number = num_mob
     return self
Beispiel #8
0
 def get_result_matrix(self, left, right):
     (m, k), n = left.shape, right.shape[1]
     mob_matrix = np.array([VGroup()]).repeat(m * n).reshape((m, n))
     for a in range(m):
         for b in range(n):
             template = "(%s)(%s)" if self.use_parens else "%s%s"
             parts = [
                 prefix + template % (left[a][c], right[c][b])
                 for c in range(k) for prefix in ["" if c == 0 else "+"]
             ]
             mob_matrix[a][b] = TexMobject(parts, next_to_buff=0.1)
     return Matrix(mob_matrix)
Beispiel #9
0
 def add_brackets(self):
     bracket_pair = TexMobject("\\big[ \\big]")
     bracket_pair.scale(2)
     bracket_pair.stretch_to_fit_height(self.get_height() + 0.5)
     l_bracket, r_bracket = bracket_pair.split()
     l_bracket.next_to(self, LEFT)
     r_bracket.next_to(self, RIGHT)
     self.add(l_bracket, r_bracket)
     self.brackets = VGroup(l_bracket, r_bracket)
     return self
Beispiel #10
0
    def generate_points(self):
        start_angle = np.pi / 2 + self.arc_angle / 2
        end_angle = np.pi / 2 - self.arc_angle / 2
        self.add(Arc(start_angle=start_angle, angle=-self.arc_angle))
        tick_angle_range = np.linspace(start_angle, end_angle, self.num_ticks)
        for index, angle in enumerate(tick_angle_range):
            vect = rotate_vector(RIGHT, angle)
            tick = Line((1 - self.tick_length) * vect, vect)
            label = TexMobject(str(10 * index))
            label.set_height(self.tick_length)
            label.shift((1 + self.tick_length) * vect)
            self.add(tick, label)

        needle = Polygon(LEFT,
                         UP,
                         RIGHT,
                         stroke_width=0,
                         fill_opacity=1,
                         fill_color=self.needle_color)
        needle.stretch_to_fit_width(self.needle_width)
        needle.stretch_to_fit_height(self.needle_height)
        needle.rotate(start_angle - np.pi / 2, about_point=ORIGIN)
        self.add(needle)
        self.needle = needle

        self.center_offset = self.get_center()
Beispiel #11
0
    def add_axes(self):
        x_axis = Line(self.tick_width * LEFT / 2, self.width * RIGHT)
        y_axis = Line(MED_LARGE_BUFF * DOWN, self.height * UP)
        ticks = VGroup()
        heights = np.linspace(0, self.height, self.n_ticks + 1)
        values = np.linspace(0, self.max_value, self.n_ticks + 1)
        for y, value in zip(heights, values):
            tick = Line(LEFT, RIGHT)
            tick.set_width(self.tick_width)
            tick.move_to(y * UP)
            ticks.add(tick)
        y_axis.add(ticks)

        self.add(x_axis, y_axis)
        self.x_axis, self.y_axis = x_axis, y_axis

        if self.label_y_axis:
            labels = VGroup()
            for tick, value in zip(ticks, values):
                label = TexMobject(str(np.round(value, 2)))
                label.set_height(self.y_axis_label_height)
                label.next_to(tick, LEFT, SMALL_BUFF)
                labels.add(label)
            self.y_axis_labels = labels
            self.add(labels)
Beispiel #12
0
 def get_number_mob(self, num):
     result = VGroup()
     place = 0
     max_place = self.max_place
     while place < max_place:
         digit = TexMobject(str(self.get_place_num(num, place)))
         if place >= len(self.digit_place_colors):
             self.digit_place_colors += self.digit_place_colors
         digit.set_color(self.digit_place_colors[place])
         digit.scale(self.num_scale_factor)
         digit.next_to(result, LEFT, buff=SMALL_BUFF, aligned_edge=DOWN)
         result.add(digit)
         place += 1
     return result
Beispiel #13
0
    def get_subdivision_braces_and_labels(self,
                                          parts,
                                          labels,
                                          direction,
                                          buff=SMALL_BUFF,
                                          min_num_quads=1):
        label_mobs = VGroup()
        braces = VGroup()
        for label, part in zip(labels, parts):
            brace = Brace(part,
                          direction,
                          min_num_quads=min_num_quads,
                          buff=buff)
            if isinstance(label, Mobject):
                label_mob = label
            else:
                label_mob = TexMobject(label)
                label_mob.scale(self.default_label_scale_val)
            label_mob.next_to(brace, direction, buff)

            braces.add(brace)
            label_mobs.add(label_mob)
        parts.braces = braces
        parts.labels = label_mobs
        parts.label_kwargs = {
            "labels": label_mobs.copy(),
            "direction": direction,
            "buff": buff,
        }
        return VGroup(parts.braces, parts.labels)
Beispiel #14
0
    def add_T_label(self,
                    x_val,
                    side=RIGHT,
                    label=None,
                    color=WHITE,
                    animated=False,
                    **kwargs):
        triangle = RegularPolygon(n=3, start_angle=np.pi / 2)
        triangle.set_height(MED_SMALL_BUFF)
        triangle.move_to(self.coords_to_point(x_val, 0), UP)
        triangle.set_fill(color, 1)
        triangle.set_stroke(width=0)
        if label == None:
            T_label = TexMobject(self.variable_point_label, fill_color=color)
        else:
            T_label = TexMobject(label, fill_color=color)

        T_label.next_to(triangle, DOWN)
        v_line = self.get_vertical_line_to_graph(x_val,
                                                 self.v_graph,
                                                 color=YELLOW)

        if animated:
            self.play(DrawBorderThenFill(triangle), ShowCreation(v_line),
                      Write(T_label, run_time=1), **kwargs)

        if np.all(side == LEFT):
            self.left_T_label_group = VGroup(T_label, triangle)
            self.left_v_line = v_line
            self.add(self.left_T_label_group, self.left_v_line)
        elif np.all(side == RIGHT):
            self.right_T_label_group = VGroup(T_label, triangle)
            self.right_v_line = v_line
            self.add(self.right_T_label_group, self.right_v_line)
Beispiel #15
0
 def get_mobs_from_terms(self, start_terms, end_terms):
     """
     Need to ensure that all image mobjects for a tex expression
     stemming from the same string are point-for-point copies of one
     and other.  This makes transitions much smoother, and not look
     like point-clouds.
     """
     num_start_terms = len(start_terms)
     all_mobs = np.array(
         TexMobject(start_terms).split() +
         TexMobject(end_terms).split()
     )
     all_terms = np.array(start_terms + end_terms)
     for term in set(all_terms):
         matches = all_terms == term
         if sum(matches) > 1:
             base_mob = all_mobs[list(all_terms).index(term)]
             all_mobs[matches] = [
                 base_mob.copy().replace(target_mob)
                 for target_mob in all_mobs[matches]
             ]
     return all_mobs[:num_start_terms], all_mobs[num_start_terms:]
Beispiel #16
0
 def get_axis_labels(self, x_label="x", y_label="y"):
     x_axis, y_axis = self.get_axes().split()
     quads = [
         (x_axis, x_label, UP, RIGHT),
         (y_axis, y_label, RIGHT, UP),
     ]
     labels = VGroup()
     for axis, tex, vect, edge in quads:
         label = TexMobject(tex)
         label.add_background_rectangle()
         label.next_to(axis, vect)
         label.to_edge(edge)
         labels.add(label)
     self.axis_labels = labels
     return labels
Beispiel #17
0
    def setup(self):
        self.dots = VGroup()
        self.number = 0
        self.max_place = 0
        self.number_mob = VGroup(TexMobject(str(self.number)))
        self.number_mob.scale(self.num_scale_factor)
        self.number_mob.shift(self.num_start_location)

        self.dot_templates = []
        self.dot_template_iterators = []
        self.curr_configurations = []

        self.arrows = VGroup()

        self.add(self.number_mob)
Beispiel #18
0
 def get_coordinate_labels(self, x_vals=None, y_vals=None):
     coordinate_labels = VGroup()
     if x_vals is None:
         x_vals = list(range(-int(self.x_radius), int(self.x_radius) + 1))
     if y_vals is None:
         y_vals = list(range(-int(self.y_radius), int(self.y_radius) + 1))
     for index, vals in enumerate([x_vals, y_vals]):
         num_pair = [0, 0]
         for val in vals:
             if val == 0:
                 continue
             num_pair[index] = val
             point = self.coords_to_point(*num_pair)
             num = TexMobject(str(val))
             num.add_background_rectangle()
             num.set_height(self.written_coordinate_height)
             num.next_to(point, DOWN + LEFT, buff=SMALL_BUFF)
             coordinate_labels.add(num)
     self.coordinate_labels = coordinate_labels
     return coordinate_labels
Beispiel #19
0
 def generate_n_choose_k_mobs(self):
     self.coords_to_n_choose_k = {}
     for n, k in self.coords:
         nck_mob = TexMobject(r"{%d \choose %d}" % (n, k))
         scale_factor = min(
             1,
             self.portion_to_fill * self.cell_height / nck_mob.get_height(),
             self.portion_to_fill * self.cell_width / nck_mob.get_width(),
         )
         center = self.coords_to_mobs[n][k].get_center()
         nck_mob.center().scale(scale_factor).shift(center)
         if n not in self.coords_to_n_choose_k:
             self.coords_to_n_choose_k[n] = {}
         self.coords_to_n_choose_k[n][k] = nck_mob
     return self
Beispiel #20
0
 def get_graph_label(
     self,
     graph,
     label="f(x)",
     x_val=None,
     direction=RIGHT,
     buff=MED_SMALL_BUFF,
     color=None,
 ):
     label = TexMobject(label)
     color = color or graph.get_color()
     label.set_color(color)
     if x_val is None:
         # Search from right to left
         for x in np.linspace(self.x_max, self.x_min, 100):
             point = self.input_to_graph_point(x, graph)
             if point[1] < FRAME_Y_RADIUS:
                 break
         x_val = x
     label.next_to(self.input_to_graph_point(x_val, graph),
                   direction,
                   buff=buff)
     label.shift_onto_screen()
     return label
Beispiel #21
0
    def add_bars(self, values):
        buff = float(self.width) / (2 * len(values) + 1)
        bars = VGroup()
        for i, value in enumerate(values):
            bar = Rectangle(
                height=(value / self.max_value) * self.height,
                width=buff,
                stroke_width=self.bar_stroke_width,
                fill_opacity=self.bar_fill_opacity,
            )
            bar.move_to((2 * i + 1) * buff * RIGHT, DOWN + LEFT)
            bars.add(bar)
        bars.set_color_by_gradient(*self.bar_colors)

        bar_labels = VGroup()
        for bar, name in zip(bars, self.bar_names):
            label = TexMobject(str(name))
            label.scale(self.bar_label_scale_val)
            label.next_to(bar, DOWN, SMALL_BUFF)
            bar_labels.add(label)

        self.add(bars, bar_labels)
        self.bars = bars
        self.bar_labels = bar_labels
Beispiel #22
0
    def get_vector_label(self,
                         vector,
                         label,
                         at_tip=False,
                         direction="left",
                         rotate=False,
                         color=None,
                         label_scale_factor=VECTOR_LABEL_SCALE_FACTOR):
        if not isinstance(label, TexMobject):
            if len(label) == 1:
                label = "\\vec{\\textbf{%s}}" % label
            label = TexMobject(label)
            if color is None:
                color = vector.get_color()
            label.set_color(color)
        label.scale(label_scale_factor)
        label.add_background_rectangle()

        if at_tip:
            vect = vector.get_vector()
            vect /= get_norm(vect)
            label.next_to(vector.get_end(), vect, buff=SMALL_BUFF)
        else:
            angle = vector.get_angle()
            if not rotate:
                label.rotate(-angle, about_point=ORIGIN)
            if direction is "left":
                label.shift(-label.get_bottom() + 0.1 * UP)
            else:
                label.shift(-label.get_top() + 0.1 * DOWN)
            label.rotate(angle, about_point=ORIGIN)
            label.shift((vector.get_end() - vector.get_start()) / 2)
        return label
Beispiel #23
0
    def get_secant_slope_group(
        self,
        x,
        graph,
        dx=None,
        dx_line_color=None,
        df_line_color=None,
        dx_label=None,
        df_label=None,
        include_secant_line=True,
        secant_line_color=None,
        secant_line_length=10,
    ):
        """
        Resulting group is of the form VGroup(
            dx_line,
            df_line,
            dx_label, (if applicable)
            df_label, (if applicable)
            secant_line, (if applicable)
        )
        with attributes of those names.
        """
        kwargs = locals()
        kwargs.pop("self")
        group = VGroup()
        group.kwargs = kwargs

        dx = dx or float(self.x_max - self.x_min) / 10
        dx_line_color = dx_line_color or self.default_input_color
        df_line_color = df_line_color or graph.get_color()

        p1 = self.input_to_graph_point(x, graph)
        p2 = self.input_to_graph_point(x + dx, graph)
        interim_point = p2[0] * RIGHT + p1[1] * UP

        group.dx_line = Line(p1, interim_point, color=dx_line_color)
        group.df_line = Line(interim_point, p2, color=df_line_color)
        group.add(group.dx_line, group.df_line)

        labels = VGroup()
        if dx_label is not None:
            group.dx_label = TexMobject(dx_label)
            labels.add(group.dx_label)
            group.add(group.dx_label)
        if df_label is not None:
            group.df_label = TexMobject(df_label)
            labels.add(group.df_label)
            group.add(group.df_label)

        if len(labels) > 0:
            max_width = 0.8 * group.dx_line.get_width()
            max_height = 0.8 * group.df_line.get_height()
            if labels.get_width() > max_width:
                labels.set_width(max_width)
            if labels.get_height() > max_height:
                labels.set_height(max_height)

        if dx_label is not None:
            group.dx_label.next_to(group.dx_line,
                                   np.sign(dx) * DOWN,
                                   buff=group.dx_label.get_height() / 2)
            group.dx_label.set_color(group.dx_line.get_color())

        if df_label is not None:
            group.df_label.next_to(group.df_line,
                                   np.sign(dx) * RIGHT,
                                   buff=group.df_label.get_height() / 2)
            group.df_label.set_color(group.df_line.get_color())

        if include_secant_line:
            secant_line_color = secant_line_color or self.default_derivative_color
            group.secant_line = Line(p1, p2, color=secant_line_color)
            group.secant_line.scale_in_place(secant_line_length /
                                             group.secant_line.get_length())
            group.add(group.secant_line)

        return group
Beispiel #24
0
 def organize_matrices(self, left, right, result):
     equals = TexMobject("=")
     everything = VGroup(left, right, equals, result)
     everything.arrange_submobjects()
     everything.set_width(FRAME_WIDTH - 1)
     self.add(everything)
Beispiel #25
0
 def __init__(self, tex_list, **kwargs):
     mobject = TexMobject(self.curr_tex).shift(start_center)
     Animation.__init__(self, mobject, **kwargs)
Beispiel #26
0
def matrix_to_mobject(matrix):
    return TexMobject(matrix_to_tex_string(matrix))
Beispiel #27
0
 def get_tex(self, *tex, **kwargs):
     tex_mob = TexMobject(*tex)
     self.put_at_tip(tex_mob, **kwargs)
     return tex_mob
Beispiel #28
0
def get_det_text(matrix,
                 determinant=None,
                 background_rect=True,
                 initial_scale_factor=2):
    parens = TexMobject(["(", ")"])
    parens.scale(initial_scale_factor)
    parens.stretch_to_fit_height(matrix.get_height())
    l_paren, r_paren = parens.split()
    l_paren.next_to(matrix, LEFT, buff=0.1)
    r_paren.next_to(matrix, RIGHT, buff=0.1)
    det = TextMobject("det")
    det.scale(initial_scale_factor)
    det.next_to(l_paren, LEFT, buff=0.1)
    if background_rect:
        det.add_background_rectangle()
    det_text = VMobject(det, l_paren, r_paren)
    if determinant is not None:
        eq = TexMobject("=")
        eq.next_to(r_paren, RIGHT, buff=0.1)
        result = TexMobject(str(determinant))
        result.next_to(eq, RIGHT, buff=0.2)
        det_text.add(eq, result)
    return det_text