如何确定多边形的边中心
How to determine the edge center of a polygon
我用多边形做三角形:
tri1 = Polygon(LEFT*1.5+UP, RIGHT*1.5+UP, RIGHT*1.5+UP*1.5)
然后我想在多边形的边缘中心显示三个文本。
c = TexMobject("c").next_to(tri1.get_edge_center(LEFT), UP)
a = TexMobject("a").next_to(tri1.get_edge_center(DOWN), DOWN)
b = TexMobject("b").next_to(tri1.get_edge_center(RIGHT), RIGHT)
但是"c"距离边缘中心很远,所以我改变了位置:
tri1.get_edge_center(LEFT)
到
interpolate(LEFT*1.5+UP, RIGHT*1.5+UP*1.5, 0.5)
看起来不错。
但是我想使用一致的方式来执行此操作,How to get position for "c" using get_edge_center?谢谢!
如果你检查 manimlib / mobject.py 库或 vector_mobject 库你会发现没有这样的相似之处。让 manim 如此酷的一件事是,如果无法完成某些事情,您可以自己做。我建议不要询问,如果你没有找到一个方法,你自己创建它(就像插值一样)。通过这种方式,您将学到更多知识并开发自己的代码,而无需依赖于您能做什么或不能做什么。
class Polygon(Polygon):
def get_center_of_edges(self,buff=SMALL_BUFF*3):
vertices = self.get_vertices()
coords_vertices = []
for i in range(len(vertices)):
if i < len(vertices)-1:
p1,p2 = [vertices[i],vertices[i+1]]
else:
p1,p2 = [vertices[-1],vertices[0]]
guide_line = Line(p1,p2)
side = guide_line.get_center()
normal_direction = guide_line.copy()
normal_direction.rotate(-PI/2)
vector_normal_direction = normal_direction.get_unit_vector()
direction = Dot(side).shift(vector_normal_direction*buff).get_center()
coords_vertices.append(direction)
return coords_vertices
class PolygonScene(Scene):
def construct(self):
tri1 = Polygon(LEFT*1.5+UP, RIGHT*1.5+UP, RIGHT*1.5+UP*1.5)
center_vertices =tri1.get_center_of_edges()
labels = VGroup(*[
TexMobject(label).move_to(point) for label,point in zip(["a","b","c"],center_vertices)
])
self.add(tri1,labels)
class RegularPolygon(RegularPolygon):
def get_center_of_edges(self,buff=SMALL_BUFF*3):
vertices = self.get_vertices()
coords_vertices = []
for i in range(len(vertices)):
if i < len(vertices)-1:
p1,p2 = [vertices[i],vertices[i+1]]
else:
p1,p2 = [vertices[-1],vertices[0]]
guide_line = Line(p1,p2)
side = guide_line.get_center()
normal_direction = guide_line.copy()
normal_direction.rotate(-PI/2)
vector_normal_direction = normal_direction.get_unit_vector()
direction = Dot(side).shift(vector_normal_direction*buff).get_center()
coords_vertices.append(direction)
return coords_vertices
class PolygonScene2(Scene):
def construct(self):
tri1 = RegularPolygon(6).scale(2.5)
center_vertices =tri1.get_center_of_edges()
labels = ["a","b","c","d","e","f"]
labels = VGroup(*[
TexMobject(label).move_to(point) for label,point in zip(labels,center_vertices)
])
self.add(tri1,labels)
我用多边形做三角形:
tri1 = Polygon(LEFT*1.5+UP, RIGHT*1.5+UP, RIGHT*1.5+UP*1.5)
然后我想在多边形的边缘中心显示三个文本。
c = TexMobject("c").next_to(tri1.get_edge_center(LEFT), UP)
a = TexMobject("a").next_to(tri1.get_edge_center(DOWN), DOWN)
b = TexMobject("b").next_to(tri1.get_edge_center(RIGHT), RIGHT)
但是"c"距离边缘中心很远,所以我改变了位置:
tri1.get_edge_center(LEFT)
到
interpolate(LEFT*1.5+UP, RIGHT*1.5+UP*1.5, 0.5)
看起来不错。 但是我想使用一致的方式来执行此操作,How to get position for "c" using get_edge_center?谢谢!
如果你检查 manimlib / mobject.py 库或 vector_mobject 库你会发现没有这样的相似之处。让 manim 如此酷的一件事是,如果无法完成某些事情,您可以自己做。我建议不要询问,如果你没有找到一个方法,你自己创建它(就像插值一样)。通过这种方式,您将学到更多知识并开发自己的代码,而无需依赖于您能做什么或不能做什么。
class Polygon(Polygon):
def get_center_of_edges(self,buff=SMALL_BUFF*3):
vertices = self.get_vertices()
coords_vertices = []
for i in range(len(vertices)):
if i < len(vertices)-1:
p1,p2 = [vertices[i],vertices[i+1]]
else:
p1,p2 = [vertices[-1],vertices[0]]
guide_line = Line(p1,p2)
side = guide_line.get_center()
normal_direction = guide_line.copy()
normal_direction.rotate(-PI/2)
vector_normal_direction = normal_direction.get_unit_vector()
direction = Dot(side).shift(vector_normal_direction*buff).get_center()
coords_vertices.append(direction)
return coords_vertices
class PolygonScene(Scene):
def construct(self):
tri1 = Polygon(LEFT*1.5+UP, RIGHT*1.5+UP, RIGHT*1.5+UP*1.5)
center_vertices =tri1.get_center_of_edges()
labels = VGroup(*[
TexMobject(label).move_to(point) for label,point in zip(["a","b","c"],center_vertices)
])
self.add(tri1,labels)
class RegularPolygon(RegularPolygon):
def get_center_of_edges(self,buff=SMALL_BUFF*3):
vertices = self.get_vertices()
coords_vertices = []
for i in range(len(vertices)):
if i < len(vertices)-1:
p1,p2 = [vertices[i],vertices[i+1]]
else:
p1,p2 = [vertices[-1],vertices[0]]
guide_line = Line(p1,p2)
side = guide_line.get_center()
normal_direction = guide_line.copy()
normal_direction.rotate(-PI/2)
vector_normal_direction = normal_direction.get_unit_vector()
direction = Dot(side).shift(vector_normal_direction*buff).get_center()
coords_vertices.append(direction)
return coords_vertices
class PolygonScene2(Scene):
def construct(self):
tri1 = RegularPolygon(6).scale(2.5)
center_vertices =tri1.get_center_of_edges()
labels = ["a","b","c","d","e","f"]
labels = VGroup(*[
TexMobject(label).move_to(point) for label,point in zip(labels,center_vertices)
])
self.add(tri1,labels)