Wednesday, 16 April 2025

Wormhole Twist 3D Pattern using Python


 import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

theta = np.linspace(0, 25 * np.pi, 1500) 

z = np.linspace(-4, 4, 1500)              

r = 1 / (z**2 + 1) + 0.5  

x = r * np.sin(theta)

y = r * np.cos(theta)

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

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

ax.plot(x, y, z, color='lime', linewidth=2)

ax.scatter(x, y, z, c=theta, cmap='cool', s=2)

ax.set_title('Wormhole Twist 3D Pattern', fontsize=18, color='white')

ax.set_facecolor('black')

fig.patch.set_facecolor('black')

ax.grid(False)

ax.set_xticks([])

ax.set_yticks([])

ax.set_zticks([])

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy → For numerical calculations.

 matplotlib.pyplot → For plotting.

 Axes3D → To enable 3D plotting.

 2. Generating Data for the Spiral Structure

a) Theta → Angle for Rotation

theta = np.linspace(0, 25 * np.pi, 1500)

Generates 1500 values from 0 to 25π.

 Controls how many circular rotations (25 rotations).

 Bigger theta range = More twists.

 b) Z → Vertical Height

z = np.linspace(-4, 4, 1500)

Generates 1500 points from -4 to 4.

 Controls vertical length of the wormhole.

 c) Radius → Controls Distance from Center

r = 1 / (z**2 + 1) + 0.5

Formula gives a shape of a wormhole or funnel.

 Center is wider, ends are narrower.

 As z increases (top & bottom), r decreases.

 Why this formula?

 1 / (z² + 1) → Makes radius smaller at the top/bottom and bigger near the center (z=0).

 +0.5 → Adds minimum radius to avoid collapse to zero.

 3. Convert Polar to Cartesian Coordinates

x = r * np.sin(theta)

y = r * np.cos(theta)

Converts from (r, θ) to (x, y) for 3D plotting.

 4. Creating Plot Environment

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

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

Creates the figure and 3D axis.

5. Plotting the Wormhole Spiral Line

ax.plot(x, y, z, color='lime', linewidth=2)

Draws the spiral line.

 Color → lime green.

 Line thickness → 2.

 6. Adding Scatter Dots (Glow Effect)

ax.scatter(x, y, z, c=theta, cmap='cool', s=2)

Adds tiny dots along the spiral line.

 c=theta → Color depends on theta (angle).

 cmap='cool' → Gradient color (cyan to magenta).

 s=2 → Dot size.

 Effect:

Looks like energy particles swirling around the wormhole.

 7. Styling the Plot

ax.set_title('Wormhole Twist 3D Pattern', fontsize=18, color='white')

ax.set_facecolor('black')

fig.patch.set_facecolor('black')

ax.grid(False)

Adds the title in white color.

Sets background color to black for a space-like look.

Removes grid lines.

8. Removing Axis Ticks

ax.set_xticks([])

ax.set_yticks([])

ax.set_zticks([])

Hides x, y, z axis numbers for a clean visual design.

9. Display the Final 3D Plot

plt.show()

Displays 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)