Thursday, 27 March 2025

Swirl Bloom Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

theta = np.linspace(0, 20 * np.pi, 2000)

radius = np.linspace(0, 10, 2000)

x = radius * np.cos(theta)

y = radius * np.sin(theta)

x += 0.5 * np.sin(10 * theta)

y += 0.5 * np.cos(10 * theta)

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

plt.plot(x, y, color='purple', linewidth=1)

plt.axis('off')

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Swirl Bloom Pattern')

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

NumPy is used for mathematical operations, especially for generating arrays using linspace and performing trigonometric calculations.

Matplotlib is used for plotting the pattern using plt.plot().

 2. Generating the Swirl with Parametric Equations

theta = np.linspace(0, 20 * np.pi, 2000)

radius = np.linspace(0, 10, 2000)

theta: This is the angle in radians, ranging from 0 to 20π.

20 * np.pi means the spiral will have 10 complete turns (since one complete turn is 2π).

2000 points ensure a smooth and continuous curve.

radius: This linearly increases from 0 to 10, creating an outward bloom effect.

 3. Creating the Swirl Shape

x = radius * np.cos(theta)

y = radius * np.sin(theta)

Parametric Equations:

x=rcos(θ)

y=rsin(θ)

These equations generate a spiral by gradually increasing the radius while rotating around the center.

 4. Adding a Wavy Distortion

x += 0.5 * np.sin(10 * theta)

y += 0.5 * np.cos(10 * theta)

This introduces a wave-like perturbation to the spiral, making it bloom or ripple.

np.sin(10 * theta) and np.cos(10 * theta) add oscillations to both the x and y coordinates.

The factor 10 controls the number of small wave ripples (10 cycles per spiral).

The amplitude 0.5 controls the size of the distortions.

This gives the pattern a swirling, blooming effect.

 5. Plotting the Pattern

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

plt.plot(x, y, color='purple', linewidth=1)

plt.figure(figsize=(8, 8)): Creates a square figure with a size of 8x8 inches.

plt.plot(): Plots the x and y coordinates with a purple color and a thin line (linewidth=1).

 6. Adjusting Aesthetics

plt.axis('off')

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Swirl Bloom Pattern')

plt.show()

plt.axis('off'): Hides the axis for a cleaner look.

plt.gca().set_aspect('equal', adjustable='box'): Maintains the aspect ratio to ensure the bloom shape is not distorted.

plt.title(): Displays the title.

plt.show(): Renders the plot.

 


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 (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) Data Strucures (8) 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 (1042) Python Coding Challenge (456) Python Quiz (117) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) 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)