次の手順を使用して、特定の範囲の正方形グリッドの特定の半径の六角形のポリゴン座標を計算しています (左下 --> 右上):
def calc_polygons(startx, starty, endx, endy, radius):
sl = (2 * radius) * math.tan(math.pi / 6)
# calculate coordinates of the hexagon points
p = sl * 0.5
b = sl * math.cos(math.radians(30))
w = b * 2
h = 2 * sl
origx = startx
origy = starty
# offsets for moving along and up rows
xoffset = b
yoffset = 3 * p
polygons = []
row = 1
counter = 0
while starty < endy:
if row % 2 == 0:
startx = origx + xoffset
else:
startx = origx
while startx < endx:
p1x = startx
p1y = starty + p
p2x = startx
p2y = starty + (3 * p)
p3x = startx + b
p3y = starty + h
p4x = startx + w
p4y = starty + (3 * p)
p5x = startx + w
p5y = starty + p
p6x = startx + b
p6y = starty
poly = [
(p1x, p1y),
(p2x, p2y),
(p3x, p3y),
(p4x, p4y),
(p5x, p5y),
(p6x, p6y),
(p1x, p1y)]
polygons.append(poly)
counter += 1
startx += w
starty += yoffset
row += 1
return polygons
これは、何百万ものポリゴンに対してはうまく機能しますが、大きなグリッドではすぐに遅くなります (そして非常に大量のメモリを消費します)。おそらく、範囲に基づいて計算された頂点の数の多い配列をまとめて圧縮し、ループを完全に削除することによって、これを最適化する方法があるかどうか疑問に思っています – ただし、私のジオメトリはこれには十分ではありません。改善を歓迎します。