Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating Patterns with Loops | Creative Coding and Generative Art
Python for Creators and Makers

bookCreating Patterns with Loops

Loops are fundamental building blocks in generative art, allowing you to create repeating and evolving patterns efficiently. By repeating actions many times with loops, you can draw grids, spirals, waves, and other complex structures with just a few lines of code. In visual design, loops help automate the placement and transformation of shapes, lines, or colors, enabling you to explore intricate designs that would be tedious to make by hand.

12345678910111213141516171819202122
import matplotlib.pyplot as plt # Set up the figure and axis fig, ax = plt.subplots(figsize=(6, 6)) # Define grid size rows = 10 cols = 10 # Use nested loops to draw a grid of circles for i in range(rows): for j in range(cols): # (j, i) determines the position of each circle circle = plt.Circle((j, i), 0.4, fill=False, edgecolor='black') ax.add_patch(circle) # Set axis limits and aspect ax.set_xlim(-1, cols) ax.set_ylim(-1, rows) ax.set_aspect('equal') ax.axis('off') plt.show()
copy

In the code above, you use two loops—one inside the other—to draw a grid pattern. The outer loop controls the rows, and the inner loop controls the columns. Each time the inner loop runs, it places a circle at a new column position in the current row. When the inner loop finishes, the outer loop moves to the next row, and the process repeats. This creates a regular grid of circles, where each loop corresponds to a dimension of the pattern: the row and the column. By changing the number of rows and columns, or by adjusting the position and size of each circle inside the loops, you can easily modify the density and style of the grid.

123456789101112131415161718
import matplotlib.pyplot as plt import numpy as np # Set up the figure and axis fig, ax = plt.subplots(figsize=(6, 6)) # Create a spiral by plotting points with increasing radius and angle num_points = 200 for i in range(num_points): angle = i * 0.2 radius = 0.1 * i x = radius * np.cos(angle) y = radius * np.sin(angle) ax.plot(x, y, 'o', color='blue', markersize=4) ax.set_aspect('equal') ax.axis('off') plt.show()
copy

1. Why are nested loops useful in pattern generation?

2. What visual effect can be achieved by varying loop parameters?

3. Fill in the blank: To create a grid of points, you use ________ loops.

question mark

Why are nested loops useful in pattern generation?

Select the correct answer

question mark

What visual effect can be achieved by varying loop parameters?

Select the correct answer

question-icon

Fill in the blank: To create a grid of points, you use ________ loops.

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how the spiral pattern is generated in the second code sample?

What are some ways to modify the spiral to create different effects?

How do loops help in creating more complex generative art patterns?

bookCreating Patterns with Loops

Stryg for at vise menuen

Loops are fundamental building blocks in generative art, allowing you to create repeating and evolving patterns efficiently. By repeating actions many times with loops, you can draw grids, spirals, waves, and other complex structures with just a few lines of code. In visual design, loops help automate the placement and transformation of shapes, lines, or colors, enabling you to explore intricate designs that would be tedious to make by hand.

12345678910111213141516171819202122
import matplotlib.pyplot as plt # Set up the figure and axis fig, ax = plt.subplots(figsize=(6, 6)) # Define grid size rows = 10 cols = 10 # Use nested loops to draw a grid of circles for i in range(rows): for j in range(cols): # (j, i) determines the position of each circle circle = plt.Circle((j, i), 0.4, fill=False, edgecolor='black') ax.add_patch(circle) # Set axis limits and aspect ax.set_xlim(-1, cols) ax.set_ylim(-1, rows) ax.set_aspect('equal') ax.axis('off') plt.show()
copy

In the code above, you use two loops—one inside the other—to draw a grid pattern. The outer loop controls the rows, and the inner loop controls the columns. Each time the inner loop runs, it places a circle at a new column position in the current row. When the inner loop finishes, the outer loop moves to the next row, and the process repeats. This creates a regular grid of circles, where each loop corresponds to a dimension of the pattern: the row and the column. By changing the number of rows and columns, or by adjusting the position and size of each circle inside the loops, you can easily modify the density and style of the grid.

123456789101112131415161718
import matplotlib.pyplot as plt import numpy as np # Set up the figure and axis fig, ax = plt.subplots(figsize=(6, 6)) # Create a spiral by plotting points with increasing radius and angle num_points = 200 for i in range(num_points): angle = i * 0.2 radius = 0.1 * i x = radius * np.cos(angle) y = radius * np.sin(angle) ax.plot(x, y, 'o', color='blue', markersize=4) ax.set_aspect('equal') ax.axis('off') plt.show()
copy

1. Why are nested loops useful in pattern generation?

2. What visual effect can be achieved by varying loop parameters?

3. Fill in the blank: To create a grid of points, you use ________ loops.

question mark

Why are nested loops useful in pattern generation?

Select the correct answer

question mark

What visual effect can be achieved by varying loop parameters?

Select the correct answer

question-icon

Fill in the blank: To create a grid of points, you use ________ loops.

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2
some-alt