top of page

How to generate your first algorithmic art!

Writer's picture: Samvar ShahSamvar Shah

Updated: Nov 15, 2024

Sample code and customizations provided to create your own masterpiece.

Creating math art with Python can be fun and creative! A simple approach is to use mathematical equations and plotting libraries to generate visually interesting patterns. In this example, we'll use the popular libraries numpy for mathematical operations and matplotlib for plotting the result.


import numpy as np

import matplotlib.pyplot as plt


# Function to generate the spirograph curve

def spirograph(R, r, d, t):

"""

R: Radius of the big circle

r: Radius of the small circle

d: Distance from the center of the small circle to the pen point

t: Array of angles (time points)

"""

x = (R - r) * np.cos(t) + d * np.cos((R - r) / r * t)

y = (R - r) * np.sin(t) - d * np.sin((R - r) / r * t)

return x, y


# Parameters for the spirograph

R = 10 # Radius of the big circle

r = 4 # Radius of the small circle

d = 5 # Pen offset

t = np.linspace(0, 4 * np.pi, 1000) # Time array (angles)


# Generate the spirograph curve

x, y = spirograph(R, r, d, t)


# Plotting the spirograph

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

plt.plot(x, y, color='b', lw=1)

plt.axis('equal')

plt.title('Math Art: Spirograph')

plt.axis('off') # Turn off the axes for a cleaner image


Now, you can play with this basic code and create endless customizations and variations.


Explanation:

  1. Parametric Equations: The function spirograph() computes the x and y coordinates of a point on the curve at each time step t, using the equations for a spirograph.

  2. Parameters:

    • R is the radius of the big circle.

    • r is the radius of the small circle that rolls around the big circle.

    • d is the distance from the center of the small circle to the pen (which draws the curve).

  3. Plot: The generated curve is plotted using matplotlib, and the axes are turned off for a cleaner look.

Customization:

  • You can change the values of R, r, and d to generate different kinds of spirographs.

  • By varying the range of t, you can change the number of rotations and the density of the curve.


Let me show a more complex variation of the above spirograph code.

I will use the same code as above but make some modifications:


  1. Add multiple layers of spirographs with different parameters.

  2. Use different colors for each layer to create a more vibrant and artistic output.

  3. Introduce transparency or random color variations to make it more visually dynamic.


import numpy as np

import matplotlib.pyplot as plt

import random


# Function to generate the spirograph curve

def spirograph(R, r, d, t):

"""

R: Radius of the big circle

r: Radius of the small circle

d: Distance from the center of the small circle to the pen point

t: Array of angles (time points)

"""

x = (R - r) * np.cos(t) + d * np.cos((R - r) / r * t)

y = (R - r) * np.sin(t) - d * np.sin((R - r) / r * t)

return x, y


# Parameters for the spirograph

t = np.linspace(0, 10 * np.pi, 2000) # Time array (angles)


# Function to generate a random color

def random_color():

return [random.random(), random.random(), random.random()]


# Plotting the complex spirograph with multiple layers

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


# Create multiple layers with different parameters

for _ in range(10): # Create 10 layers with different parameters

R = random.randint(6, 12) # Big circle radius

r = random.randint(2, 6) # Small circle radius

d = random.randint(1, 6) # Pen offset

color = random_color() # Random color for each layer

# Generate the spirograph curve

x, y = spirograph(R, r, d, t)

# Plot each curve with random color

plt.plot(x, y, color=color, lw=1, alpha=0.7) # Alpha for transparency


# Final touches for a cleaner look

plt.axis('equal')

plt.title('Advanced Math Art: Complex Spirograph')

plt.axis('off') # Turn off the axes for a cleaner image


  • You can adjust the number of layers by changing the range(10) in the loop. More layers will result in a more complex image.

  • You can also tweak the ranges of R, r, and d to further vary the patterns.

  • The alpha parameter controls the transparency. Lower values (e.g., alpha=0.3) will make the curves more transparent, allowing the background to show through more, while higher values will make them more opaque.


You can try experimenting with other kinds of mathematical curves, such as Lissajous curves, polar plots, or fractals, to create different types of math art.


So why don't you run the above code, make some modifications and generate your own algorithmic art! Do not forget to send your creations to the Algorithmic Art Competition 2024 to win some fantastic prizes! Deadline is Nov 23, 2024.


179 views5 comments

Recent Posts

See All

5 commentaires

Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
🥇
15 nov. 2024
Noté 5 étoiles sur 5.

Nice I will try to participate btw for that I have to learn python first

J'aime
Samvar
15 nov. 2024
En réponse à

Great! Feel free to use the python code provided and make customizations. Also, you can use any other programming language as well.

Modifié
J'aime

corona
13 nov. 2024
Noté 5 étoiles sur 5.

This seems doable for someone with no maths skill which is great. Thank you.

J'aime

Orz
13 nov. 2024
Noté 5 étoiles sur 5.

Excited!

J'aime

Invité
13 nov. 2024
Noté 4 étoiles sur 5.

Helpful thonk

J'aime
bottom of page