如何确定多边形的边缘中心

时间:2019-08-28 02:58:29

标签: manim

我使用多边形制作三角形:

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)

看起来还可以。 但是我想使用一致的方法来执行此操作,如何使用get_edge_center获取“ c”的位置?谢谢!

1 个答案:

答案 0 :(得分:0)

如果您检查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)

enter image description here

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)

enter image description here