Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sin, cos, pi
- import matplotlib.pyplot as plt
- from matplotlib.patches import Circle, RegularPolygon
- from matplotlib.animation import FuncAnimation
- from matplotlib import colormaps as cm
- import matplotlib.colors as mplcolors
- RADIUS_C = 1
- num_sides = [i for i in range(3, 11)]
- num_sides_min = min(num_sides)
- num_sides_max = max(num_sides)
- num_frames = len(num_sides)
- cmap = cm.get_cmap("winter")
- colors = [mplcolors.to_hex(cmap(i)) for i in range(num_frames)]
- polygon_areas = []
- polygon_prims = []
- for n_side in num_sides:
- polygon_areas.append(n_side * RADIUS_C**2 * sin(pi /n_side) * cos(pi / n_side))
- polygon_prims.append(2 * n_side * RADIUS_C * sin(pi / n_side))
- fig, ax = plt.subplots(figsize=(6, 6))
- def create_circle():
- shape_1 = Circle((1.5, 1.5),
- radius=RADIUS_C,
- fill=False,
- linewidth=0.2,
- edgecolor="red")
- ax.add_patch(shape_1)
- def animate(frame):
- ax.clear()
- ax.axis([0, 3, 0, 3])
- create_circle()
- n_sides = frame + 3
- for i in range(frame + 1):
- ax.add_patch(polygons[i])
- ax.text(.1, .25,
- f"Sides: {n_sides}",
- fontsize=12,
- color='black',
- ha='left',
- va='top')
- ax.text(1, .25,
- f"A: {polygon_areas[frame]:.6f}",
- fontsize=12,
- color='black',
- ha='left',
- va='top')
- ax.text(2, .25,
- f"C: {polygon_prims[frame]:.6f}",
- fontsize=12,
- color='black',
- ha='left',
- va='top')
- polygons = []
- for polygon in range(num_sides_min, num_sides_max+1):
- shape_2 = RegularPolygon((1.5, 1.5),
- numVertices=polygon,
- radius=1,
- facecolor="None",
- linewidth=0.2,
- edgecolor=colors[polygon-3])
- polygons.append(shape_2)
- anim = FuncAnimation(fig,
- animate,
- frames=num_frames,
- interval=200,
- repeat=True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement