def position_endpoints_on(self, start, end): curr_vect = self.points[-1] - self.points[0] 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)) self.rotate(angle_of_vector(target_vect) - angle_of_vector(curr_vect)) self.shift(start - self.points[0]) return self
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)
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
def look(self, direction): norm = get_norm(direction) if norm == 0: return direction /= norm self.purposeful_looking_direction = direction for pupil, eye in zip(self.pupils.split(), self.eyes.split()): c = eye.get_center() right = eye.get_right() - c up = eye.get_top() - c vect = direction[0] * right + direction[1] * up v_norm = get_norm(vect) p_radius = 0.5 * pupil.get_width() vect *= (v_norm - 0.75 * p_radius) / v_norm pupil.move_to(c + vect) self.pupils[1].align_to(self.pupils[0], DOWN) return self
def __init__(self, car, target_point, **kwargs): assert isinstance(car, Car) ApplyMethod.__init__(self, car.move_to, target_point, **kwargs) displacement = self.target_mobject.get_right() - self.starting_mobject.get_right() distance = get_norm(displacement) if not self.moving_forward: distance *= -1 tire_radius = car.get_tires()[0].get_width() / 2 self.total_tire_radians = -distance / tire_radius
def path_along_arc(arc_angle, axis=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 = 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
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)
def set_submobject_colors_by_radial_gradient(self, center=None, radius=1, inner_color=WHITE, outer_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
def align_to(self, mobject_or_point, direction=ORIGIN, alignment_vect=UP): """ Examples: mob1.align_to(mob2, UP) moves mob1 vertically so that its top edge lines ups with mob2's top edge. mob1.align_to(mob2, alignment_vector = RIGHT) moves mob1 horizontally so that it's center is directly above/below the center of mob2 """ if isinstance(mobject_or_point, Mobject): mob = mobject_or_point target_point = mob.get_critical_point(direction) else: target_point = mobject_or_point direction_norm = get_norm(direction) if direction_norm > 0: alignment_vect = np.array(direction) / direction_norm reference_point = self.get_critical_point(direction) else: reference_point = self.get_center() diff = target_point - reference_point self.shift(alignment_vect * np.dot(diff, alignment_vect)) return self
def func(point): centered = point + vect return radius * centered / get_norm(centered)
def get_direction(self): vect = self.get_tip() - self.get_center() return vect / get_norm(vect)
def is_closed(points): return get_norm(points[0] - points[-1]) < CLOSED_THRESHOLD