Beispiel #1
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, Tex):
            if len(label) == 1:
                label = "\\vec{\\textbf{%s}}" % label
            label = Tex(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 == "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 #2
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 = Tex(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 #3
0
def get_det_text(
    matrix: Matrix,
    determinant: int | str | None = None,
    background_rect: bool = False,
    initial_scale_factor: int = 2
) -> VGroup:
    parens = Tex("(", ")")
    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 = TexText("det")
    det.scale(initial_scale_factor)
    det.next_to(l_paren, LEFT, buff=0.1)
    if background_rect:
        det.add_background_rectangle()
    det_text = VGroup(det, l_paren, r_paren)
    if determinant is not None:
        eq = Tex("=")
        eq.next_to(r_paren, RIGHT, buff=0.1)
        result = Tex(str(determinant))
        result.next_to(eq, RIGHT, buff=0.2)
        det_text.add(eq, result)
    return det_text
Beispiel #4
0
 def add_brackets(self):
     bracket_pair = Tex("\\big[", "\\big]")
     bracket_pair.scale(2)
     bracket_pair.stretch_to_fit_height(
         self.get_height() + 2 * self.bracket_v_buff
     )
     l_bracket, r_bracket = bracket_pair.split()
     l_bracket.next_to(self, LEFT, self.bracket_h_buff)
     r_bracket.next_to(self, RIGHT, self.bracket_h_buff)
     self.add(l_bracket, r_bracket)
     self.brackets = VGroup(l_bracket, r_bracket)
     return self
Beispiel #5
0
 def get_number_mob(self, num):
     result = VGroup()
     place = 0
     max_place = self.max_place
     while place < max_place:
         digit = Tex(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 #6
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 = Tex(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 #7
0
    def construct(self):
        self.setup_axes(animate=True)
        # Example
        #wtdata = [[0.2,0.3],[0.4,0.5],[0.4,0.7],[0.6,0.7]]
        #data = [[0.1,0.2],[0.2,0.3],[0.6,0.6],[0.8,0.5]]
        #label = [0,0,1,1]
        #new_data = [0.5, 0.2]
        # And gate
        #wtdata = [[0.0,0.9],[0.0,0.7],[0.0,0.5],[0.2,0.7],[0.2,0.5]]
        #data = [[0,0],[0,1],[1,0],[1,1]]
        #label = [0,0,0,1]

        # Or gate
        wtdata = [[0.2,0.3],[0.4,0.5],[0.6,0.7]]
        data = [[0,0],[0,1],[1,0],[1,1]]
        label = [0,1,1,1]
        c = [RED_E, GREEN]
        for i,(x,y) in enumerate(zip(data,label)):
            d = Dot().move_to(self.coords_to_point(x[0], x[1])).set_color(c[y])
            t = Tex('({},{})'.format(x[0],x[1])).next_to(d, LEFT+DOWN*0.2)
            self.add(d)
            self.add(t.scale(0.6))
        
        self.w = wtdata[0]
        gt = self.get_graph(self.line_func)
        eq1 = Tex('{} x_1 + {} x_2 = 0.6'.format(self.w[0],self.w[1])).to_corner(LEFT+DOWN)
        self.play(Write(eq1))
        self.play(ShowCreation(gt))
        for wt in wtdata[1:]:
            self.w = wt
            g = self.get_graph(self.line_func)
            eq = Tex('{} x_1 + {} x_2 = 0.6'.format(wt[0],wt[1])).to_corner(LEFT+DOWN)
            self.wait(1)
            self.play(Transform(gt,g), Transform(eq1,eq))


        #d = Dot().move_to(self.coords_to_point(new_data[0], new_data[1])).set_color(YELLOW)
        #self.add(d)
        #true = Tex('True').next_to(gt,UP*0.1).set_color(GREEN)
        #false = Tex('False').next_to(gt,DOWN*0.8).set_color(RED)
        #self.add(true, false)
        self.wait(2)