mirror of
https://github.com/3b1b/manim.git
synced 2025-08-01 17:29:06 +08:00
Up to vector addition in chapter 1
This commit is contained in:
117
eola/chapter1.py
117
eola/chapter1.py
@ -386,6 +386,9 @@ class DifferentConceptions(Scene):
|
||||
])
|
||||
|
||||
|
||||
class ThreeDVectorField(Scene):
|
||||
pass
|
||||
|
||||
|
||||
class HelpsToHaveOneThought(Scene):
|
||||
def construct(self):
|
||||
@ -515,7 +518,7 @@ class ListsOfNumbersAddOn(Scene):
|
||||
self.dither(2)
|
||||
|
||||
|
||||
class CoordinateSystemWalkthrough(VectorCoordinateScene):
|
||||
class CoordinateSystemWalkthrough(VectorScene):
|
||||
def construct(self):
|
||||
self.introduce_coordinate_plane()
|
||||
self.show_vector_coordinates()
|
||||
@ -626,17 +629,113 @@ class CoordinateSystemWalkthrough(VectorCoordinateScene):
|
||||
self.clear()
|
||||
self.add(*starting_mobjects)
|
||||
|
||||
class LabeledThreeDVector(Scene):
|
||||
pass
|
||||
|
||||
class ThreeAxisLabels(Scene):
|
||||
class WriteZ(Scene):
|
||||
def construct(self):
|
||||
z = TexMobject("z").scale(2)
|
||||
z.show()
|
||||
self.play(Write(z, run_time = 1))
|
||||
z = TexMobject("z").highlight(Z_COLOR)
|
||||
z.scale_to_fit_height(4)
|
||||
self.play(Write(z, run_time = 2))
|
||||
self.dither(3)
|
||||
|
||||
|
||||
class Write3DVector(Scene):
|
||||
def construct(self):
|
||||
array = Matrix([2, 1, 3]).scale(2)
|
||||
x, y, z = array.get_mob_matrix().flatten()
|
||||
brackets = array.get_brackets()
|
||||
x.highlight(X_COLOR)
|
||||
y.highlight(Y_COLOR)
|
||||
z.highlight(Z_COLOR)
|
||||
|
||||
self.add(brackets)
|
||||
for mob in x, y, z:
|
||||
self.play(Write(mob), run_time = 2)
|
||||
self.dither()
|
||||
|
||||
|
||||
class VectorAddition(VectorScene):
|
||||
def construct(self):
|
||||
self.add_plane()
|
||||
self.define_addition()
|
||||
self.answer_why()
|
||||
|
||||
def define_addition(self):
|
||||
v1 = self.add_vector([1, 2])
|
||||
v2 = self.add_vector([3, -1], color = MAROON_B)
|
||||
l1 = self.label_vector(v1, "v")
|
||||
l2 = self.label_vector(v2, "w")
|
||||
self.dither()
|
||||
self.play(ApplyMethod(v2.shift, v1.get_end()))
|
||||
self.dither()
|
||||
v_sum = self.add_vector(v2.get_end(), color = PINK)
|
||||
sum_tex = "\\vec{\\textbf{v}} + \\vec{\\textbf{w}}"
|
||||
self.label_vector(v_sum, sum_tex, rotate = True)
|
||||
self.dither(3)
|
||||
|
||||
def answer_why(self):
|
||||
pass
|
||||
|
||||
|
||||
class ItDoesntMatterWhich(Scene):
|
||||
def construct(self):
|
||||
physy = Physicist()
|
||||
compy = ComputerScientist()
|
||||
physy.title = TextMobject("Physics student").to_corner(DOWN+LEFT)
|
||||
compy.title = TextMobject("CS student").to_corner(DOWN+RIGHT)
|
||||
for pi in physy, compy:
|
||||
pi.next_to(pi.title, UP)
|
||||
self.add(pi, pi.title)
|
||||
compy_speech = compy.get_bubble("speech")
|
||||
physy_speech = physy.get_bubble("speech")
|
||||
arrow = Vector([2, 1])
|
||||
array = matrix_to_mobject([2, 1])
|
||||
goes_to = TexMobject("\\Rightarrow")
|
||||
physy_statement = VMobject(arrow, goes_to, array)
|
||||
physy_statement.arrange_submobjects(RIGHT)
|
||||
compy_statement = physy_statement.copy()
|
||||
compy_statement.arrange_submobjects(LEFT)
|
||||
physy_speech.position_mobject_inside(physy_statement)
|
||||
compy_speech.position_mobject_inside(compy_statement)
|
||||
|
||||
new_arrow = Vector([2, 1])
|
||||
x_line = Line(ORIGIN, 2*RIGHT, color = X_COLOR)
|
||||
y_line = Line(2*RIGHT, 2*RIGHT+UP, color = Y_COLOR)
|
||||
x_mob = TexMobject("2").next_to(x_line, DOWN)
|
||||
y_mob = TexMobject("1").next_to(y_line, RIGHT)
|
||||
new_arrow.add(x_line, y_line, x_mob, y_mob)
|
||||
back_and_forth = VMobject(
|
||||
new_arrow,
|
||||
TexMobject("\\Leftrightarrow"),
|
||||
matrix_to_mobject([2, 1])
|
||||
)
|
||||
back_and_forth.arrange_submobjects(LEFT).center()
|
||||
|
||||
|
||||
self.dither()
|
||||
self.play(
|
||||
ApplyMethod(physy.change_mode, "speaking"),
|
||||
ShowCreation(physy_speech),
|
||||
Write(physy_statement),
|
||||
run_time = 1
|
||||
)
|
||||
self.play(Blink(compy))
|
||||
self.play(
|
||||
ApplyMethod(physy.change_mode, "sassy"),
|
||||
ApplyMethod(compy.change_mode, "speaking"),
|
||||
FadeOut(physy_speech),
|
||||
ShowCreation(compy_speech),
|
||||
Transform(physy_statement, compy_statement, path_arc = np.pi)
|
||||
)
|
||||
self.dither(2)
|
||||
|
||||
|
||||
|
||||
|
||||
self.play(
|
||||
ApplyMethod(physy.change_mode, "pondering"),
|
||||
ApplyMethod(compy.change_mode, "pondering"),
|
||||
Transform(compy_speech, VectorizedPoint(compy_speech.get_tip())),
|
||||
Transform(physy_statement, back_and_forth)
|
||||
)
|
||||
self.dither()
|
||||
|
||||
|
||||
|
||||
|
@ -16,6 +16,7 @@ VECTOR_LABEL_SCALE_VAL = 0.7
|
||||
|
||||
X_COLOR = GREEN_C
|
||||
Y_COLOR = RED_C
|
||||
Z_COLOR = BLUE_D
|
||||
|
||||
def matrix_to_tex_string(matrix):
|
||||
matrix = np.array(matrix).astype("string")
|
||||
@ -326,7 +327,50 @@ class NumericalMatrixMultiplication(Scene):
|
||||
|
||||
|
||||
|
||||
class VectorCoordinateScene(Scene):
|
||||
class VectorScene(Scene):
|
||||
def add_plane(self, animate = False, **kwargs):
|
||||
plane = NumberPlane(**kwargs)
|
||||
if animate:
|
||||
self.play(ShowCreation(plane, submobject_mode = "lagged_start"))
|
||||
self.add(plane)
|
||||
return plane
|
||||
|
||||
def add_vector(self, vector, animate = True, color = YELLOW):
|
||||
arrow = Vector(vector, color = color)
|
||||
if animate:
|
||||
self.play(ShowCreation(arrow, submobject_mode = "one_at_a_time"))
|
||||
self.add(arrow)
|
||||
return arrow
|
||||
|
||||
def label_vector(self, vector, label, animate = True,
|
||||
direction = "left", rotate = False,
|
||||
color = WHITE, add_to_vector = True,
|
||||
buff_factor = 1.5):
|
||||
if len(label) == 1:
|
||||
label = "\\vec{\\textbf{%s}}"%label
|
||||
label = TexMobject(label)
|
||||
label.highlight(color)
|
||||
label.scale(VECTOR_LABEL_SCALE_VAL)
|
||||
if rotate:
|
||||
label.rotate(vector.get_angle())
|
||||
|
||||
vector_vect = vector.get_end() - vector.get_start()
|
||||
if direction is "left":
|
||||
rot_angle = -np.pi/2
|
||||
else:
|
||||
rot_angle = np.pi/2
|
||||
label.shift(-buff_factor*label.get_boundary_point(
|
||||
rotate_vector(vector_vect, rot_angle)
|
||||
))
|
||||
label.shift(vector.get_center())
|
||||
|
||||
if add_to_vector:
|
||||
vector.add(label)
|
||||
if animate:
|
||||
self.play(Write(label, run_time = 1))
|
||||
self.add(label)
|
||||
return label
|
||||
|
||||
def position_x_coordinate(self, x_coord, x_line, vector):
|
||||
x_coord.next_to(x_line, -vector[1]*UP)
|
||||
x_coord.highlight(X_COLOR)
|
||||
|
@ -21,8 +21,10 @@ class VMobject(Mobject):
|
||||
|
||||
## Colors
|
||||
def init_colors(self):
|
||||
if not hasattr(self, "stroke_color"):
|
||||
self.stroke_color = self.color
|
||||
self.set_style_data(
|
||||
stroke_color = self.color,
|
||||
stroke_color = self.stroke_color,
|
||||
stroke_width = self.stroke_width,
|
||||
fill_color = self.fill_color,
|
||||
fill_opacity = self.fill_opacity,
|
||||
|
@ -129,8 +129,9 @@ class Arrow(Line):
|
||||
CONFIG = {
|
||||
"color" : YELLOW_C,
|
||||
"tip_length" : 0.25,
|
||||
"tip_angle" : np.pi/6,
|
||||
"buff" : 0.3,
|
||||
"propogate_style_to_family" : True,
|
||||
"propogate_style_to_family" : False,
|
||||
"preserve_tip_size_when_scaling" : True,
|
||||
}
|
||||
def __init__(self, *args, **kwargs):
|
||||
@ -148,10 +149,16 @@ class Arrow(Line):
|
||||
start, end = end, start
|
||||
vect = -vect
|
||||
tip_points = [
|
||||
end+rotate_vector(vect, u*np.pi/5)
|
||||
end+rotate_vector(vect, u*self.tip_angle)
|
||||
for u in 1, -1
|
||||
]
|
||||
self.tip = VMobject(close_new_points = False)
|
||||
self.tip = VMobject(
|
||||
close_new_points = True,
|
||||
mark_paths_closed = True,
|
||||
fill_color = self.color,
|
||||
fill_opacity = 1,
|
||||
stroke_color = self.color,
|
||||
)
|
||||
self.tip.set_anchor_points(
|
||||
[tip_points[0], end, tip_points[1]],
|
||||
mode = "corners"
|
||||
|
Reference in New Issue
Block a user