def coords_to_vector(self, vector, coords_start=2 * RIGHT + 2 * UP, clean_up=True): starting_mobjects = list(self.mobjects) array = Matrix(vector) array.shift(coords_start) arrow = Vector(vector) x_line = Line(ORIGIN, vector[0] * RIGHT) y_line = Line(x_line.get_end(), arrow.get_end()) x_line.set_color(X_COLOR) y_line.set_color(Y_COLOR) x_coord, y_coord = array.get_mob_matrix().flatten() self.play(Write(array, run_time=1)) self.wait() self.play(ApplyFunction( lambda x: self.position_x_coordinate(x, x_line, vector), x_coord )) self.play(ShowCreation(x_line)) self.play( ApplyFunction( lambda y: self.position_y_coordinate(y, y_line, vector), y_coord ), FadeOut(array.get_brackets()) ) y_coord, brackets = self.get_mobjects_from_last_animation() self.play(ShowCreation(y_line)) self.play(ShowCreation(arrow)) self.wait() if clean_up: self.clear() self.add(*starting_mobjects)
def coords_to_vector(self, vector, coords_start=2 * RIGHT + 2 * UP, clean_up=True): starting_mobjects = list(self.submobjects) array = Matrix(vector) array.shift(coords_start) arrow = Vector(vector) x_line = Line(ORIGIN, vector[0] * RIGHT) y_line = Line(x_line.get_end(), arrow.get_end()) x_line.set_color(X_COLOR) y_line.set_color(Y_COLOR) x_coord, y_coord = array.get_mob_matrix().flatten() self.play(Write(array, run_time=1)) self.wait() self.play( ApplyFunction( lambda x: self.position_x_coordinate(x, x_line, vector), x_coord)) self.play(ShowCreation(x_line)) self.play( ApplyFunction( lambda y: self.position_y_coordinate(y, y_line, vector), y_coord), FadeOut(array.get_brackets())) y_coord, brackets = self.get_mobjects_from_last_animation() self.play(ShowCreation(y_line)) self.play(ShowCreation(arrow)) self.wait() if clean_up: self.clear() self.add(*starting_mobjects)
def vector_to_coords(self, vector, integer_labels=True, clean_up=True): """ This method displays vector as a Vector() based vector, and then shows the corresponding lines that make up the x and y components of the vector. Then, a column matrix (henceforth called the label) is created near the head of the Vector. Parameters ---------- vector Union(np.ndarray, list, tuple) The vector to show. integer_label (bool=True) Whether or not to round the value displayed. in the vector's label to the nearest integer clean_up (bool=True) Whether or not to remove whatever this method did after it's done. """ starting_mobjects = list(self.mobjects) show_creation = False if isinstance(vector, Arrow): arrow = vector vector = arrow.get_end()[:2] else: arrow = Vector(vector) show_creation = True array = vector_coordinate_label(arrow, integer_labels=integer_labels) x_line = Line(ORIGIN, vector[0] * RIGHT) y_line = Line(x_line.get_end(), arrow.get_end()) x_line.set_color(X_COLOR) y_line.set_color(Y_COLOR) x_coord, y_coord = array.get_mob_matrix().flatten() x_coord_start = self.position_x_coordinate(x_coord.copy(), x_line, vector) y_coord_start = self.position_y_coordinate(y_coord.copy(), y_line, vector) brackets = array.get_brackets() if show_creation: self.play(ShowCreation(arrow)) self.play(ShowCreation(x_line), Write(x_coord_start), run_time=1) self.play(ShowCreation(y_line), Write(y_coord_start), run_time=1) self.wait() self.play( Transform(x_coord_start, x_coord, lag_ratio=0), Transform(y_coord_start, y_coord, lag_ratio=0), Write(brackets, run_time=1), ) self.wait() self.remove(x_coord_start, y_coord_start, brackets) self.add(array) if clean_up: self.clear() self.add(*starting_mobjects) return array, x_line, y_line
def coords_to_vector(self, vector, coords_start=2 * RIGHT + 2 * UP, clean_up=True): """ This method writes the vector as a column matrix (henceforth called the label), takes the values in it one by one, and form the corresponding lines that make up the x and y components of the vector. Then, an Vector() based vector is created between the lines on the Screen. Parameters ---------- vector Union(np.ndarray, list, tuple) The vector to show. coords_start Union(np.ndarray,list,tuple) The starting point of the location of the label of the vector that shows it numerically. Defaults to 2 * RIGHT + 2 * UP or (2,2) clean_up (bool=True) Whether or not to remove whatever this method did after it's done. """ starting_mobjects = list(self.mobjects) array = Matrix(vector) array.shift(coords_start) arrow = Vector(vector) x_line = Line(ORIGIN, vector[0] * RIGHT) y_line = Line(x_line.get_end(), arrow.get_end()) x_line.set_color(X_COLOR) y_line.set_color(Y_COLOR) x_coord, y_coord = array.get_mob_matrix().flatten() self.play(Write(array, run_time=1)) self.wait() self.play( ApplyFunction( lambda x: self.position_x_coordinate(x, x_line, vector), x_coord)) self.play(ShowCreation(x_line)) self.play( ApplyFunction( lambda y: self.position_y_coordinate(y, y_line, vector), y_coord), FadeOut(array.get_brackets())) y_coord, brackets = self.get_mobjects_from_last_animation() self.play(ShowCreation(y_line)) self.play(ShowCreation(arrow)) self.wait() if clean_up: self.clear() self.add(*starting_mobjects)
def construct(self): screen_grid = ScreenGrid() self.add(screen_grid) v_1 = np.array([1, 0.4, 0]) v_2 = np.array([1.2, 0.2, 0]) self.play(FadeIn(Vector(v_1, color = BLUE))) self.play(FadeIn(Vector(v_2, color = GREEN))) vectors1 = {} vectors2 = {} for i in range(-15, 15): vectors1[i] = i * v_1 vectors2[i] = i * v_2 self.add(screen_grid, Vector(vectors1[i], color = BLUE, stroke = 0.2)) self.play(ReplacementTransform(Vector(vectors1[i]), Dot(vectors1[i])), run_time = 0.125) self.play(FadeIn(Vector(vectors2[i], color = GREEN, stroke = 0.2), run_time = 0.125)) self.play(ReplacementTransform(Vector(vectors2[i]), Dot(vectors2[i])), run_time = 0.125) vector_sum = {} for j in range (-15, 15): for i in range (-15, 15): vector_sum[(i,j)] = (i * v_1) + (j * v_2) self.play(FadeIn(Vector(vector_sum[(i,j)], color = ORANGE, stroke = 0.2), run_time = 0.125)) self.play(ReplacementTransform(Vector(vector_sum[(i,j)]), Dot(vector_sum[(i,j)])), run_time = 0.125) self.wait(5)
def vector_to_coords(self, vector, integer_labels=True, clean_up=True): starting_mobjects = list(self.mobjects) show_creation = False if isinstance(vector, Arrow): arrow = vector vector = arrow.get_end()[:2] else: arrow = Vector(vector) show_creation = True array = vector_coordinate_label(arrow, integer_labels=integer_labels) x_line = Line(ORIGIN, vector[0] * RIGHT) y_line = Line(x_line.get_end(), arrow.get_end()) x_line.set_color(X_COLOR) y_line.set_color(Y_COLOR) x_coord, y_coord = array.get_mob_matrix().flatten() x_coord_start = self.position_x_coordinate( x_coord.copy(), x_line, vector ) y_coord_start = self.position_y_coordinate( y_coord.copy(), y_line, vector ) brackets = array.get_brackets() if show_creation: self.play(ShowCreation(arrow)) self.play( ShowCreation(x_line), Write(x_coord_start), run_time=1 ) self.play( ShowCreation(y_line), Write(y_coord_start), run_time=1 ) self.wait() self.play( Transform(x_coord_start, x_coord, lag_ratio=0), Transform(y_coord_start, y_coord, lag_ratio=0), Write(brackets, run_time=1), ) self.wait() self.remove(x_coord_start, y_coord_start, brackets) self.add(array) if clean_up: self.clear() self.add(*starting_mobjects) return array, x_line, y_line
def get_basis_vectors(self, i_hat_color=X_COLOR, j_hat_color=Y_COLOR, k_hat_color=Z_COLOR): """ Returns a VGroup of the Basis Vectors (1,0,0)、(0,1,0) and (0,0,1) Parameters ---------- i_hat_color (str) The hex colour to use for the basis vector in the x direction j_hat_color (str) The hex colour to use for the basis vector in the z direction k_hat_color (str) The hex colour to use for the basis vector in the y direction Returns ------- VGroup VGroup of the Vector Mobjects representing the basis vectors. """ return VGroup(*[ Vector( vect, color=color, stroke_width=self.basis_vector_stroke_width) for vect, color in [ ([1, 0, 0], i_hat_color), ([0, 1, 0], j_hat_color), ([0, 0, 1], k_hat_color), ] ])
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
def add_vector(self, vector, color=YELLOW, animate=True, **kwargs): """ Returns the Vector after adding it to the Plane. Parameters ---------- vector Union(Arrow,list,tuple,np.ndarray) It can be a pre-made graphical vector, or the coordinates of one. color (str) The string of the hex color of the vector. This is only taken into consideration if 'vector' is not an Arrow. Defaults to YELLOW. animate (bool=True) Whether or not to animate the addition of the vector by using GrowArrow **kwargs Any valid keyword argument of Arrow. These are only considered if vector is not an Arrow. Returns ------- Arrow The arrow representing the vector. """ if not isinstance(vector, Arrow): vector = Vector(vector, color=color, **kwargs) if animate: self.play(GrowArrow(vector)) self.add(vector) return vector
def add_vector(self, vector, color=YELLOW, animate=True, **kwargs): if not isinstance(vector, Arrow): vector = Vector(vector, color=color, **kwargs) if animate: self.play(GrowArrow(vector)) self.add(vector) return vector
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_basis_vectors(self, i_hat_color=X_COLOR, j_hat_color=Y_COLOR): return VGroup(*[ Vector( vect, color=color, stroke_width=self.basis_vector_stroke_width ) for vect, color in [ ([1, 0], i_hat_color), ([0, 1], j_hat_color) ] ])
def construct(self): plane = NumberPlane() plane.add(plane.get_axis_labels()) self.add(plane) points = [ x * RIGHT + y * UP for x in np.arange(-5, 5, 1) for y in np.arange(-5, 5, 1) ] vec_field = [] for point in points: field = 0.5 * RIGHT + 0.5 * UP result = Vector(field).shift(point) vec_field.append(result) draw_field = VGroup(*vec_field) self.play(ShowCreation(draw_field)) self.wait(2)
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
def get_vector_movement(self, func): """ This method returns an animation that moves a mobject in "self.moving_vectors" to its corresponding .target value. func is a function that determines where the .target goes. Parameters ---------- func (function) The function that determines where the .target of the moving mobject goes. Returns ------- Animation The animation of the movement. """ 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 construct(self): self.phase = 0 point_x, point_y = self.comp_point(0) self.make_axes() self.ellipse = VMobject(color=self.path_color) self.phi = TexMobject(r"\Delta \Phi = ").set_color(BLACK).shift(4 * RIGHT + 2 * UP) self.phi_0 = TexMobject("0").set_color(BLACK).next_to(self.phi, RIGHT) self.phi_pi_2 = TexMobject(r"\frac{\pi}{2}").set_color(BLACK).next_to( self.phi, RIGHT).shift(0.06 * DOWN) self.phi_3_pi_4 = TexMobject(r"\frac{3 \pi}{4}").set_color( BLACK).next_to(self.phi, RIGHT) self.x_vector = Vector(point_x * RIGHT, color=self.axes_vector_color, **self.vector_args) self.y_vector = Vector(point_y * UP, color=self.axes_vector_color, **self.vector_args) self.xy_vector = Vector(point_x * RIGHT + point_y * UP, color=self.xy_vector_color, **self.vector_args) update_group = VGroup( self.ellipse, self.xy_vector, self.x_vector, self.y_vector, ) self.wait(1.6) self.add(self.ellipse) self.play(FadeIn(self.x_vector)) # self.wait(1) self.play(FadeIn(self.y_vector)) self.wait(4) self.play(FadeIn(self.xy_vector)) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) self.play( UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear)) self.play( AnimationGroup(UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear), AnimationGroup(FadeIn(self.phi), FadeIn(self.phi_0)), lag_ratio=0.7)) self.play( UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear)) self.x_vector.set_color(BLACK) self.play(UpdateFromAlphaFunc(update_group, self.do_90_phase_shift, rate_func=linear), FadeOut(self.ellipse), FadeOut(self.phi_0), FadeIn(self.phi_pi_2), run_time=self.period / 4) self.phase = np.pi / 2 self.ellipse.set_points([]) self.x_vector.set_color(self.axes_vector_color) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) for _ in range(3): self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_only_period), run_time=self.period, rate_func=linear) self.x_vector.set_color(BLACK) self.play(UpdateFromAlphaFunc(update_group, self.do_45_phase_shift, rate_func=linear), FadeOut(self.ellipse), FadeOut(self.phi_pi_2), FadeIn(self.phi_3_pi_4), run_time=self.period / 8) self.phase = 3 * np.pi / 4 self.ellipse.set_points([]) self.x_vector.set_color(self.axes_vector_color) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) self.play( AnimationGroup(UpdateFromAlphaFunc( update_group, self.do_vectors_only_period_hide, rate_func=linear, run_time=self.period), AnimationGroup(FadeOut(self.phi_3_pi_4), FadeOut(self.phi)), lag_ratio=0.7)) self.play(FadeOut(self.axes), FadeOut(self.axes_labels)) self.wait()
def calc_field(self, point): x, y = point[:2] Rx, Ry = self.point_charge_loc[:2] r = math.sqrt((x - Rx)**2 + (y - Ry)**2) efield = (point - self.point_charge_loc) / r**3 return Vector(efield).shift(point)
class Polarization(Scene): CONFIG = { "period": 6, "camera_config": { "background_color": Color("white") }, "axes_scale": 0.9, "vector_length": 2, "vector_args": { "max_stroke_width_to_length_ratio": 10000, "max_tip_length_to_length_ratio": 0.95, }, "xy_line_stroke_width": DEFAULT_STROKE_WIDTH / 2, "xy_line_color": Color("blue"), "path_color": Color("red"), "axes_color": BLACK, "axes_vector_color": Color("green"), "xy_vector_color": Color("blue"), } def construct(self): self.phase = 0 point_x, point_y = self.comp_point(0) self.make_axes() self.ellipse = VMobject(color=self.path_color) self.phi = TexMobject(r"\Delta \Phi = ").set_color(BLACK).shift(4 * RIGHT + 2 * UP) self.phi_0 = TexMobject("0").set_color(BLACK).next_to(self.phi, RIGHT) self.phi_pi_2 = TexMobject(r"\frac{\pi}{2}").set_color(BLACK).next_to( self.phi, RIGHT).shift(0.06 * DOWN) self.phi_3_pi_4 = TexMobject(r"\frac{3 \pi}{4}").set_color( BLACK).next_to(self.phi, RIGHT) self.x_vector = Vector(point_x * RIGHT, color=self.axes_vector_color, **self.vector_args) self.y_vector = Vector(point_y * UP, color=self.axes_vector_color, **self.vector_args) self.xy_vector = Vector(point_x * RIGHT + point_y * UP, color=self.xy_vector_color, **self.vector_args) update_group = VGroup( self.ellipse, self.xy_vector, self.x_vector, self.y_vector, ) self.wait(1.6) self.add(self.ellipse) self.play(FadeIn(self.x_vector)) # self.wait(1) self.play(FadeIn(self.y_vector)) self.wait(4) self.play(FadeIn(self.xy_vector)) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) self.play( UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear)) self.play( AnimationGroup(UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear), AnimationGroup(FadeIn(self.phi), FadeIn(self.phi_0)), lag_ratio=0.7)) self.play( UpdateFromAlphaFunc(update_group, self.do_vectors_only_period, run_time=self.period, rate_func=linear)) self.x_vector.set_color(BLACK) self.play(UpdateFromAlphaFunc(update_group, self.do_90_phase_shift, rate_func=linear), FadeOut(self.ellipse), FadeOut(self.phi_0), FadeIn(self.phi_pi_2), run_time=self.period / 4) self.phase = np.pi / 2 self.ellipse.set_points([]) self.x_vector.set_color(self.axes_vector_color) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) for _ in range(3): self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_only_period), run_time=self.period, rate_func=linear) self.x_vector.set_color(BLACK) self.play(UpdateFromAlphaFunc(update_group, self.do_45_phase_shift, rate_func=linear), FadeOut(self.ellipse), FadeOut(self.phi_pi_2), FadeIn(self.phi_3_pi_4), run_time=self.period / 8) self.phase = 3 * np.pi / 4 self.ellipse.set_points([]) self.x_vector.set_color(self.axes_vector_color) self.play(UpdateFromAlphaFunc(update_group, self.do_vectors_and_ellipse_period), run_time=self.period, rate_func=linear) self.play( AnimationGroup(UpdateFromAlphaFunc( update_group, self.do_vectors_only_period_hide, rate_func=linear, run_time=self.period), AnimationGroup(FadeOut(self.phi_3_pi_4), FadeOut(self.phi)), lag_ratio=0.7)) self.play(FadeOut(self.axes), FadeOut(self.axes_labels)) self.wait() def make_axes(self): self.axes = Axes(x_min=-self.axes_scale * FRAME_X_RADIUS, x_max=self.axes_scale * FRAME_X_RADIUS, y_min=-self.axes_scale * FRAME_Y_RADIUS, y_max=self.axes_scale * FRAME_Y_RADIUS, x_axis_config={"include_ticks": False}, y_axis_config={"include_ticks": False}, number_line_config={"color": self.axes_color}) self.axes_labels = self.axes.get_axis_labels( x_label_tex="E_x", y_label_tex="E_y").set_color(self.axes_color) self.play( AnimationGroup(GrowFromCenter(self.axes), FadeIn(self.axes_labels), lag_ratio=0.4)) def do_vectors_and_ellipse_period(self, _, alpha): point_x, point_y = self.comp_point(alpha) self.add_point_to_ellipse(point_x, point_y) self.set_vector_positions(point_x, point_y) def do_vectors_only_period(self, _, alpha): point_x, point_y = self.comp_point(alpha) self.set_vector_positions(point_x, point_y) def do_90_phase_shift(self, _, alpha): alpha = interpolate(0, 1 / 4, alpha) x_point, y_point = self.comp_point(alpha) self.y_vector.put_start_and_end_on(ORIGIN, y_point * UP) self.xy_vector.put_start_and_end_on(ORIGIN, y_point * UP + 2 * RIGHT) def do_45_phase_shift(self, _, alpha): alpha = interpolate(0, 1 / 8, alpha) x_point, y_point = self.comp_point(alpha) self.y_vector.put_start_and_end_on(ORIGIN, y_point * UP) self.xy_vector.put_start_and_end_on(ORIGIN, y_point * UP + 2 * RIGHT) def set_vector_positions(self, x, y): self.x_vector.put_start_and_end_on(ORIGIN, x * RIGHT) self.y_vector.put_start_and_end_on(ORIGIN, y * UP) self.xy_vector.put_start_and_end_on(ORIGIN, x * RIGHT + y * UP) def add_point_to_ellipse(self, x, y): if not len(self.ellipse.get_points()): self.ellipse.set_points([UP * y + RIGHT * x]) else: self.ellipse.add_points_as_corners([UP * y + RIGHT * x]) def do_vectors_only_period_hide(self, _, alpha): fade_ratio = 3 if alpha >= (fade_ratio - 1) / fade_ratio: opacity = 1 - smooth(fade_ratio * alpha - (fade_ratio - 1)) self.x_vector.set_opacity(opacity) self.y_vector.set_opacity(opacity) self.xy_vector.set_opacity(opacity) self.ellipse.set_stroke(opacity=opacity) self.do_vectors_only_period(_, alpha) def comp_point(self, alpha): point_x = self.vector_length * np.cos( 2 * np.pi * alpha) * RIGHT + 0.01 * RIGHT point_y = self.vector_length * np.cos(2 * np.pi * alpha + self.phase) * UP + 0.01 * UP return point_x, point_y