Created
August 16, 2023 01:37
-
-
Save tmthydvnprt/54edb497649f3b4cb18142182be438a9 to your computer and use it in GitHub Desktop.
Python Spirals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import division | |
| import matplotlib.pyplot as plt | |
| from mpl_toolkits.mplot3d import Axes3D | |
| import numpy as np | |
| from scipy.special import fresnel | |
| theta = np.linspace(0, 10 * np.pi, 1000) | |
| # Archimedean Spiral | |
| a, b = 0, 1 | |
| r_archimedean = a + b * theta | |
| x_archimedean = r_archimedean * np.cos(theta) | |
| y_archimedean = r_archimedean * np.sin(theta) | |
| # Logarithmic Spiral | |
| a, b = 1, 0.1 | |
| r_logarithmic = a * np.exp(b * theta) | |
| x_logarithmic = r_logarithmic * np.cos(theta) | |
| y_logarithmic = r_logarithmic * np.sin(theta) | |
| # Fermat's Spiral | |
| r_fermat = np.sqrt(theta) | |
| x_fermat = r_fermat * np.cos(theta) | |
| y_fermat = r_fermat * np.sin(theta) | |
| # Hyperbolic Spiral | |
| a = 1 | |
| r_hyperbolic = a / theta | |
| x_hyperbolic = r_hyperbolic * np.cos(theta) | |
| y_hyperbolic = r_hyperbolic * np.sin(theta) | |
| # Lituus Spiral | |
| a = 1 | |
| r_lituus = np.sqrt(a**2 / theta) | |
| x_lituus = r_lituus * np.cos(theta) | |
| y_lituus = r_lituus * np.sin(theta) | |
| # Golden Spiral | |
| phi = (1 + np.sqrt(5)) / 2 # Golden Ratio | |
| a = 1 | |
| b = 0.306349 # Related to the golden ratio, b = (2 * pi) / log(phi) | |
| r_golden = a * np.exp(b * theta) | |
| x_golden = r_golden * np.cos(theta) | |
| y_golden = r_golden * np.sin(theta) | |
| # Cornu Spiral | |
| t_cornu = np.linspace(-np.sqrt(np.pi), np.sqrt(np.pi), 1000) | |
| x_cornu, y_cornu = fresnel(t_cornu) | |
| # Spherical Spiral | |
| # Define the parameters | |
| phi_spherical = np.linspace(0, 20 * np.pi, 1000) # Angle with respect to the z-axis | |
| theta_spherical = phi_spherical * np.tan(np.radians(45)) # Constant angle with respect to a fixed diameter | |
| r_spherical = 1 # Radius of the sphere | |
| # Convert to Cartesian coordinates | |
| x_spherical = r_spherical * np.sin(phi_spherical) * np.cos(theta_spherical) | |
| y_spherical = r_spherical * np.sin(phi_spherical) * np.sin(theta_spherical) | |
| z_spherical = r_spherical * np.cos(phi_spherical) | |
| # Sinusoidal Spiral | |
| a_sinusoidal = 1 | |
| k_sinusoidal = 2 | |
| r_sinusoidal = a_sinusoidal * np.cos(k_sinusoidal * theta) | |
| x_sinusoidal = r_sinusoidal * np.cos(theta) | |
| y_sinusoidal = r_sinusoidal * np.sin(theta) | |
| # Catenary Spiral | |
| t_catenary = np.linspace(-5, 5, 1000) | |
| a_catenary = 1 | |
| b_catenary = 1 | |
| x_catenary = a_catenary * np.cosh(t_catenary / a_catenary) | |
| y_catenary = b_catenary * t_catenary | |
| # Display | |
| fig, axes = plt.subplots(5, 2, figsize=(10, 10)) | |
| axes[0, 0].plot(x_archimedean, y_archimedean) | |
| axes[0, 0].set_title("Archimedean Spiral") | |
| axes[0, 0].axis("equal") | |
| axes[0, 1].plot(x_logarithmic, y_logarithmic) | |
| axes[0, 1].set_title("Logarithmic Spiral") | |
| axes[0, 1].axis("equal") | |
| axes[1, 0].plot(x_fermat, y_fermat) | |
| axes[1, 0].set_title("Fermat's Spiral") | |
| axes[1, 0].axis("equal") | |
| axes[1, 1].plot(x_hyperbolic, y_hyperbolic) | |
| axes[1, 1].set_title("Hyperbolic Spiral") | |
| axes[1, 1].axis("equal") | |
| axes[2, 0].plot(x_lituus, y_lituus) | |
| axes[2, 0].set_title("Lituus Spiral") | |
| axes[2, 0].axis("equal") | |
| axes[2, 1].plot(x_golden, y_golden) | |
| axes[2, 1].set_title("Golden Spiral") | |
| axes[2, 1].axis("equal") | |
| axes[3, 0].plot(x_cornu, y_cornu) | |
| axes[3, 0].set_title("Cornu Spiral") | |
| axes[3, 0].axis("equal") | |
| axes[3, 1].cla() | |
| ax_spherical = fig.add_subplot(5, 2, 8, projection='3d') | |
| ax_spherical.plot(x_spherical, y_spherical, z_spherical) | |
| ax_spherical.set_title("Spherical Spiral") | |
| axes[4, 0].plot(x_sinusoidal, y_sinusoidal) | |
| axes[4, 0].set_title("Sinusoidal Spiral") | |
| axes[4, 0].axis("equal") | |
| axes[4, 1].plot(x_catenary, y_catenary) | |
| axes[4, 1].set_title("Catenary Spiral") | |
| axes[4, 1].axis("equal") | |
| plt.tight_layout() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment