示例#1
0
 def generate_points(self):
     numer = TexMobject(self.p)
     denom = TexMobject(self.q)
     line = Rectangle(height = 0.02)
     line.set_width(max(numer.get_width(), denom.get_width()) * 1.1, stretch = True)
     self.add(numer, line, denom)
     self.arrange_submobjects(DOWN, buff = 0.15)
     self.numer = numer
     self.line = line
     self.denom = denom
示例#2
0
 def add_label(self):
     circle = self.circle
     label = TexMobject(
         "%d" % int(np.round(1. / self.radius)),
         background_stroke_width=0,
     )
     h_factor = circle.get_width() * 0.6 / label.get_width()
     v_factor = circle.get_height() * 0.5 / label.get_height()
     factor = np.min([h_factor, v_factor])
     label.scale(factor)
     label.move_to(self.center)
     self.add(label)
     self.label = label
示例#3
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
示例#4
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
示例#5
0
    def get_table(tabledict: dict,
                  buff_length=0.3,
                  line_color=WHITE,
                  text_color=WHITE):
        def flatten(inlist):
            outlist = []
            for element in inlist:
                for sub_element in element:
                    outlist.append(sub_element)
            return outlist

        table = VGroup(
        )  #The table is a VGroup with all the fields, records and separators.

        fields = list(tabledict.keys(
        ))  #Since the data is recieved as a dict, the keys will be the fields

        cell_length = TexMobject(
            max(fields + flatten(tabledict.values()), key=len)
        ).get_width(
        ) + 2 * buff_length  #The length of a record/field of max length is the base cell size
        cell_height = TexMobject(
            max(fields + flatten(tabledict.values()),
                key=len)).get_height() + 2 * buff_length

        #The first position is set like so.
        field_position = [
            (cell_length - TexMobject(fields[0]).get_width()) / 2 +
            TexMobject(fields[0]).get_width() / 2, 0, 0
        ]  #The initial position of the first field. This is
        #NOTE: Coordinates of TexMobjects in Manim are taken from centre, not top-right. Adjustments have been made.

        total_table_width = (cell_length -
                             TexMobject(fields[0]).get_width()) / 2

        total_table_height = cell_height * (
            len(tabledict[max(tabledict.keys(), key=len)]) + 1)

        for n in range(len(fields)):

            field = TexMobject(fields[n])

            field_length = field.get_width(
            )  #This is the length that the actual field name will take up on-screen

            if n + 1 < len(
                    fields
            ):  #This gets the nxt field if it exists and chooses an empty TexMobject if it doesn't
                next_field = TexMobject(fields[n + 1])
            else:
                next_field = TexMobject("")

            next_field_length = next_field.get_width(
            )  #Gets the next fields length

            field.move_to(field_position)

            space_to_right_of_field = (cell_length - field_length) / 2

            space_to_left_of_next_field = (cell_length - next_field_length) / 2

            space_to_leave = space_to_right_of_field + space_to_left_of_next_field + next_field_length / 2

            #next_field_length/2 is added to account for the fact that coordinates are taken from centre and not left edges.

            total_table_width += field_length + space_to_leave / 2
            table.add(field)
            field_position = field.get_right() + (space_to_leave, 0, 0)

        for keynum in range(len(tabledict.keys())):
            key = list(tabledict.keys())[keynum]  #gets the actual key
            recordlist = tabledict[key]  #selects the list with the records for

            if recordlist != []:

                record_position = [
                    table[keynum].get_center()[0],
                    -((cell_height - TexMobject(fields[keynum]).get_height()) /
                      2 + TexMobject(fields[keynum]).get_height() / 2 +
                      cell_height), 0
                ]

                #the record position is set to be the [center of the field it belongs to, buffer space above the record + centered height of the record, 0  ]

                for recordnum in range(len(recordlist)):  # for each record for
                    record = TexMobject(
                        recordlist[recordnum])  # the selected field

                    if recordnum + 1 < len(
                            recordlist
                    ):  #This gets the nxt record if it exists and chooses an empty TexMobject if it doesn't
                        next_record = TexMobject(recordlist[recordnum + 1])
                    else:
                        next_record = TexMobject("")

                    record.move_to(record_position)
                    record_position = record.get_center() + (0, -cell_height,
                                                             0)
                    table.add(record)
            else:
                pass

        line_hor = Line(start=(0, -2 * cell_height / 3, 0),
                        end=(total_table_width, -2 * cell_height / 3, 0),
                        color=line_color)
        table.add(line_hor)

        line_hor = Line(start=(0, cell_height / 2, 0),
                        end=(total_table_width, cell_height / 2, 0),
                        color=line_color)
        table.add(line_hor)

        for l in range(len(fields) - 1):
            line = Line(start=(table[l].get_center() +
                               (cell_length / 2, cell_height / 2, 0)),
                        end=(table[l].get_center() +
                             (cell_length / 2, -total_table_height, 0)))
            table.add(line)

        line = Line(start=(table[0].get_center() +
                           (-cell_length / 2, cell_height / 2, 0)),
                    end=(table[0].get_center() +
                         (-cell_length / 2, -total_table_height, 0)))
        table.add(line)

        #line=Line( start=(table[l].get_center()+ (total_table_width,cell_height/2,0)), end =(table[l].get_center()+ (total_table_width,-total_table_height,0)))
        line = Line(start=(total_table_width, cell_height / 2, 0),
                    end=(total_table_width, -total_table_height, 0))
        table.add(line)

        return table