6

我正在使用以下过程为给定范围(左下->右上)的方形网格计算给定半径的六边形多边形坐标:

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 数组压缩在一起,并完全删除循环——然而,我的几何结构还不够好,所以有什么建议欢迎改进。

4

2 回答 2

4

将问题分解为规则的方形网格(不连续)。一个列表将包含所有移位的十六进制(即偶数行),另一个将包含未移位(直)的行。

def calc_polygons_new(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


    # offsets for moving along and up rows
    xoffset = b
    yoffset = 3 * p

    row = 1

    shifted_xs = []
    straight_xs = []
    shifted_ys = []
    straight_ys = []

    while startx < endx:
        xs = [startx, startx, startx + b, startx + w, startx + w, startx + b, startx]
        straight_xs.append(xs)
        shifted_xs.append([xoffset + x for x in xs])
        startx += w

    while starty < endy:
        ys = [starty + p, starty + (3 * p), starty + h, starty + (3 * p), starty + p, starty, starty + p]
        (straight_ys if row % 2 else shifted_ys).append(ys)
        starty += yoffset
        row += 1

    polygons = [zip(xs, ys) for xs in shifted_xs for ys in shifted_ys] + [zip(xs, ys) for xs in straight_xs for ys in straight_ys]
    return polygons

正如您所预测的,压缩会带来更快的性能,尤其是对于较大的网格。在我的笔记本电脑上,计算 30 六边形网格时,我看到了 3 倍的加速 - 2900 六边形网格的速​​度提高了 10 倍。

>>> from timeit import Timer
>>> t_old = Timer('calc_polygons_orig(1, 1, 100, 100, 10)', 'from hexagons import calc_polygons_orig')
>>> t_new = Timer('calc_polygons_new(1, 1, 100, 100, 10)', 'from hexagons import calc_polygons_new')
>>> t_old.timeit(20000)
9.23395299911499
>>> t_new.timeit(20000)
3.12791109085083
>>> t_old_large = Timer('calc_polygons_orig(1, 1, 1000, 1000, 10)', 'from hexagons import calc_polygons_orig')
>>> t_new_large = Timer('calc_polygons_new(1, 1, 1000, 1000, 10)', 'from hexagons import calc_polygons_new')
>>> t_old_large.timeit(200)
9.09613299369812
>>> t_new_large.timeit(200)
0.7804560661315918

可能有机会创建一个迭代器而不是列表来节省内存。取决于您的代码如何使用多边形列表。

于 2014-11-01T19:00:37.083 回答
1

这是一个不需要任何循环的解决方案。它创建 50x50 六边形的网格:

coord_x, coord_y = np.meshgrid(50, 50, sparse=False, indexing='xy')

ratio = np.sqrt(3) / 2
coord_y = coord_y * ratio          # Condense the coordinates along Y-axes
coord_x = coord_x.astype(np.float)
coord_x[1::2, :] += 0.5            # Shift every other row of the grid
coord_x = coord_x.reshape(-1, 1)   # Flatten the grid matrices into [2500, 1] arrays
coord_y = coord_y.reshape(-1, 1)

radius = 5                         # Inflate each hexagon to the required radius
coord_x *= radius 
coord_y *= radius

这是我在 python 中的 hexalattice 包中的一个代码片段,它可以为您做到这一点(+ 网格旋转、大小和绘图的高级选项)

在这里您可以找到其他示例并链接到 repo:LINK

于 2020-10-11T09:32:08.463 回答