Advertisement
Guest User

Untitled

a guest
Jun 8th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. from math import sin, cos, pi
  2. import matplotlib.pyplot as plt
  3. from matplotlib.patches import Circle, RegularPolygon
  4. from matplotlib.animation import FuncAnimation
  5. from matplotlib import colormaps as cm
  6. import matplotlib.colors as mplcolors
  7.  
  8.  
  9. RADIUS_C = 1
  10.  
  11. num_sides = [i for i in range(3, 11)]
  12. num_sides_min = min(num_sides)
  13. num_sides_max = max(num_sides)
  14. num_frames = len(num_sides)
  15.  
  16. cmap = cm.get_cmap("winter")
  17. colors = [mplcolors.to_hex(cmap(i)) for i in range(num_frames)]
  18.  
  19. polygon_areas = []
  20. polygon_prims = []
  21. for n_side in num_sides:
  22.     polygon_areas.append(n_side * RADIUS_C**2 * sin(pi /n_side) * cos(pi / n_side))
  23.     polygon_prims.append(2 * n_side * RADIUS_C * sin(pi / n_side))
  24.  
  25. fig, ax = plt.subplots(figsize=(6, 6))
  26.  
  27.  
  28. def create_circle():
  29.     shape_1 = Circle((1.5, 1.5),
  30.                      radius=RADIUS_C,
  31.                      fill=False,
  32.                      linewidth=0.2,
  33.                      edgecolor="red")
  34.     ax.add_patch(shape_1)
  35.  
  36.  
  37. def animate(frame):
  38.     ax.clear()
  39.     ax.axis([0, 3, 0, 3])
  40.     create_circle()
  41.     n_sides = frame + 3
  42.     for i in range(frame + 1):
  43.         ax.add_patch(polygons[i])
  44.     ax.text(.1, .25,
  45.             f"Sides: {n_sides}",
  46.             fontsize=12,
  47.             color='black',
  48.             ha='left',
  49.             va='top')
  50.     ax.text(1, .25,
  51.             f"A: {polygon_areas[frame]:.6f}",
  52.             fontsize=12,
  53.             color='black',
  54.             ha='left',
  55.             va='top')
  56.     ax.text(2, .25,
  57.             f"C: {polygon_prims[frame]:.6f}",
  58.             fontsize=12,
  59.             color='black',
  60.             ha='left',
  61.             va='top')
  62.  
  63.  
  64. polygons = []
  65. for polygon in range(num_sides_min, num_sides_max+1):
  66.     shape_2 = RegularPolygon((1.5, 1.5),
  67.                              numVertices=polygon,
  68.                              radius=1,
  69.                              facecolor="None",
  70.                              linewidth=0.2,
  71.                              edgecolor=colors[polygon-3])
  72.     polygons.append(shape_2)
  73.  
  74. anim = FuncAnimation(fig,
  75.                      animate,
  76.                      frames=num_frames,
  77.                      interval=200,
  78.                      repeat=True)
  79.  
  80. plt.show()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement