我有一堆大小可变的矩形,我需要将它们大致组合成一个圆圈,大概最大的矩形在中心。
注意。圆圈的大小不是固定的——这只是我所追求的整体形状。
这更像是我想象的懒惰的人类包装(一旦一块到位,它就会留下来。)
它们已经按照宽度和高度的最大值排序,最大的在前。
理想情况下 - 我认为这可以通过订购来保证 - 根本不会有差距。
我正在努力的算法是:
for each rectangle:
if first:
place rectangle at origin
add all edges to edge list
else:
for each edge in edge list:
if edge is long enough to accomodate rectangle (length <= width or height depending on orientation):
if rectangle placed on this edge does not collide with any other edges:
calculate edge score (distance of mid-point from origin)
use edge with lowest edge score
place rectangle on edge
for each of this rectangles edges:
if edge overlaps one already in the edge list:
merge or remove edge
else:
add to edge list
remove used edge from edge list
add unused sections of this edge into edge list
这对于前几个矩形来说没问题,但是边缘合并非常麻烦,而且我目前选择使用边缘的哪一部分(一端或另一端)的方法往往会留下很多间隙。
尽管我认为我最终会让这种方法相当令人满意地工作,但感觉就像我缺少一种更优雅的(图形?)算法。