Tuesday, 27 May 2025

3D Conch Shell Structure using Python

 

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

theta = np.linspace(0, 8 * np.pi, 500)   

z = np.linspace(0, 2, 500)               

r = z**2 + 0.1                           

phi = np.linspace(0, 2 * np.pi, 50)

theta, phi = np.meshgrid(theta, phi)

z = np.linspace(0, 2, 500)

z = np.tile(z, (50, 1))

r = z**2 + 0.1

x = r * np.cos(theta) * np.cos(phi)

y = r * np.sin(theta) * np.cos(phi)

z = r * np.sin(phi)

fig = plt.figure(figsize=(6, 6))

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='copper', edgecolor='none', alpha=0.9)

ax.set_title('3D Conch Shell ', fontsize=14)

ax.set_box_aspect([1,1,1])

ax.axis('off') 

plt.show()

#source code --> clcoding.com


Code Explanation:

1. Import Libraries

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy is for numerical arrays and functions.

matplotlib is for plotting.

Axes3D is needed to enable 3D plotting in matplotlib. 

2. Define Spiral Parameters

theta_vals = np.linspace(0, 8 * np.pi, 500)  # Spiral angle: 0 to 8π

z_vals = np.linspace(0, 2, 500)              # Vertical height

phi_vals = np.linspace(0, 2 * np.pi, 50)     # Circular angle around shell's axis

theta_vals: Controls how many times the spiral wraps around — 4 full rotations.

z_vals: The height (like vertical position in the shell).

phi_vals: Describes the circle at each spiral point (the shell's cross-section). 

3. Create 2D Grids

theta, phi = np.meshgrid(theta_vals, phi_vals)

meshgrid creates a 2D grid so we can compute (x, y, z) coordinates across both theta and phi.

theta and phi will be shape (50, 500) so that we can build a surface. 

4. Define Radius and Z

z = np.linspace(0, 2, 500)

z = np.tile(z, (50, 1))       # Repeat z rows to shape (50, 500)

r = z**2 + 0.1                # Radius grows quadratically with height

z is tiled to match the (50, 500) shape of theta and phi.

r = z^2 + 0.1: Makes the radius increase faster as you go up, giving the shell its expanding spiral. 

5. Compute 3D Coordinates

x = r * np.cos(theta) * np.cos(phi)

y = r * np.sin(theta) * np.cos(phi)

z = r * np.sin(phi)

This is the parametric equation for a growing spiral swept by a circle.

theta sweeps the spiral.

phi creates a circular cross-section at each spiral point. 

The cos(phi) and sin(phi) give the circular shape.

r * cos(theta) and r * sin(theta) give the spiral path. 

6. Plotting the Shell

 fig = plt.figure(figsize=(10, 8))

ax = fig.add_subplot(111, projection='3d')

 ax.plot_surface(x, y, z, cmap='copper', edgecolor='none', alpha=0.9)

ax.set_title('3D Conch Shell Structure', fontsize=14)

ax.set_box_aspect([1,1,1])

ax.axis('off')

plt.show()

plot_surface(...): Renders a 3D surface using x, y, z.

 cmap='copper': Gives it a warm, shell-like color.

 ax.axis('off'): Hides the axis to enhance the visual aesthetics.

 set_box_aspect([1,1,1]): Keeps the plot from being stretched.


 


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)