Saturday, 10 May 2025

3D Parametric Shell Surface using Python

 


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

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)