import numpy as np
import matplotlib.pyplot as plt
a = 1
b = 0.2
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 4 * np.pi, 100)
u, v = np.meshgrid(u, v)
X = a * (1 - v / (2 * np.pi)) * np.cos(v) * np.cos(u)
Y = a * (1 - v / (2 * np.pi)) * np.cos(v) * np.sin(u)
Z = a * (1 - v / (2 * np.pi)) * np.sin(v) + b * u
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='magma', edgecolor='k', alpha=0.9)
ax.set_title("3D Parametric Shell Surface")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
ax.set_box_aspect([1, 1, 1])
plt.tight_layout()
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy: Used for numerical computations and creating
arrays.
matplotlib.pyplot: Used for plotting 2D and 3D visualizations.
2. Define Constants for Shape Control
a = 1
b = 0.2
a: Controls the overall scale of the shell surface.
b: Adds a vertical twist/extension as the surface rotates.
3. Create Parameter Grids (u and v)
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 4 * np.pi, 100)
u, v = np.meshgrid(u, v)
u: Angle around the circular cross-section (like the
shell's rim).
v: Controls the spiral motion (shell’s growth).
np.meshgrid: Creates a grid for evaluating parametric equations over 2D domains.
4. Define Parametric Equations
X = a * (1 - v / (2 * np.pi)) * np.cos(v) *
np.cos(u)
Y = a * (1 - v / (2 * np.pi)) * np.cos(v) *
np.sin(u)
Z = a * (1 - v / (2 * np.pi)) * np.sin(v) + b * u
These equations generate a spiraling and shrinking
shell surface:
X and Y: Determine the radial coordinates; they shrink over v.
Z: Adds a vertical twist via b * u, and height via sin(v).
The structure gets smaller as v increases, mimicking how a shell tightens inward.
5. Set Up 3D Plotting
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
Initializes a 3D plot.
figsize=(10, 8): Sets the size of the figure.
6. Plot the Shell Surface
ax.plot_surface(X, Y, Z, cmap='magma',
edgecolor='k', alpha=0.9)
plot_surface: Draws the parametric surface in 3D.
cmap='magma': Uses a vibrant color map.
edgecolor='k': Adds black edges for clarity.
alpha=0.9: Slight transparency for smoother visuals.
7. Customize Plot Appearance
ax.set_title("3D Parametric Shell
Surface")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
ax.set_box_aspect([1, 1, 1])
Adds axis labels and title.
8. Display the Plot
plt.tight_layout()
plt.show()
tight_layout(): Avoids label overlaps.
show(): Renders the 3D plot.


0 Comments:
Post a Comment