Created
January 6, 2025 23:56
-
-
Save trbarron/e0faa616a4b89e1ab4e3f0fb08159920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from matplotlib import pyplot as plt | |
| import numpy as np | |
| from matplotlib.animation import FuncAnimation | |
| import matplotlib.animation as animation | |
| sheet_length = 4 | |
| l_values = [] | |
| packing_factors = [] | |
| def get_height(L): | |
| return np.sqrt(4 - (L/2)**2) | |
| def plot_frame(L): | |
| plt.clf() | |
| ax1 = plt.subplot(211) | |
| ax2 = plt.subplot(212) | |
| ax1.set_facecolor('#f0f0f0') | |
| height = get_height(L) | |
| max_height = get_height(2.01) | |
| # Bottom circles | |
| for i in range(-1, sheet_length * 2): | |
| bot_circle = plt.Circle((i * L, 0), 1, fill=True, color='royalblue') | |
| bot_circle_edge = plt.Circle((i * L, 0), 1, fill=False, color='navy', linewidth=1.5, alpha=0.8) | |
| ax1.add_artist(bot_circle) | |
| ax1.add_artist(bot_circle_edge) | |
| # Top circles | |
| for i in range(sheet_length * 2): | |
| top_circle = plt.Circle((i * L - L/2, height), 1, fill=True, color='indianred') | |
| top_circle_edge = plt.Circle((i * L - L/2, height), 1, fill=False, color='darkred', linewidth=1.5, alpha=0.8) | |
| ax1.add_artist(top_circle) | |
| ax1.add_artist(top_circle_edge) | |
| # Bottom rectangle | |
| bot_rect = plt.Rectangle((-100, 0), 300, -100, fill=True, color='royalblue') | |
| ax1.add_artist(bot_rect) | |
| # Top rectangle | |
| top_rect = plt.Rectangle((-100, height), 300, 100, fill=True, color='indianred') | |
| ax1.add_artist(top_rect) | |
| packing_factor = L * height - np.pi * 1**2 | |
| l_values.append(L) | |
| packing_factors.append(packing_factor) | |
| max_width = 3.45 * sheet_length - 3.45/2 | |
| ax1.set_xlim(-1, max_width) | |
| ax1.set_ylim(-0.1, max_height + 0.1) | |
| ax1.set_aspect('equal', adjustable='box') | |
| ax1.grid(True, linestyle='--', alpha=0.3) | |
| ax1.set_title(f'Packing Factor: {packing_factor:.3f}\nL = {L:.3f}', | |
| pad=20, fontsize=10) | |
| ax2.clear() | |
| ax2.plot(l_values, packing_factors, 'b-', label='Packing Factor') | |
| ax2.set_xlim(2.01, 3.45) | |
| ax2.set_ylim(0, max(packing_factors) * 1.1 if packing_factors else 5) | |
| ax2.set_xlabel('L Value') | |
| ax2.set_ylabel('Packing Factor') | |
| ax2.grid(True, linestyle='--', alpha=0.3) | |
| ax2.set_title('Packing Factor vs L', pad=20, fontsize=10) | |
| plt.tight_layout() | |
| return packing_factor | |
| fig = plt.figure(figsize=(12, 7), dpi=60) | |
| def animate(frame): | |
| L = 2.01 + frame * (3.45 - 2.01) / 79 | |
| plot_frame(L) | |
| anim = FuncAnimation(fig, animate, frames=80, interval=100) | |
| writer = animation.PillowWriter(fps=8) | |
| anim.save('packing_animation.gif', writer=writer) | |
| plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment