Search code examples
pythonmatplotlibjupyter-labbezierreplicate

Is there a way to iterate over a pattern to repeat it clockwise with matplotlib in Python?


I was plotting a car using matplotlib on JupyterLab, a Python distribution offered by Anaconda. I was using Bézier curves to do so.

When I was about to plot the wheels of the car, I realized that it was going to be extremely slow and stressful to get every single point of those many lines and shapes we have inside the Y wheel pattern and plot it. Take a look at it:

enter image description here
So I wanted to know if there was a way to build one shape (a list of points) and iterate over it 360º to repeat the sequence around a central point and make the wheel. My intention is to think as if the shape was the cookie cutter and then I just have to do more cookies using the cutter.


Solution

  • After a long time, I realized that the answer for my question was here in this other question. The function rotate provided by Mark Dickinson is all I needed. This function rotates a given point, so all I had to do was to apply this function to all the points inside the PATH list of points. Here is how I did it:

    def replica(center_of_rotation, times, points, codes, linewidth = 1, c = "black"):
        l = []
        angle = 0
        counter = 1
        while angle <= 360:
            for i in points: 
                l.append(rotate(center_of_rotation, i, math.radians(angle)))
                counter += 1
                if len(l) == len(points):
                    ax.add_patch(patches.PathPatch(Path(l, codes), fc="none", transform=ax.transData, color=c, lw = linewidth))
                    angle += 360/times
                    l = []
                if angle >= 360:
                    return
    

    The result was awesome, I can replicate any PATH pattern 'N' number of times. I used this to build the wheels of my Lamborghini.

    • Pattern replicated 1 time:

    enter image description here

    • Replicated 5 times, just how much I need:

    enter image description here