本文整理汇总了Python中mobject.vectorized_mobject.VMobject.add方法的典型用法代码示例。如果您正苦于以下问题:Python VMobject.add方法的具体用法?Python VMobject.add怎么用?Python VMobject.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobject.vectorized_mobject.VMobject
的用法示例。
在下文中一共展示了VMobject.add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_braces
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
def add_braces(self):
a = self.i_hat.get_end()[0]*RIGHT
b = self.j_hat.get_end()[0]*RIGHT
c = self.i_hat.get_end()[1]*UP
d = self.j_hat.get_end()[1]*UP
quads = [
(ORIGIN, a, DOWN, "a"),
(a, a+b, DOWN, "b"),
(a+b, a+b+c, RIGHT, "c"),
(a+b+c, a+b+c+d, RIGHT, "d"),
(a+b+c+d, a+c+d, UP, "a"),
(a+c+d, d+c, UP, "b"),
(d+c, d, LEFT, "c"),
(d, ORIGIN, LEFT, "d"),
]
everyone = VMobject()
for p1, p2, direction, char in quads:
line = Line(p1, p2)
brace = Brace(line, direction, buff = 0)
text = brace.get_text(char)
text.add_background_rectangle()
if char in ["a", "c"]:
text.highlight(X_COLOR)
else:
text.highlight(Y_COLOR)
everyone.add(brace, text)
self.play(Write(everyone), run_time = 1)
示例2: construct
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
def construct(self):
self.setup()
blob = Blob(
height = self.blob_height,
random_seed = 5,
random_nudge_size = 0.2,
)
blob.next_to(ORIGIN, UP+RIGHT)
self.add_transformable_mobject(blob)
arange = np.arange(
0, self.blob_height + self.square_size,
self.square_size
)
square = Square(side_length = self.square_size)
square.set_stroke(YELLOW, width = 2)
square.set_fill(YELLOW, opacity = 0.3)
squares = VMobject()
for x, y in it.product(*[arange]*2):
point = x*RIGHT + y*UP
if blob.probably_contains(point):
squares.add(square.copy().shift(point))
self.play(ShowCreation(
squares, submobject_mode = "lagged_start",
run_time = 2,
))
self.add_transformable_mobject(squares)
self.dither()
self.apply_transposed_matrix([[1, -1], [0.5, 1]])
self.dither()
示例3: add_polygons
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
def add_polygons(self):
a = self.i_hat.get_end()[0]*RIGHT
b = self.j_hat.get_end()[0]*RIGHT
c = self.i_hat.get_end()[1]*UP
d = self.j_hat.get_end()[1]*UP
shapes_colors_and_tex = [
(Polygon(ORIGIN, a, a+c), TEAL, "bd/2"),
(Polygon(ORIGIN, d+b, d), TEAL, "\\dfrac{bd}{2}"),
(Polygon(a+c, a+b+c, a+b+c+d), MAROON, "\\dfrac{ac}{2}"),
(Polygon(b+d, a+b+c+d, b+c+d), MAROON, "ac/2"),
(Polygon(a, a+b, a+b+c, a+c), PINK, "bc"),
(Polygon(d, d+b, d+b+c, d+c), PINK, "bc"),
]
everyone = VMobject()
for shape, color, tex in shapes_colors_and_tex:
shape.set_stroke(width = 0)
shape.set_fill(color = color, opacity = 0.7)
tex_mob = TexMobject(tex)
tex_mob.scale(0.7)
tex_mob.move_to(shape.get_center_of_mass())
everyone.add(shape, tex_mob)
self.play(FadeIn(
everyone,
submobject_mode = "lagged_start",
run_time = 1
))
示例4: add_lines
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
def add_lines(self, left, right):
line_kwargs = {
"color" : BLUE,
"stroke_width" : 2,
}
left_rows = [
VMobject(*row) for row in left.get_mob_matrix()
]
h_lines = VMobject()
for row in left_rows[:-1]:
h_line = Line(row.get_left(), row.get_right(), **line_kwargs)
h_line.next_to(row, DOWN, buff = left.v_buff/2.)
h_lines.add(h_line)
right_cols = [
VMobject(*col) for col in np.transpose(right.get_mob_matrix())
]
v_lines = VMobject()
for col in right_cols[:-1]:
v_line = Line(col.get_top(), col.get_bottom(), **line_kwargs)
v_line.next_to(col, RIGHT, buff = right.h_buff/2.)
v_lines.add(v_line)
self.play(ShowCreation(h_lines))
self.play(ShowCreation(v_lines))
self.dither()
self.show_frame()
示例5: get_det_text
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
def get_det_text(matrix, determinant = None):
parens = TexMobject(["(", ")"])
parens.scale(2)
parens.stretch_to_fit_height(matrix.get_height())
l_paren, r_paren = parens.split()
l_paren.next_to(matrix, LEFT, buff = 0.1)
r_paren.next_to(matrix, RIGHT, buff = 0.1)
det = TextMobject("det").next_to(l_paren, LEFT, buff = 0.1)
det.add_background_rectangle()
det_text = VMobject(det, l_paren, r_paren)
if determinant is not None:
eq = TexMobject("=")
eq.next_to(r_paren, RIGHT, buff = 0.1)
result = TexMobject(str(determinant))
result.next_to(eq, RIGHT, buff = 0.2)
det_text.add(eq, result)
return det_text
示例6: NumberLine
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
class NumberLine(VMobject):
CONFIG = {
"color" : BLUE,
"x_min" : -SPACE_WIDTH,
"x_max" : SPACE_WIDTH,
"unit_size" : 1,
"tick_size" : 0.1,
"tick_frequency" : 1,
"leftmost_tick" : None, #Defaults to ceil(x_min)
"numbers_with_elongated_ticks" : [0],
"numbers_to_show" : None,
"longer_tick_multiple" : 2,
"number_at_center" : 0,
"propogate_style_to_family" : True
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
if self.leftmost_tick is None:
self.leftmost_tick = np.ceil(self.x_min)
VMobject.__init__(self, **kwargs)
def generate_points(self):
self.main_line = Line(self.x_min*RIGHT, self.x_max*RIGHT)
self.tick_marks = VMobject()
self.add(self.main_line, self.tick_marks)
for x in self.get_tick_numbers():
self.add_tick(x, self.tick_size)
for x in self.numbers_with_elongated_ticks:
self.add_tick(x, self.longer_tick_multiple*self.tick_size)
self.stretch(self.unit_size, 0)
self.shift(-self.number_to_point(self.number_at_center))
def add_tick(self, x, size):
self.tick_marks.add(Line(
x*RIGHT+size*DOWN,
x*RIGHT+size*UP,
))
return self
def get_tick_marks(self):
return self.tick_marks
def get_tick_numbers(self):
epsilon = 0.001
return np.arange(
self.leftmost_tick, self.x_max+epsilon,
self.tick_frequency
)
def number_to_point(self, number):
alpha = float(number-self.x_min)/(self.x_max - self.x_min)
return interpolate(
self.main_line.get_start(),
self.main_line.get_end(),
alpha
)
def point_to_number(self, point):
left_point, right_point = self.main_line.get_start_and_end()
full_vect = right_point-left_point
def distance_from_left(p):
return np.dot(p-left_point, full_vect)/np.linalg.norm(full_vect)
return interpolate(
self.x_min, self.x_max,
distance_from_left(point)/distance_from_left(right_point)
)
def default_numbers_to_display(self):
if self.numbers_to_show is not None:
return self.numbers_to_show
return np.arange(self.leftmost_tick, self.x_max, 1)
def get_vertical_number_offset(self, direction = DOWN):
return 4*direction*self.tick_size
def get_number_mobjects(self, *numbers, **kwargs):
#TODO, handle decimals
if len(numbers) == 0:
numbers = self.default_numbers_to_display()
result = VGroup()
for number in numbers:
mob = TexMobject(str(int(number)))
mob.scale_to_fit_height(3*self.tick_size)
mob.shift(
self.number_to_point(number),
self.get_vertical_number_offset(**kwargs)
)
result.add(mob)
return result
def add_numbers(self, *numbers, **kwargs):
self.numbers = self.get_number_mobjects(
*numbers, **kwargs
)
self.add(*self.numbers)
return self
示例7: NumberPlane
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
class NumberPlane(VMobject):
CONFIG = {
"color" : BLUE_D,
"secondary_color" : BLUE_E,
"axes_color" : WHITE,
"secondary_stroke_width" : 1,
"x_radius": None,
"y_radius": None,
"x_unit_size" : 1,
"y_unit_size" : 1,
"center_point" : ORIGIN,
"x_line_frequency" : 1,
"y_line_frequency" : 1,
"secondary_line_ratio" : 1,
"written_coordinate_height" : 0.2,
"propogate_style_to_family" : False,
}
def generate_points(self):
if self.x_radius is None:
center_to_edge = (SPACE_WIDTH + abs(self.center_point[0]))
self.x_radius = center_to_edge / self.x_unit_size
if self.y_radius is None:
center_to_edge = (SPACE_HEIGHT + abs(self.center_point[1]))
self.y_radius = center_to_edge / self.y_unit_size
self.axes = VMobject()
self.main_lines = VMobject()
self.secondary_lines = VMobject()
tuples = [
(
self.x_radius,
self.x_line_frequency,
self.y_radius*DOWN,
self.y_radius*UP,
RIGHT
),
(
self.y_radius,
self.y_line_frequency,
self.x_radius*LEFT,
self.x_radius*RIGHT,
UP,
),
]
for radius, freq, start, end, unit in tuples:
main_range = np.arange(0, radius, freq)
step = freq/float(freq + self.secondary_line_ratio)
for v in np.arange(0, radius, step):
line1 = Line(start+v*unit, end+v*unit)
line2 = Line(start-v*unit, end-v*unit)
if v == 0:
self.axes.add(line1)
elif v in main_range:
self.main_lines.add(line1, line2)
else:
self.secondary_lines.add(line1, line2)
self.add(self.secondary_lines, self.main_lines, self.axes)
self.stretch(self.x_unit_size, 0)
self.stretch(self.y_unit_size, 1)
self.shift(self.center_point)
#Put x_axis before y_axis
y_axis, x_axis = self.axes.split()
self.axes = VMobject(x_axis, y_axis)
def init_colors(self):
VMobject.init_colors(self)
self.axes.set_stroke(self.axes_color, self.stroke_width)
self.main_lines.set_stroke(self.color, self.stroke_width)
self.secondary_lines.set_stroke(
self.secondary_color, self.secondary_stroke_width
)
return self
def get_center_point(self):
return self.coords_to_point(0, 0)
def coords_to_point(self, x, y):
x, y = np.array([x, y])
result = self.axes.get_center()
result += x*self.get_x_unit_size()*RIGHT
result += y*self.get_y_unit_size()*UP
return result
def point_to_coords(self, point):
new_point = point - self.axes.get_center()
x = new_point[0]/self.get_x_unit_size()
y = new_point[1]/self.get_y_unit_size()
return x, y
def get_x_unit_size(self):
return self.axes.get_width() / (2.0*self.x_radius)
def get_y_unit_size(self):
return self.axes.get_height() / (2.0*self.y_radius)
def get_coordinate_labels(self, x_vals = None, y_vals = None):
result = []
if x_vals == None and y_vals == None:
x_vals = range(-int(self.x_radius), int(self.x_radius))
y_vals = range(-int(self.y_radius), int(self.y_radius))
#.........这里部分代码省略.........
示例8: NumberLine
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
class NumberLine(VMobject):
CONFIG = {
"color" : BLUE,
"x_min" : -SPACE_WIDTH,
"x_max" : SPACE_WIDTH,
"space_unit_to_num" : 1,
"tick_size" : 0.1,
"tick_frequency" : 0.5,
"leftmost_tick" : None, #Defaults to ceil(x_min)
"numbers_with_elongated_ticks" : [0],
"longer_tick_multiple" : 2,
"number_at_center" : 0,
"propogate_style_to_family" : True
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
if self.leftmost_tick is None:
self.leftmost_tick = np.ceil(self.x_min)
VMobject.__init__(self, **kwargs)
def generate_points(self):
self.main_line = Line(self.x_min*RIGHT, self.x_max*RIGHT)
self.tick_marks = VMobject()
self.add(self.main_line, self.tick_marks)
for x in self.get_tick_numbers():
self.add_tick(x, self.tick_size)
for x in self.numbers_with_elongated_ticks:
self.add_tick(x, self.longer_tick_multiple*self.tick_size)
self.stretch(self.space_unit_to_num, 0)
self.shift(-self.number_to_point(self.number_at_center))
def add_tick(self, x, size):
self.tick_marks.add(Line(
x*RIGHT+size*DOWN,
x*RIGHT+size*UP,
))
return self
def get_tick_marks(self):
return self.tick_marks
def get_tick_numbers(self):
return np.arange(self.leftmost_tick, self.x_max, self.tick_frequency)
def number_to_point(self, number):
return interpolate(
self.main_line.get_left(),
self.main_line.get_right(),
float(number-self.x_min)/(self.x_max - self.x_min)
)
def point_to_number(self, point):
dist_from_left = (point[0]-self.main_line.get_left()[0])
num_dist_from_left = num_dist_from_left/self.space_unit_to_num
return self.x_min + dist_from_left
def default_numbers_to_display(self):
return self.get_tick_numbers()[::2]
def get_vertical_number_offset(self, direction = DOWN):
return 4*direction*self.tick_size
def get_number_mobjects(self, *numbers, **kwargs):
#TODO, handle decimals
if len(numbers) == 0:
numbers = self.default_numbers_to_display()
result = []
for number in numbers:
mob = TexMobject(str(int(number)))
mob.scale_to_fit_height(2*self.tick_size)
mob.shift(
self.number_to_point(number),
self.get_vertical_number_offset(**kwargs)
)
result.append(mob)
return result
def add_numbers(self, *numbers, **kwargs):
self.numbers = self.get_number_mobjects(
*numbers, **kwargs
)
self.add(*self.numbers)
return self
示例9: NumberPlane
# 需要导入模块: from mobject.vectorized_mobject import VMobject [as 别名]
# 或者: from mobject.vectorized_mobject.VMobject import add [as 别名]
class NumberPlane(VMobject):
CONFIG = {
"color" : BLUE_D,
"secondary_color" : BLUE_E,
"axes_color" : WHITE,
"secondary_stroke_width" : 1,
"x_radius": SPACE_WIDTH,
"y_radius": SPACE_HEIGHT,
"space_unit_to_x_unit" : 1,
"space_unit_to_y_unit" : 1,
"x_line_frequency" : 1,
"y_line_frequency" : 1,
"secondary_line_ratio" : 1,
"written_coordinate_height" : 0.2,
"written_coordinate_nudge" : 0.1*(DOWN+RIGHT),
"num_pair_at_center" : (0, 0),
"propogate_style_to_family" : False,
"submobject_partial_creation_mode" : "smoothed_lagged_start",
}
def generate_points(self):
self.axes = VMobject()
self.main_lines = VMobject()
self.secondary_lines = VMobject()
tuples = [
(
self.x_radius,
self.x_line_frequency,
self.y_radius*DOWN,
self.y_radius*UP,
RIGHT
),
(
self.y_radius,
self.y_line_frequency,
self.x_radius*LEFT,
self.x_radius*RIGHT,
UP,
),
]
for radius, freq, start, end, unit in tuples:
main_range = np.arange(0, radius, freq)
step = freq/float(freq + self.secondary_line_ratio)
for v in np.arange(0, radius, step):
line1 = Line(start+v*unit, end+v*unit)
line2 = Line(start-v*unit, end-v*unit)
if v == 0:
self.axes.add(line1)
elif v in main_range:
self.main_lines.add(line1, line2)
else:
self.secondary_lines.add(line1, line2)
self.add(self.axes, self.main_lines, self.secondary_lines)
self.stretch(self.space_unit_to_x_unit, 0)
self.stretch(self.space_unit_to_y_unit, 1)
#Put x_axis before y_axis
y_axis, x_axis = self.axes.split()
self.axes = VMobject(x_axis, y_axis)
def init_colors(self):
VMobject.init_colors(self)
self.axes.set_stroke(self.axes_color, self.stroke_width)
self.main_lines.set_stroke(self.color, self.stroke_width)
self.secondary_lines.set_stroke(
self.secondary_color, self.secondary_stroke_width
)
return self
def get_center_point(self):
return self.num_pair_to_point(self.num_pair_at_center)
def num_pair_to_point(self, pair):
pair = np.array(pair) + self.num_pair_at_center
result = self.get_center()
result[0] += pair[0]*self.space_unit_to_x_unit
result[1] += pair[1]*self.space_unit_to_y_unit
return result
def point_to_num_pair(self, point):
new_point = point-self.get_center()
center_x, center_y = self.num_pair_at_center
x = center_x + point[0]/self.space_unit_to_x_unit
y = center_y + point[1]/self.space_unit_to_y_unit
return x, y
def get_coordinate_labels(self, x_vals = None, y_vals = None):
result = []
if x_vals == None and y_vals == None:
x_vals = range(-int(self.x_radius), int(self.x_radius))
y_vals = range(-int(self.y_radius), int(self.y_radius))
for index, vals in enumerate([x_vals, y_vals]):
num_pair = [0, 0]
for val in vals:
num_pair[index] = val
point = self.num_pair_to_point(num_pair)
num = TexMobject(str(val))
num.scale_to_fit_height(
self.written_coordinate_height
)
num.shift(
#.........这里部分代码省略.........