微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将匀称的多边形切成N个相等大小的多边形

如何解决将匀称的多边形切成N个相等大小的多边形

我有一个Shapely多边形。我想将这些多边形切成 n 个多边形,每个多边形的大小区域大致相同。大小相等是最好的,但是也可以近似。

我尝试使用两种方法described here,这两种方法都是朝着正确方向迈出的一步,而不是我不需要的。两者都不允许目标 n

我调查了 voronoi ,我对此很陌生。此分析给出的最终形状将是理想的,但它需要点而不是形状作为输入。

解决方法

这是我能做到的最好的。它不会使每个多边形的表面积相等,但是事实证明它可以满足我的需要。这将使用特定数量的点填充形状(如果参数保持恒定,则点数也将保持不变)。然后将这些点转换为voronoi,然后将其转换为三角形。

# Voronoi doesn't work properly with points below (0,0) so set lowest point to (0,0)
shape = affinity.translate(shape,-shape_a.bounds[0],-shape_a.bounds[1])

points = shape_to_points(shape)

vor = points_to_voronoi(points)

triangles = MultiPolygon(triangulate(MultiLineString(vor)))



def shape_to_points(shape,num = 10,smaller_versions = 10):
    points = []

    # Take the shape,shrink it by a factor (first iteration factor=1),and then 
    # take points around the contours
    for shrink_factor in range(0,smaller_versions,1):
        # calculate the shrinking factor
        shrink_factor = smaller_versions - shrink_factor
        shrink_factor = shrink_factor / float(smaller_versions)
        # actually shrink - first iteration it remains at 1:1
        smaller_shape = affinity.scale(shape,shrink_factor,shrink_factor)
        # Interpolate numbers around the boundary of the shape
        for i in range(0,int(num*shrink_factor),1):
            i = i / int(num*shrink_factor)
            x,y =  smaller_shape.interpolate(i,normalized=True).xy
            points.append( (x[0],y[0]))
    
    # add the origin
    x,y = smaller_shape.centroid.xy
    points.append( (x[0],y[0]) ) # near,but usually not add (0,0)
    
    points = np.array(points)
    return points


def points_to_voronoi(points):
    vor = Voronoi(points)
    vertices = [ x for x in vor.ridge_vertices if -1 not in x]
    # For some reason,some vertices were seen as super,super long. Probably also infinite lines,so take them out
    lines = [ LineString(vor.vertices[x]) for x in vertices if not vor.vertices[x].max() > 50000]
    return MultiLineString(lines)

这是输入形状:

enter image description here

这是在shape_to_points之后:

enter image description here

这是在points_to_voronoi

之后

enter image description here

然后我们可以对voronoi进行三角剖分:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。