Home > Blockchain >  Is it possible to create 2D hexagonal arrays just like the square arrays in Python?
Is it possible to create 2D hexagonal arrays just like the square arrays in Python?

Time:07-30

I was wondering if it is possible in Python to create a 2D hexagonal array whose elements are stored at the vertices of the hexagons. For example, in a 2D square array (NumPy array), the vertices of the squares represent the elements of the array.

CodePudding user response:

You would probably just use a normal 2d array like Hexagon map

So the center of the hexagon is (0, 0) or (1, 0) etc.

To get the corners you can use this function:

import math

def get_corners(coord):
    corners = []
    for angle in range(30, 330, 60):
        radians = math.radians(angle)
        corners.append((math.cos(radians)*0.5 coord[0], math.sin(radians)*0.5 coord[0]))
    return corners

The function returns a list of floats relative to the center.

Example for (0, 0): [(0.43301270189221935, 0.24999999999999997), (3.061616997868383e-17, 0.5), (-0.43301270189221935, 0.24999999999999997), (-0.4330127018922193, -0.25000000000000006), (-9.184850993605148e-17, -0.5)]

  • Related