Skip to content

Instantly share code, notes, and snippets.

@abdullahrkw
Last active July 30, 2023 18:52
Show Gist options
  • Save abdullahrkw/dba70fa490758f8a78ceaf15cb01e664 to your computer and use it in GitHub Desktop.
Save abdullahrkw/dba70fa490758f8a78ceaf15cb01e664 to your computer and use it in GitHub Desktop.
Drawing Torus or Donut as a Parametric surface in Python

You can follow the theory of parametric surfaces here

This is my python code that uses matplotlib and python

# %%
import numpy as np
import matplotlib.pyplot as plt

# %%
def plot_3d(points, points_color, title):
    x, y, z = points
    fig, ax = plt.subplots(
        figsize=(6, 6),
        facecolor="white",
        tight_layout=True,
        subplot_kw={"projection": "3d"},
    )
    fig.suptitle(title, size=16)
    col = ax.scatter(x, y, z, c=points_color, s=50, alpha=0.8)
    ax.set_zlim(ax.get_xlim())

    fig.colorbar(col, ax=ax, orientation="horizontal", shrink=0.6, aspect=60, pad=0.01)
    plt.show()

# %% Parametric Surface using Vector Valued Functions
t = np.linspace(0, 10, 300)
u_t, v_t = np.meshgrid(t,t)
x = (10+5*np.cos(u_t))*np.cos(v_t)
y = (10+5*np.cos(u_t))*np.sin(v_t)
z = 5*np.sin(u_t)

points = np.vstack((x.reshape(-1),y.reshape(-1),z.reshape(-1)))
color = points[2]
title = "Parametric Surfaces: Donut"

plot_3d(points, color, title)

# %%
@abdullahrkw
Copy link
Author

Donut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment