Sunday, 20 April 2025

Torus Knot Pattern using Python

 


import matplotlib.pyplot as plt

import numpy as np

from mpl_toolkits.mplot3d import Axes3D

R=3

r=1

p=2

q=3

theta=np.linspace(0,2*np.pi,1000)

x=(R+r*np.cos(q*theta))*np.cos(p*theta)

y=(R+r*np.cos(q*theta))*np.sin(p*theta)

z=r*np.sin(q*theta)

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

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

ax.plot(x,y,z,color='purple',lw=2)

ax.set_xlabel('X Axis')

ax.set_ylabel('Y Axis')

ax.set_zlabel('Z Axis')

ax.set_title('Torus Knot',fontsize=16)

plt.show()

#source code --> clcoding.com

Code Explanation:

Imports:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy: Used for numerical calculations and to generate the array of angles (theta) for the parametric equations.

 matplotlib.pyplot: Used to plot the 3D torus knot.

 mpl_toolkits.mplot3d: Provides the necessary tools to create 3D plots in matplotlib.

 Parameters:

R = 3

r = 1

p = 2 

q = 3

R: The radius of the central circle of the torus. This determines the distance from the center of the hole to the center of the tube (doughnut shape).

 r: The radius of the tube itself. This controls the thickness of the torus.

 p and q: These are the parameters that determine how many times the knot will wind around the tube and the hole of the torus.

 p: The number of times the knot winds around the tube of the torus.

 q: The number of times the knot winds around the central hole of the torus.

 In this case, the values are set as:

 p = 2: The knot will loop around the tube 2 times.

 q = 3: The knot will loop around the hole 3 times.

 Parameter Array (theta):

theta = np.linspace(0, 2 * np.pi, 1000)

theta is the angle parameter that will range from 0 to 2π, which represents one full rotation around the circle.

 The np.linspace(0, 2 * np.pi, 1000) generates 1000 evenly spaced values between 0 and 2π, which will be used to plot the curve.

 Parametric Equations for the Torus Knot:

x = (R + r * np.cos(q * theta)) * np.cos(p * theta)

y = (R + r * np.cos(q * theta)) * np.sin(p * theta)

z = r * np.sin(q * theta)

The 3D coordinates (x, y, z) are computed using parametric equations. These equations define the position of each point on the knot:

 X-coordinate (x):

x(θ)=(R+rcos(qθ))cos(pθ)

This equation describes the horizontal position of the point as it winds around the torus.

 Y-coordinate (y):

y(θ)=(R+rcos(qθ))sin(pθ)

This equation describes the vertical position of the point, complementing the X-coordinate to form the loop around the torus.

Z-coordinate (z):

z(θ)=rsin(qθ)

This equation determines the height of the point relative to the horizontal plane. It ensures the knot's winding around the hole of the torus.

 Plotting the Torus Knot:

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

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

 ax.plot(x, y, z, color='purple', lw=2)

ax.set_title(f'Torus Knot (p={p}, q={q})', fontsize=16)

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

plt.show()

 Creating the Figure:

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

Creates a figure of size 8x8 inches to hold the plot.

 Creating a 3D Axes Object:

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

Adds a 3D subplot to the figure. The projection='3d' argument specifies that the axes should be 3-dimensional.

 Plotting the Knot:

ax.plot(x, y, z, color='purple', lw=2)

Plots the points (x, y, z) on the 3D axes. The curve is colored purple, and the line width is set to 2.

 Setting Labels and Title:

ax.set_title(f'Torus Knot (p={p}, q={q})', fontsize=16)

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_title(): Sets the title of the plot, which includes the values of p and q to indicate which specific torus knot is being displayed.

 ax.set_xlabel(), ax.set_ylabel(), ax.set_zlabel(): Labels the X, Y, and Z axes respectively.

 Displaying the Plot:

plt.show()

Displays the plot in a new window.


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)