File size: 812 Bytes
8332c01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import matplotlib.pyplot as plt
import numpy as np

def draw_hexagonal_grid(rows, cols, text):
    fig, ax = plt.subplots()
    ax.set_aspect('equal')

    # Create a hexagonal lattice
    for i in range(rows):
        for j in range(cols):
            if i % 2 == 0:
                y = i * np.sqrt(3) / 2
                x = j * 3 / 2
            else:
                y = (i + 0.5) * np.sqrt(3) / 2
                x = (j + 0.5) * 3 / 2
            ax.add_patch(plt.RegularPolygon((x, y), numVertices=6, radius=0.5, orientation=np.pi/6, fill=None))

            # Add text inside each cell
            ax.text(x, y, text[i * cols + j], ha='center', va='center')

    ax.autoscale_view()
    ax.axis('off')
    plt.show()

# Example usage
text = "HELLO"
rows = 5
cols = 5
draw_hexagonal_grid(rows, cols, text)