Ejemplo n.º 1
0
 def get_vector_movement(self, func):
     for v in self.moving_vectors:
         v.target = Vector(func(v.get_end()), color=v.get_color())
         norm = get_norm(v.target.get_end())
         if norm < 0.1:
             v.target.get_tip().scale_in_place(norm)
     return self.get_piece_movement(self.moving_vectors)
Ejemplo n.º 2
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=consts.SMALL_BUFF)
        else:
            angle = vector.get_angle()
            if not rotate:
                label.rotate(-angle, about_point=consts.ORIGIN)
            if direction == "left":
                label.shift(-label.get_bottom() + 0.1 * consts.UP)
            else:
                label.shift(-label.get_top() + 0.1 * consts.DOWN)
            label.rotate(angle, about_point=consts.ORIGIN)
            label.shift((vector.get_end() - vector.get_start()) / 2)
        return label
Ejemplo n.º 3
0
 def add_line(self, start, end, color=None):
     start, end = list(map(np.array, [start, end]))
     length = get_norm(end - start)
     if length == 0:
         points = [start]
     else:
         epsilon = self.epsilon / length
         points = [
             interpolate(start, end, t) for t in np.arange(0, 1, epsilon)
         ]
     self.add_points(points, color=color)
Ejemplo n.º 4
0
 def put_start_and_end_on(self, start, end):
     curr_start, curr_end = self.get_start_and_end()
     if np.all(curr_start == curr_end):
         # TODO, any problems with resetting
         # these attrs?
         self.start = start
         self.end = end
         self.generate_points()
     curr_vect = curr_end - curr_start
     if np.all(curr_vect == 0):
         raise Exception("Cannot position endpoints of closed loop")
     target_vect = end - start
     self.scale(
         get_norm(target_vect) / get_norm(curr_vect),
         about_point=curr_start,
     )
     self.rotate(angle_of_vector(target_vect) - angle_of_vector(curr_vect),
                 about_point=curr_start)
     self.shift(start - curr_start)
     return self
Ejemplo n.º 5
0
def path_along_arc(arc_angle, axis=consts.OUT):
    """
    If vect is vector from start to end, [vect[:,1], -vect[:,0]] is
    perpendicular to vect in the left direction.
    """
    if abs(arc_angle) < STRAIGHT_PATH_THRESHOLD:
        return straight_path
    if get_norm(axis) == 0:
        axis = consts.OUT
    unit_axis = axis / get_norm(axis)

    def path(start_points, end_points, alpha):
        vects = end_points - start_points
        centers = start_points + 0.5 * vects
        if arc_angle != np.pi:
            centers += np.cross(unit_axis, vects / 2.0) / np.tan(arc_angle / 2)
        rot_matrix = rotation_matrix(alpha * arc_angle, unit_axis)
        return centers + np.dot(start_points - centers, rot_matrix.T)

    return path
Ejemplo n.º 6
0
    def __init__(self, func, **kwargs):
        VGroup.__init__(self, **kwargs)
        self.func = func
        dt = self.dt

        start_points = self.get_start_points(
            **self.start_points_generator_config
        )
        for point in start_points:
            points = [point]
            for t in np.arange(0, self.virtual_time, dt):
                last_point = points[-1]
                points.append(last_point + dt * func(last_point))
                if get_norm(last_point) > self.cutoff_norm:
                    break
            line = VMobject()
            step = max(1, int(len(points) / self.n_anchors_per_line))
            line.set_points_smoothly(points[::step])
            self.add(line)

        self.set_stroke(self.stroke_color, self.stroke_width)

        if self.color_by_arc_length:
            len_to_rgb = get_rgb_gradient_function(
                self.min_arc_length,
                self.max_arc_length,
                colors=self.colors,
            )
            for line in self:
                arc_length = line.get_arc_length()
                rgb = len_to_rgb([arc_length])[0]
                color = rgb_to_color(rgb)
                line.set_color(color)
        elif self.color_by_magnitude:
            image_file = get_color_field_image_file(
                lambda p: get_norm(func(p)),
                min_value=self.min_magnitude,
                max_value=self.max_magnitude,
                colors=self.colors,
            )
            self.color_using_background_image(image_file)
Ejemplo n.º 7
0
 def begin(self):
     super().begin()
     car = self.mobject
     distance = get_norm(
         op.sub(
             self.target_mobject.get_right(),
             self.starting_mobject.get_right(),
         ))
     if not self.moving_forward:
         distance *= -1
     tire_radius = car.get_tires()[0].get_width() / 2
     self.total_tire_radians = -distance / tire_radius
Ejemplo n.º 8
0
    def update_path(self):
        new_point = self.traced_point_func()
        if self.has_no_points():
            self.start_new_path(new_point)
            self.add_line_to(new_point)
        else:
            # Set the end to be the new point
            self.points[-1] = new_point

            # Second to last point
            nppcc = self.n_points_per_cubic_curve
            dist = get_norm(new_point - self.points[-nppcc])
            if dist >= self.min_distance_to_new_point:
                self.add_line_to(new_point)
Ejemplo n.º 9
0
def get_3d_vmob_unit_normal(vmob, point_index):
    n_points = vmob.get_num_points()
    if len(vmob.get_anchors()) <= 2:
        return np.array(consts.consts.UP)
    i = point_index
    im3 = i - 3 if i > 2 else (n_points - 4)
    ip3 = i + 3 if i < (n_points - 3) else 3
    unit_normal = get_unit_normal(
        vmob.points[ip3] - vmob.points[i],
        vmob.points[im3] - vmob.points[i],
    )
    if get_norm(unit_normal) == 0:
        return np.array(consts.consts.UP)
    return unit_normal
Ejemplo n.º 10
0
 def get_vector(self, point, **kwargs):
     output = np.array(self.func(point))
     norm = get_norm(output)
     if norm == 0:
         output *= 0
     else:
         output *= self.length_func(norm) / norm
     vector_config = dict(self.vector_config)
     vector_config.update(kwargs)
     vect = Vector(output, **vector_config)
     vect.shift(point)
     fill_color = rgb_to_color(
         self.rgb_gradient_function(np.array([norm]))[0]
     )
     vect.set_color(fill_color)
     return vect
Ejemplo n.º 11
0
    def set_submobject_colors_by_radial_gradient(
            self,
            center=None,
            radius=1,
            inner_color=Color('WHITE'),
            outer_color=Color('BLACK')):
        if center is None:
            center = self.get_center()

        for mob in self.family_members_with_points():
            t = get_norm(mob.get_center() - center) / radius
            t = min(t, 1)
            mob_color = interpolate_color(inner_color, outer_color, t)
            mob.set_color(mob_color, family=False)

        return self
Ejemplo n.º 12
0
 def get_length(self):
     return get_norm(self.get_vector())
Ejemplo n.º 13
0
 def get_length(self):
     start, end = self.get_start_and_end()
     return get_norm(start - end)
Ejemplo n.º 14
0
 def get_direction(self):
     vect = self.get_tip() - self.get_center()
     return vect / get_norm(vect)