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.
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).
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.
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(θ)=(R+r⋅cos(qθ))⋅cos(pθ)
This equation describes the horizontal position of
the point as it winds around the torus.
y(θ)=(R+r⋅cos(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(θ)=r⋅sin(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.
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
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()
fig = plt.figure(figsize=(8, 8))
Creates a figure of size 8x8 inches to hold the
plot.
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.
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.
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.
plt.show()
Displays the plot in a new window.


0 Comments:
Post a Comment