Sunday, March 24, 2024

PMAE 6 : rounded rectangle

Python Matplotlib Animation Example 6

rounded rectangle

The provided Python code generates an animated plot of a rounded rectangle using matplotlib, initializing the figure and axes and defining parameters such as dimensions and curvature. Through trigonometric computations, the getdata function determines coordinates along the rectangle's outline, accounting for the specified curvature. The update function dynamically modifies the plot during animation, adjusting data points based on the current angle, while the draw function regulates animation timing. Utilizing FuncAnimation, the code orchestrates continuous plot updates, exemplifying matplotlib's capabilities and mathematical techniques for geometric manipulation in Python. This illustrative example showcases the transformation of a rounded rectangle over time, providing insights into graphical representation and animation methodologies.



This Python code animates a rotating, filled rounded rectangle using Matplotlib's libraries. It defines functions to generate the shape's data based on a radius value. The getdata function calculates corner points using trigonometric functions and adjusts them for smooth curvature. An animation loop continuously updates the visible portion of the rectangle. It receives a time-dependent angle, calculates the visible data points, and creates the illusion of rotation. This showcases how to create and animate a visually appealing rounded rectangle.

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, 1])
ax.set_ylim([-1, 1])
ax.axis('off')

thmin = 0
thmax = pi
nth = 100
dth = pi/(nth-1)

rmin = 0.5
rmax = 1.0
nr = 3
dr = (rmax-rmin)/(nr)

def getdata(rr):
if rr < 0.0: rr = 0.0
if rr > 1.0: rr = 1.0
nth = int(300/4)*4
dth = 2*pi/(nth)
sth = 2*pi/4
xmax = cos(sth/2)
ymax = sin(sth/2)
xsmall0 = xmax - xmax*rr
ysmall0 = ymax - ymax*rr
rsmall = xmax*rr
xs, ys = [], []
for ith in range(nth):
theta = ith*dth
rth = int(theta/sth)*sth
x1 = cos(sth/2)
y1 = tan(sth/2-(theta-rth))*x1
if abs(y1) > ysmall0:
if y1 == 0: sign = 1.0
else: sign = abs(y1)/(y1)
thsmall = (rsmall-(ymax-abs(y1)))/rsmall*pi/4
xsmall = cos(thsmall*sign)*rsmall
ysmall = sin(thsmall*sign)*rsmall
x1 = xsmall + xsmall0
y1 = ysmall + ysmall0*sign
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(ys[0])
return xs, ys

xs, ys = getdata(0.5)

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

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

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()


■  Setting Up the Environment:
• Imports libraries for plotting (matplotlib) and animation.
• Creates a figure with a white background and an axis without labels or ticks.
• Defines parameters for angles and radii used in the animation.

■ Calculating Rounded Rectangle Points:
• The `getdata` function takes a radius value (`rr`) as input.
• It calculates trigonometric values based on a reference angle (`sth`).
• It iterates through a range of angles (`nth`) to define points along the rounded rectangle's edge.
• For points falling outside the rectangle due to curvature, it adjusts them using additional calculations to maintain a smooth shape.
• Finally, it returns lists containing the x and y coordinates of all the points for the rounded rectangle.

■ Creating the Initial Plot and Animation Functions:
• The code retrieves points for a medium radius and creates the initial plot of the rounded rectangle using `ax.plot`.
• The `update` function takes an angle (`theta`) as input. It calculates the number of points visible based on the rotation and updates the line plot data accordingly.
• The `draw` function is called repeatedly during the animation. It calculates a time-dependent angle, calls `update` to adjust the visible portion, and creates the illusion of rotation.

■ Running the Animation:
• `FuncAnimation` from matplotlib's animation library is used to initiate the animation process. It calls the `draw` function at set intervals (40 milliseconds in this case).
• Finally, `plt.show()` displays the animation on the screen.

No comments:

Post a Comment