Sunday, 2 March 2025

Octagonal grid pattern plot Using Python



 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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) 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 (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) 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

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)