Search code examples
pythonanimationmanim

When rotating shapes, vertices are just slightly off


I'm working on a manim challenge from this video: https://youtu.be/HKPm8FZYaqI?t=700. The challenge is to code the animation which starts at 11:40 and ends at 11:49.

I got up to the point where the triangles are rotated and copied over to the second square, but for some reason they are just the ones that I had to rotate are just ever so slightly off, while the ones I didn't have to rotate seem to be perfect.

Look at this image: enter image description here

The triangles fit perfectly inside the square to the right. But in the right square, the ones that were rotated (1 and 4) do not. Below is a closeup of what I mean for triangle number 1:enter image description here

Of course, this is how I want it to look:enter image description here The dimensions of the shapes and maybe the colours are a little different, but that is because this is the solution of the author of the video, and the previous was my attempt. I don't care about that, I only care about why the triangles don't fit perfectly in my attempt like they do here. Zooming in on this picture, we see that the triangles do indeed fit perfectly:enter image description here

Any insight into why this is happening would be very much appreciated!

The source code for my animation is this:

class Pythagoras(Scene):
    def construct(self):
        title = TextMobject("Pythagorean Theorem")
        title.to_edge(UL)

        pre_square = Polygon(
            [-2, 2, 0],
            [2, 2, 0],
            [2, -2, 0],
            [-2, -2, 0],
            color=WHITE
        )

        self.wait()


        square2 = Polygon(
            [-1.41, 1.41, 0],
            [1.41, 1.41, 0],
            [1.41, -1.41, 0],
            [-1.41, -1.41, 0]

        )
        square2.rotate(PI/6)

        triangle1 = Polygon(
            [-2, 2, 0],
            [-2 + math.sqrt(6), 2, 0],
            [-2, 2 - math.sqrt(2), 0],
            color=YELLOW
        )

        triangle2 = Polygon(
            [2, 2, 0],
            [-2 + math.sqrt(6), 2, 0],
            [2, 2 - math.sqrt(6), 0],
            color=YELLOW
        )

        triangle3 = Polygon(
            [2, 2 - math.sqrt(6), 0],
            [2, -2, 0],
            [2 - math.sqrt(6), -2, 0],
            color=YELLOW
        )

        triangle4 = Polygon(
            [-2, 2 - math.sqrt(2), 0],
            [-2, -2, 0],
            [2 - math.sqrt(6), -2, 0],
            color=YELLOW
        )

        triangles = [triangle1, triangle2, triangle3, triangle4]
        for triangle in triangles:
            triangle.set_fill(YELLOW, 0.6)

        self.play(Write(title), ShowCreation(pre_square), ShowCreation(triangle1), ShowCreation(triangle2), ShowCreation(triangle3), ShowCreation(triangle4))

        self.wait()

        group = VGroup(pre_square, triangle1, triangle2, triangle3, triangle4)

        self.play(ApplyMethod(group.to_edge, LEFT, {"buff": 1.6}))
        self.wait()

        square3 = pre_square.copy()

        self.play(ApplyMethod(square3.shift, RIGHT * 7))

        triangle2.generate_target()
        triangle2.target.shift(RIGHT * (7- math.sqrt(6)))

        triangle1.generate_target()
        triangle1.target = triangle2.target.copy().rotate(PI) 

        triangle3.generate_target()
        triangle3.target.shift(RIGHT * 7)

        triangle4.generate_target()
        triangle4.target = triangle3.target.copy().rotate(PI)


        self.play(MoveToTarget(triangle1.copy()), MoveToTarget(triangle2.copy()), MoveToTarget(triangle3.copy()), MoveToTarget(triangle4.copy()))

        self.wait()

Solution

  • The problem is the thickness of the VMobjects, by default it is 4, if you change it to 2 or 1 (in the solution that I give is 1) those corners are removed. Add this in your for:

            for triangle in triangles:
                triangle.set_fill(YELLOW, 0.6)
                triangle.set_stroke(None,1.5)
                #or
                #triangle.set_stroke(width=1.5)
                #it is the same