import matplotlib.pyplot as plt
import numpy as np
# Set up the figure and axis
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(-1.2, 1.2)
ax.set_ylim(0, 1.2)
ax.set_aspect('equal')
ax.axhline(y=0, color='k', linewidth=0.5) # Base line
ax.axvline(x=0, color='k', linewidth=0.5) # Axis
# Parameters
r = 1.0 # Radius/height for simplicity
# 1. Cylinder cross-section: Rectangle from y=0 to y=r, x=-r to x=r
ax.add_patch(plt.Rectangle((-r, 0), 2*r, r, fill=False, edgecolor='blue', linewidth=2, label='Cylinder'))
# 2. Cone cross-section: Triangle from base at y=0 (x=-r to r) to apex at y=r (x=0)
ax.plot([-r, 0, r], [0, r, 0], color='red', linewidth=2, label='Cone')
# 3. Hemisphere cross-section: Semicircle with diameter at y=r (x=-r to r), curving down to point at y=0 (x=0)
theta = np.linspace(np.pi, 2*np.pi, 100) # From pi to 2pi for lower semicircle
x_semi = r * np.cos(theta)
y_semi = r + r * np.sin(theta) # Shift up by r so it starts at y=r and goes down to y=0
ax.plot(x_semi, y_semi, color='green', linewidth=2, label='Hemisphere (pole-down)')
# Labels and ticks for heights
ax.set_xticks([-r, 0, r])
ax.set_xticklabels(['-r', 'Axis', 'r'])
ax.set_yticks([0, 0.5*r, r])
ax.set_yticklabels(['h=0 (Base)', 'h=r/2', 'h=r (Top)'])
ax.set_xlabel('Width (2r total)')
ax.set_ylabel('Height h')
ax.set_title("Vertical Cross-Sections: Cone + Hemisphere = Cylinder\n(Archimedes' Cavalieri Setup)")
# Legend
ax.legend(loc='upper right')
# Fill to show overlap (optional: fills the cone and hemisphere lightly)
ax.fill([-r, 0, r], [0, r, 0], alpha=0.3, color='red') # Cone fill
ax.fill_between(x_semi, y_semi, 0, alpha=0.3, color='green') # Hemisphere fill (from curve to base, but since base is at 0, approximate)
plt.grid(True, alpha=0.3)
plt.show()
No comments:
Post a Comment