Sunday, 30 March 2025

Arc Twirl Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

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

r = 1 + 0.2 * np.sin(5 * theta)

x = r * np.cos(theta)

y = r * np.sin(theta)

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

plt.plot(x, y, color='teal', linewidth=2)

for i in range(1, 6):

    arc_theta = np.linspace(0, 2 * np.pi / i, 300)

    arc_r = 0.8 + 0.1 * i

    arc_x = arc_r * np.cos(arc_theta)

    arc_y = arc_r * np.sin(arc_theta)

    plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6)

plt.gca().set_aspect('equal')

plt.axis('off')

plt.title('Arc Twirl Pattern')

plt.show()

#source code --> clcoding.com 


Code Explanation:

1. Import Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: Used for numerical operations like generating a range of values for the angles and radii.

matplotlib.pyplot: Used for plotting and visualizing the pattern.

2. Define Parameters for the Arc Twirl Effect

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

r = 1 + 0.2 * np.sin(5 * theta)

theta: Creates a range of 1000 values between 0 and 6 * np.pi (approximately 18.85). This is the angle parameter used to generate the spiral.

r: The radius at each angle theta is calculated. The formula 1 + 0.2 * np.sin(5 * theta) introduces oscillations to the radius as theta increases, which creates a twirling effect.

3. Convert Polar Coordinates to Cartesian Coordinates

x = r * np.cos(theta)

y = r * np.sin(theta)

x and y: Convert the polar coordinates (r, theta) into Cartesian coordinates (x, y) using the formulas:

x = r * cos(theta)

y = r * sin(theta)

These are the coordinates needed to plot the spiral pattern on a 2D graph.

4. Create the Plot

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

plt.plot(x, y, color='teal', linewidth=2)

plt.figure(figsize=(7, 7)): Creates a new figure with a square aspect ratio of 7x7 inches.

plt.plot(x, y, color='teal', linewidth=2): Plots the spiral defined by the x and y values with a teal color and a line width of 2. This creates the main spiral pattern.

5. Adding Arcs with Variation

for i in range(1, 6):

    arc_theta = np.linspace(0, 2 * np.pi / i, 300)

    arc_r = 0.8 + 0.1 * i

    arc_x = arc_r * np.cos(arc_theta)

    arc_y = arc_r * np.sin(arc_theta)

    plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6)

for i in range(1, 6): A loop that generates 5 additional arc patterns, each with a different radius and number of points.

arc_theta: For each arc, arc_theta is a range of angles from 0 to 2 * np.pi / i with 300 points, which defines how much of a full circle the arc will cover.

arc_r: The radius for each arc starts at 0.8 and increases by 0.1 * i in each iteration. This creates expanding arcs with each loop iteration.

arc_x and arc_y: These calculate the x and y Cartesian coordinates for the arcs.

plt.plot(arc_x, arc_y, color='darkcyan', linewidth=1.5, alpha=0.6): Plots the arcs using a darkcyan color, with a line width of 1.5 and transparency (alpha=0.6) to give them a subtle appearance, allowing the main spiral to remain prominent.

6. Styling

plt.gca().set_aspect('equal')

plt.axis('off')

plt.title("Arc Twirl", fontsize=20, color='teal')

plt.gca().set_aspect('equal'): Sets the aspect ratio to "equal," meaning the units on the x and y axes are scaled equally. This ensures that the arcs and spiral remain circular rather than stretched.

plt.axis('off'): Turns off the axis lines and labels for a clean look.

plt.title("Arc Twirl", fontsize=20, color='teal'): Adds a title to the plot "Arc Twirl" in a teal color with a font size of 20.

7. Display the Plot

plt.show()

plt.show(): Displays the final plot with all the arcs and spiral patterns.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (2) data management (12) Data Science (148) Data Strucures (9) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1047) Python Coding Challenge (456) Python Quiz (118) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)