给定以下多边形,并根据下面所示方式将其分割成子多边形[left],我想创建n
个连续、大小相等的子多边形组[右侧,其中n=6
]。子多边形没有规律的模式,但保证是相邻的且没有洞。
这不是将一个多边形分割为相等的形状,而是将其子多边形分组为相等的、连续的组。初试多边形可能没有能被n
整除的子多边形数,在这种情况下,非等大小的组也可以。我所拥有的唯一数据是n
,要创建的组数,以及子多边形和其外壳的坐标(通过剪裁库生成)。
我的当前算法如下:
list sub_polygons[] # list of polygon objects
for i in range(n - 1):
# start a new grouping
pick random sub_polygon from list as a starting point
remove this sub_polygon from the list
add this sub_polygon to the current group
while (number of shapes in group < number needed to be added):
add a sub_polygon that the group borders to the group
remove this sub-polygon from the sub-polygons list
add all remaining sub-shapes to the final group
然而,这会导致连续性问题。下图说明了问题-如果将红色多边形添加到蓝色组中,它会切断绿色多边形,使其无法添加到其他任何东西以创建连续的组。
当向组中添加子多边形时,很容易添加一个检查来解决此问题,例如:
if removing sub-polygon from list will create non-contiguous union
pass;
但是在某些边缘情况下,每个可以添加的形状都会创建可用子多边形的不连续联合体。在下面的示例中,我的当前算法正在尝试将一个子多边形添加到红色组中,并检查其连通性,但无法添加任何一个:
是否有更好的算法来对子多边形进行分组?