
import matplotlib.pyplot as plt
import numpy as np
def draw_octagon(ax, center, size):
"""Draws an octagon given a center and size."""
angles = np.linspace(0, 2 * np.pi, 9)[:-1] + np.pi / 8
x = center[0] + size * np.cos(angles)
y = center[1] + size * np.sin(angles)
ax.plot(np.append(x, x[0]), np.append(y, y[0]), 'b')
def draw_octagonal_grid(rows, cols, size=1.0):
"""Generates a grid of octagons."""
fig, ax = plt.subplots(figsize=(8, 8))
dx = 2 * size * np.cos(np.pi / 8)
dy = 2 * size * np.sin(np.pi / 8) + size
for row in range(rows):
for col in range(cols):
x_offset = col * dx
y_offset = row * dy - (col % 2) * (dy / 2)
draw_octagon(ax, (x_offset, y_offset), size)
ax.set_aspect('equal')
ax.axis('off')
plt.title('octagonal pattern plot')
plt.show()
rows, cols = 5, 5
draw_octagonal_grid(rows, cols)
#source code --> clcoding.com
Code Explanation:
1. Import Required Libraries
import matplotlib.pyplot as plt
import numpy as np
matplotlib.pyplot → Used for plotting the octagons.
numpy → Used for numerical calculations (especially trigonometric functions for octagon vertices).
2. Function to Draw a Single Octagon
def draw_octagon(ax, center, size):
"""Draws an octagon given a center and size."""
This function draws a single octagon at the specified center with a given size.
angles = np.linspace(0, 2 * np.pi, 9)[:-1] + np.pi / 8
np.linspace(0, 2 * np.pi, 9) → Generates 9 angles from 0 to
2π (full circle).
[:-1] → Removes the last element, keeping 8 points (for an octagon).
+ np.pi / 8 → Rotates the octagon slightly for proper orientation.
x = center[0] + size * np.cos(angles)
y = center[1] + size * np.sin(angles)
Computes the x and y coordinates of the octagon vertices using trigonometric functions:
np.cos(angles) → Calculates x-coordinates.
np.sin(angles) → Calculates y-coordinates.
ax.plot(np.append(x, x[0]), np.append(y, y[0]), 'b')
np.append(x, x[0]) → Connects the first and last points to close the octagon.
ax.plot(..., 'b') → Plots the octagon outline using a blue ('b') color.
3. Function to Draw an Octagonal Grid
def draw_octagonal_grid(rows, cols, size=1.0):
"""Generates a grid of octagons."""
Defines a function to generate a grid of octagons with given rows, columns, and octagon size.
fig, ax = plt.subplots(figsize=(8, 8))
Creates a figure (fig) and axes (ax) with a size of 8×8 inches.
dx = 2 * size * np.cos(np.pi / 8)
dy = 2 * size * np.sin(np.pi / 8) + size
dx (horizontal spacing) → Uses cosine to calculate the horizontal distance between octagons.
dy (vertical spacing) → Uses sine and size to set the vertical gap between rows.
for row in range(rows):
for col in range(cols):
Loops through the grid with rows × cols iterations to draw multiple octagons.
x_offset = col * dx
y_offset = row * dy - (col % 2) * (dy / 2)
x_offset = col * dx → Determines the x-coordinate for each octagon.
y_offset = row * dy - (col % 2) * (dy / 2) →
Adjusts vertical alignment so alternate columns are slightly shifted downwards, creating a staggered pattern.
draw_octagon(ax, (x_offset, y_offset), size)
Calls draw_octagon() to draw an octagon at each calculated position.
ax.set_aspect('equal')
Ensures equal scaling so the octagons do not appear distorted.
ax.axis('off')
Hides the x and y axes for a clean visual representation.
plt.title('octagonal pattern plot')
Sets the plot title.
plt.show()
Displays the final grid of octagons.
4. Function Call
rows, cols = 5, 5
draw_octagonal_grid(rows, cols)
0 Comments:
Post a Comment