我有各种大小的矩形,我想把它们从中心开始放入一个较大的矩形中。下面是我创建的动画,以直观地描述需要发生的情况。
我一直在努力找到一种建模这种行为的方法。是否存在类似的东西?我只需要指点方向。
以下是非常粗略的解释:
初始化
- 从n个矩形开始 - 按密度排序(它们实际上是3D立方体的俯视图) - 将第一个矩形放在中心
其余矩形(尽可能多地适配)
- 尝试将最高密度分组在中心并向外移动
我一直在努力找到一种建模这种行为的方法。是否存在类似的东西?我只需要指点方向。
以下是非常粗略的解释:
初始化
- 从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
}