3
我使用以下过程来计算给定半径的六边形的多边形坐标为给定程度的正方形栅格(左下 - >右上):计算六边形网格比较快的方式坐标
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
这适用于数百万个多边形,但对于大型网格会很快变慢(并占用大量内存)。我想知道是否有办法优化这一点,可能是通过将基于范围计算的numpy顶点数组一起压缩,然后完全移除这些循环 - 但是我的几何不够好,因此,任何建议为改进而受到欢迎。
这应该被迁移到codereview。 – 2014-11-01 17:53:57
其实这是一个很好的关于六角形网格的问题。列表中的多边形排序与您相关吗? – 2014-11-01 18:05:51
@MariaZverina不一定,没有。 – urschrei 2014-11-01 18:17:09