Sunday, March 10, 2024

PMAE 5 : curved regular polygon

Python Matplotlib Animation Example 5

curved regular polygon

The provided Python code generates an animated plot of a curved regular polygon using the Matplotlib library. The polygon is dynamically drawn with a specified number of sides (in this case, 4), resulting in a visually appealing animation. The getdata function computes the coordinates of the polygon vertices based on the number of sides, utilizing trigonometric functions like cosine and sine. The update function progressively updates the plot during the animation, while the draw function, invoked by the animation framework, calculates the current angle of rotation for the polygon based on time, creating the illusion of continuous motion. The animation interval is set to 40 milliseconds, controlling the speed of the rotation and showcasing a smoothly rotating regular polygon within the specified plot limits. The code efficiently utilizes mathematical computations and animation functionalities from Matplotlib to generate an engaging visualization of a curved regular polygon. It's a concise and well-structured piece of code that leverages the strengths of Matplotlib for dynamic graphics generation.



This Python code animates a curved regular polygon. The getdata function calculates the coordinates for a curved shape by rotating smaller line segments. The animation function continuously reveals more segments as time progresses, creating the illusion of a curved polygon rotating and unfolding. Libraries like matplotlib handle plotting and animation. In essence, the code dynamically generates and animates a curved regular polygon with continuous motion. This animation hinges on a clever interplay of trigonometric functions, coordinate transformations, and Matplotlib's animation capabilities.

Python source code

import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from math import cos, sin, tan, pi

fig = plt.figure(figsize=(3,3), facecolor="#FFFFFF")
ax = plt.axes([0, 0, 1, 1])
ax.set_xlim([-1.2, 1.2])
ax.set_ylim([-1.2, 1.2])
ax.axis('off')

def getdata(n):
nth = int(500/n)*n
dth = 2*pi/(nth-1)
sth = 2*pi/n
xs, ys = [], []
for ith in range(nth):
theta = ith*dth
rth = int(theta/sth)*sth
x1 = cos(pi-sth/2+(theta-rth)) + cos(sth/2)*2
y1 = sin(pi-sth/2+(theta-rth))
x2 = x1*cos(rth) + y1*sin(rth)
y2 = -x1*sin(rth) + y1*cos(rth)
xs.append(x2)
ys.append(y2)
xs.append(xs[0])
ys.append(xs[0])
return xs, ys

pl = ax.plot([], [], color="#0000FF")[0]

xs, ys = getdata(4)

def update(theta):
ith = int(theta/(2*pi/(len(xs)-1)))
pl.set_data(xs[0:ith], ys[0:ith])

def draw(i):
P = 5.0
t = time.time()
theta = t/P*2*pi%(2*pi)
update(theta)

ani = animation.FuncAnimation(fig, draw, interval=40)

plt.show()

■  Imports:
• Brings in `time`, `matplotlib.pyplot`, `matplotlib.animation`, and `math` for time-related functions, plotting, animation, and mathematical calculations.

■ Figure and Axes Setup:
• Creates a figure for the animation with a white background.
• Sets up axes with specified limits and turns off axis labels.

■ `getdata` Function:
• Takes the number of sides (`n`) as input.
• Calculates coordinates for a curved n-sided polygon using trigonometric functions.
• Returns lists of x- and y-coordinates representing the polygon's points.

■ Plotting Elements:
• Creates an empty plot with blue color for future updates.
• Calls `getdata(4)` to generate coordinates for a 4-sided polygon.

■ Animation Functions:
• `update(theta)`: Takes an angle `theta`, calculates which portion of the polygon to display based on `theta`, and updates the plot accordingly.
• `draw(i)`: Calculates a time-dependent angle `theta`, calls `update(theta)`, and handles animation frame updates.

■ Animation Creation:
• Uses `matplotlib.animation.FuncAnimation` to create a continuous animation based on the `draw` function.

■ Display:
• `plt.show()`: Shows the animated plot.

No comments:

Post a Comment