class Shadow_2d(VGroup): CONFIG = { 'shadow_color': DARK_GRAY, 'shadow_opacity': 0.6, 'blur_width': 0.25, 'layer_num': 40, 'scale_factor': 1, 'shadow_out': True, 'show_basic_shape': True, 'plot_depth':-1, 'rate_func': lambda t: t ** 0.5, } def __init__(self, mob_or_points, **kwargs): VGroup.__init__(self, **kwargs) if type(mob_or_points) == list: self.shape = Polygon(*mob_or_points, stroke_width=0, plot_depth=-1) else: self.shape = mob_or_points.set_stroke(width=0) self.shape.set_fill(color=self.shadow_color, opacity=self.shadow_opacity * (1 if self.show_basic_shape else 0)).scale(self.scale_factor) self.blur_outline = VGroup() s = (self.shape.get_height() + self.shape.get_width())/2 if self.blur_width > 1e-4: for i in range(self.layer_num): layer_i = self.shape.copy().set_stroke(color=self.shadow_color, width=100 * self.blur_width/self.layer_num, opacity=self.shadow_opacity * (1-self.rate_func(i/self.layer_num))).\ set_fill(opacity=0).scale((s + (1 if self.shadow_out else -1) * self.blur_width/self.layer_num * (i+0.5))/ s).set_plot_depth(-2) self.blur_outline.add(layer_i) self.add(self.shape, self.blur_outline)
def get_cone(self): self.bottom_circle = Polygon(*self.bc_points, color=self.color, fill_opacity=self.fill_opacity, stroke_width=1, stroke_color=self.color) self.cone_side = VGroup() n = 4 rm_n = (len(self.bc_points) - 1) % (n - 2) step = (len(self.bc_points) - 1) // (n - 2) range1 = range(0, step * (n - 2), n - 2) for i in range1: points = [self.bc_points[j] for j in range(i, i + n - 1)] points.append(self.direction) self.cone_side.add( Polygon(*points, color=self.color, fill_opacity=self.fill_opacity, stroke_width=1, stroke_color=self.color)) points = [self.bc_points[i] for i in \ range(step*(n-2),step*(n-2)+rm_n+1)] if rm_n != 0: points = [self.bc_points[i] for i in \ range(step*(n-2),step*(n-2)+rm_n+1)] points.append(self.direction) self.cone_side.add( Polygon(*points, color=self.color, fill_opacity=self.fill_opacity, stroke_width=1, stroke_color=self.color))
def __init__(self, **kwargs): kwargs["color"] = TRIANGLE_COLOR Polygon.__init__(self, *POINTS2[[1, 0, 2]], edge_colors=[B_COLOR, C_COLOR, A_COLOR], fill_color=TRIANGLE_COLOR, fill_opacity=0.5, **kwargs)
def init_points(self): start_angle = np.pi / 2 + self.arc_angle / 2 end_angle = np.pi / 2 - self.arc_angle / 2 self.add(Arc(start_angle=start_angle, angle=-self.arc_angle)) tick_angle_range = np.linspace(start_angle, end_angle, self.num_ticks) for index, angle in enumerate(tick_angle_range): vect = rotate_vector(RIGHT, angle) tick = Line((1 - self.tick_length) * vect, vect) label = TexMobject(str(10 * index)) label.set_height(self.tick_length) label.shift((1 + self.tick_length) * vect) self.add(tick, label) needle = Polygon(LEFT, UP, RIGHT, stroke_width=0, fill_opacity=1, fill_color=self.needle_color) needle.stretch_to_fit_width(self.needle_width) needle.stretch_to_fit_height(self.needle_height) needle.rotate(start_angle - np.pi / 2, about_point=ORIGIN) self.add(needle) self.needle = needle self.center_offset = self.get_center()
def __init__(self, corner=ORIGIN, angle=0, **kwargs): VGroup.__init__(self, **kwargs) self.corner = ORIGIN self.angle = 0 r = UR if self.on_the_right else UL self.add( Polygon(ORIGIN, RIGHT * self.size * r, UR * self.size * r, UP * self.size * r, stroke_width=0, fill_color=self.fill_color, fill_opacity=self.fill_opacity), Line(RIGHT * self.size * r, UR * self.size * r + UP * self.stroke_width / 100 / 2 * 0.8, stroke_width=self.stroke_width, stroke_color=self.stroke_color), Line(UR * self.size * r + RIGHT * self.stroke_width / 100 / 2 * r * 0.8, UP * self.size * r, stroke_width=self.stroke_width, stroke_color=self.stroke_color), ) self.move_corner_to(corner) self.change_angle_to(angle)
def generate_points(self): start_angle = np.pi / 2 + self.arc_angle / 2 end_angle = np.pi / 2 - self.arc_angle / 2 self.add(Arc( start_angle=start_angle, angle=-self.arc_angle )) tick_angle_range = np.linspace(start_angle, end_angle, self.num_ticks) for index, angle in enumerate(tick_angle_range): vect = rotate_vector(RIGHT, angle) tick = Line((1 - self.tick_length) * vect, vect) label = TexMobject(str(10 * index)) label.set_height(self.tick_length) label.shift((1 + self.tick_length) * vect) self.add(tick, label) needle = Polygon( LEFT, UP, RIGHT, stroke_width=0, fill_opacity=1, fill_color=self.needle_color ) needle.stretch_to_fit_width(self.needle_width) needle.stretch_to_fit_height(self.needle_height) needle.rotate(start_angle - np.pi / 2, about_point=ORIGIN) self.add(needle) self.needle = needle self.center_offset = self.get_center()
def __init__(self, mob_or_points, **kwargs): VGroup.__init__(self, **kwargs) if type(mob_or_points) == list: self.shape = Polygon(*mob_or_points, stroke_width=0, plot_depth=-1) else: self.shape = mob_or_points.set_stroke(width=0) self.shape.set_fill(color=self.shadow_color, opacity=self.shadow_opacity * (1 if self.show_basic_shape else 0)).scale(self.scale_factor) self.blur_outline = VGroup() s = (self.shape.get_height() + self.shape.get_width())/2 if self.blur_width > 1e-4: for i in range(self.layer_num): layer_i = self.shape.copy().set_stroke(color=self.shadow_color, width=100 * self.blur_width/self.layer_num, opacity=self.shadow_opacity * (1-self.rate_func(i/self.layer_num))).\ set_fill(opacity=0).scale((s + (1 if self.shadow_out else -1) * self.blur_width/self.layer_num * (i+0.5))/ s).set_plot_depth(-2) self.blur_outline.add(layer_i) self.add(self.shape, self.blur_outline)
def __init__(self, *vertices, **kwargs): VGroup.__init__(self, **kwargs) self.lines, self.dots = VGroup(plot_depth=1), VGroup(plot_depth=1) self.poly=Polygon(*vertices, fill_color=self.fill_color, fill_opacity=self.fill_opacity, plot_depth=0).set_stroke(width=0) self.add(self.lines, self.dots, self.poly) n = len(vertices) for i in range(n): self.lines.add(Line(vertices[i], vertices[(i+1) % n], color=self.stroke_color, stroke_width=self.stroke_width, plot_depth=2)) self.dots.add(Dot(vertices[i], color=self.stroke_color, plot_depth=2).set_height(self.stroke_width/100))
def construct(self): circle = Circle() square = Square() line = Line((3, 0, 0), (5, 0, 0)) triangle = Polygon((0, 0, 0), (1, 1, 0), (1, -1, 0)) self.play(ShowCreation(circle), run_time=5) self.play(FadeOut(circle), GrowFromCenter(square)) self.add(line) self.play(Transform(square, triangle)) self.play(Transform(triangle)) self.wait()
def construct(self): circle = Circle() square = Square() line = Line(np.array([3, 0, 0]), np.array([5, 0, 0])) triangle = Polygon(np.array([0, 0, 0]), np.array([1, 1, 0]), np.array([1, -1, 0])) self.add(line) self.play(ShowCreation(circle)) self.play(FadeOut(circle)) self.play(GrowFromCenter(square)) self.play(Transform(square, triangle)) self.wait()
def init_points(self) -> None: # Star by creating two of the pentagons, meeting # back to back on the positive x-axis phi = (1 + math.sqrt(5)) / 2 x, y, z = np.identity(3) pentagon1 = Polygon( [phi, 1 / phi, 0], [1, 1, 1], [1 / phi, 0, phi], [1, -1, 1], [phi, -1 / phi, 0], ) pentagon2 = pentagon1.copy().stretch(-1, 2, about_point=ORIGIN) pentagon2.reverse_points() x_pair = VGroup(pentagon1, pentagon2) z_pair = x_pair.copy().apply_matrix(np.array([z, -x, -y]).T) y_pair = x_pair.copy().apply_matrix(np.array([y, z, x]).T) self.add(*x_pair, *y_pair, *z_pair) for pentagon in list(self): pc = pentagon.copy() pc.apply_function(lambda p: -p) pc.reverse_points() self.add(pc)
def VisitTriangles(face): # 访问网 visitvalue = face.Visit notvisitvalue = not visitvalue faces = [face] # 访问过 face.Visit = notvisitvalue delaunaynet = VGroup() while faces: eachface = faces[-1] faces.pop(-1) # 面所在的三条边 e1 = eachface.HalfEdge e2 = e1.Suc e3 = e2.Suc # 标记访问过 e1.Visit = notvisitvalue e2.Visit = notvisitvalue e3.Visit = notvisitvalue # 面对三个点 p1 = e1.Start.Point p2 = e2.Start.Point p3 = e3.Start.Point delaunaynet.add(Polygon(p1, p2, p3)) ei = [e1, e2, e3] for each in ei: et = each.Twin if et: etf = et.Face # 未访问过 if etf.Visit == visitvalue: # 访问过 etf.Visit = notvisitvalue faces.append(etf) return delaunaynet
def add_spikes(self): layers = VGroup() radii = np.linspace( self.outer_radius, self.pupil_radius, self.n_spike_layers, endpoint=False, ) radii[:2] = radii[1::-1] # Swap first two if self.n_spike_layers > 2: radii[-1] = interpolate(radii[-1], self.pupil_radius, 0.25) for radius in radii: tip_angle = self.spike_angle half_base = radius * np.tan(tip_angle) triangle, right_half_triangle = [ Polygon( radius * UP, half_base * RIGHT, vertex3, fill_opacity=1, stroke_width=0, ) for vertex3 in ( half_base * LEFT, ORIGIN, ) ] left_half_triangle = right_half_triangle.copy() left_half_triangle.flip(UP, about_point=ORIGIN) n_spikes = self.n_spikes full_spikes = [ triangle.copy().rotate(-angle, about_point=ORIGIN) for angle in np.linspace(0, TAU, n_spikes, endpoint=False) ] index = (3 * n_spikes) // 4 if radius == radii[0]: layer = VGroup(*full_spikes) layer.rotate(-TAU / n_spikes / 2, about_point=ORIGIN) layer.brown_index = index else: half_spikes = [ right_half_triangle.copy(), left_half_triangle.copy().rotate( 90 * DEGREES, about_point=ORIGIN, ), right_half_triangle.copy().rotate( 90 * DEGREES, about_point=ORIGIN, ), left_half_triangle.copy() ] layer = VGroup(*it.chain( half_spikes[:1], full_spikes[1:index], half_spikes[1:3], full_spikes[index + 1:], half_spikes[3:], )) layer.brown_index = index + 1 layers.add(layer) # Color spikes blues = self.blue_spike_colors browns = self.brown_spike_colors for layer, blue, brown in zip(layers, blues, browns): index = layer.brown_index layer[:index].set_color(blue) layer[index:].set_color(brown) self.spike_layers = layers self.add(layers)
def c_square(**kwargs): return Polygon(*POINTS2[[2, 1, 8, 7]], color=C_COLOR, **kwargs, fill_color=C_COLOR, fill_opacity=0.5)
def a_square(**kwargs): return Polygon(*pts[[0, 2, 4, 3]], color=ac, **kwargs, fill_color=ac, fill_opacity=0.7)
def Triangle(**kwargs): return Polygon(*pts[[1, 0, 2]], color=tc, **kwargs, fill_color=tc, fill_opacity=0.7)
def get_table(elts_list, buff_length=0.3, cell_length=1, cell_height=1, line_color=WHITE, text_color=WHITE, background_color=BLACK): nb_l = len(elts_list) nb_c = len(elts_list[0]) l_fill = 0.015 table = VGroup() grid = VGroup() result = VGroup() rec = Polygon( (-cell_length / 2, cell_height / 2, 0), (-cell_length / 2 + (nb_c * cell_length), cell_height / 2, 0), (-cell_length / 2 + (nb_c * cell_length), cell_height / 2 - (nb_l) * cell_height, 0), (-cell_length / 2, cell_height / 2 - (nb_l) * cell_height, 0), mark_paths_closed=True, close_new_points=True, fill_color=background_color, fill_opacity=1, color=background_color) for i in range(nb_l): for j in range(nb_c): #elt = Text(elts_list[i][j],color = text_color, font = "Open Sans Bold Italic") if elts_list[i][j] != " ": #TextMobject doesnt like " " strings elt = TextMobject(elts_list[i][j], color=text_color) elt.move_to([j * cell_length, -i * cell_height, 0]) table.add(elt) for i in range(nb_l + 1): # start_line_hor = (0, -i * cell_height,0) # + (-cell_length ,cell_height,0) # end_line_hor = ((nb_c * cell_length), -i * cell_height,0) # + (-cell_length ,cell_height,0) start_line_hor = (-cell_length / 2 - l_fill, cell_height / 2 - i * cell_height, 0 ) # + ( ,cell_height,0) end_line_hor = (-cell_length / 2 + (nb_c * cell_length) + l_fill, cell_height / 2 - i * cell_height, 0 ) # + (-cell_length ,cell_height,0) line_hor = Line(start=start_line_hor, end=end_line_hor, color=line_color) grid.add(line_hor) for j in range(nb_c + 1): start_line_ver = (-cell_length / 2 + j * cell_length, cell_height / 2 + l_fill, 0 ) # (-cell_length ,cell_height,0) end_line_ver = (-cell_length / 2 + j * cell_length, cell_height / 2 - (nb_l) * cell_height - l_fill, 0 ) # (-cell_length ,cell_height,0) line_ver = Line(start=start_line_ver, end=end_line_ver, color=line_color) grid.add(line_ver) #Rec = Rectangle() result.add(rec) result.add(table) result.add(grid) return result
def get_seed_shape(self): return Polygon( RIGHT, np.sqrt(3) * UP, LEFT, )
def b_square(**kwargs): return Polygon(*pts[[1, 0, 5, 6]], color=bc, **kwargs, fill_color=bc, fill_opacity=0.7)
def polygon_to_mobject(self, polygon: se.Polygon) -> Polygon: points = [_convert_point_to_3d(*point) for point in polygon] return Polygon(*points)
def triangle(**kwargs): return Polygon(*POINTS[[0, 1, 2]], color=TRI_COLOR, **kwargs, fill_color=TRI_COLOR, fill_opacity=1)
def leftblock(**kwargs): return Polygon(*POINTS[[3, 4, 5, 6]], color=BLOCK_COLOR, **kwargs, fill_color=BLOCK_COLOR, fill_opacity=0.8)
def rightblock(**kwargs): return Polygon(*POINTS[[10, 9, 8, 7]], color=BLOCK_COLOR, **kwargs, fill_color=BLOCK_COLOR, fill_opacity=0.8)
def c_square(**kwargs): return Polygon(*pts[[2, 1, 8, 7]], color=cc, **kwargs, fill_color=cc, fill_opacity=0.7)
def a_square(**kwargs): return Polygon(*POINTS2[[0, 2, 4, 3]], color=A_COLOR, **kwargs, fill_color=A_COLOR, fill_opacity=0.5)
def b_square(**kwargs): return Polygon(*POINTS2[[1, 0, 5, 6]], color=B_COLOR, **kwargs, fill_color=B_COLOR, fill_opacity=0.5)
def big_square(**kwargs): return Polygon(*pts[[9, 10, 11, 12]], color=WHITE, **kwargs, fill_color=WHITE, fill_opacity=0)