没有一种方法来填充在每一列的底部的间隙,因为砌筑排序在垂直顺序的砖,然后在水平方向顺序。它类似于装箱算法,其中一些附加数学与树图算法类似。 bin-packing alogrithm的想法是尽可能减少固定数量砖块堆积在柱子中所需的柱子数量。这是一个完整的问题,当然你在底部(或顶部)有间隙,这些间隙不能填充。
对于树形图,您可以使用kd树。一个很好的描述在这里:http://www.blackpawn.com/texts/lightmaps/default.html。
{
Node* child[2]
Rectangle rc
int imageID
}
Node* Node::Insert(const Image& img)
if we're not a leaf then
(try inserting into first child)
newNode = child[0]->Insert(img)
if newNode != NULL return newNode
(no room, insert into second)
return child[1]->Insert(img)
else
(if there's already a lightmap here, return)
if imageID != NULL return NULL
(if we're too small, return)
if img doesn't fit in pnode->rect
return NULL
(if we're just right, accept)
if img fits perfectly in pnode->rect
return pnode
(otherwise, gotta split this node and create some kids)
pnode->child[0] = new Node
pnode->child[1] = new Node
(decide which way to split)
dw = rc.width - img.width
dh = rc.height - img.height
if dw > dh then
child[0]->rect = Rectangle(rc.left, rc.top,
rc.left+width-1, rc.bottom)
child[1]->rect = Rectangle(rc.left+width, rc.top,
rc.right, rc.bottom)
else
child[0]->rect = Rectangle(rc.left, rc.top,
rc.right, rc.top+height-1)
child[1]->rect = Rectangle(rc.left, rc.top+height,
rc.right, rc.bottom)
(insert into first child we created)
return Insert(img, pnode->child[0])
您的htmlx和html5填充问题的问题很容易解释。你有一个填充:8px;在html5文档的body标签中。所以图像与每边4px的周围图像之间存在差距。看到我的图片:
你能解释为什么它自然会在底部有空隙吗?砌体中的每块砖可以像墙壁一样粘在一起,并且墙壁没有间隙? – Bytemain 2012-07-20 00:34:43
是的,砖块粘在一起,但看起来图像高度的总和并不总是与容器的高度完全一样,所以它的余下部分变成了每一列底部的这些间隙。 – 2012-07-20 01:53:50