我有各种大小的矩形,我试图将它们从中心开始放入一个更大的矩形中。您将在下面找到我创建的动画,以直观地描述需要发生的事情。
我一直在努力想出一种方法来模拟这种行为。是否存在与此类似的东西?我只需要指出正确的方向。
下面是一个非常粗略的解释:
初始化
n
从矩形开始- 按密度排序(它们实际上是 3d 立方体鸟瞰图)
- 将第一个矩形放在中心
剩余的矩形(尽可能多地适合) 尝试将最高密度分组在中心并向外移动
Dimensions = { width: 400, height: 300 }
Boundries = {
WEST = 0,
EAST = Dimensions.width,
NORTH = 0,
SOUTH = Dimensions.height
}
// each rectangle has width, height, and other information
rectArr = Array of {width:60, height:40}
root = { x:EAST/2, y:SOUTH/2 }
foreach rect in rectArr {
// I will always traverse from the root and try to go left and right. If I cannot, I move up and try the same thing. I then move down. The problem is if there are 5 or more rows. I will be starting from the root and going up, then down, then up-up, then down. It's like I have two parallel trees.
// Try to branch left or right
if rect.width <= (Boundries.EAST - ('rightmost rectangle'.x + 'rightmost rectangle'.width/2)) branch-right
if rect.width <= (('leftmost rectangle'.x + 'leftmost rectangle'.width/2) - Boundries.WEST) branch-left
// Try to branch up or down
if rect.height <= ((root.x + root.height/2) - Boundries.NORTH) branch-up
if rect.height <= (Boundries.SOUTH - (root.x + root.height/2)) branch-down
}