Creating 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.
12345678910111213141516171819202122import 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()
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.
123456789101112131415161718import 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()
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.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 5.56
Creating Patterns with Loops
Glissez pour afficher le menu
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.
12345678910111213141516171819202122import 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()
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.
123456789101112131415161718import 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()
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.
Merci pour vos commentaires !