I have rectangles and a GridLayout, the width and the height of these rectangles are the same. so the layout put the rectangle's position like the next picture.
This is the problem: If the width and the height of my rectangles are of different size, what is the algorithm for the layout to put the rectangle's position in a compact way?
Finally got some time for this question so here is my bin pack code:
usage:
OK now some binpack algorithm explaining:
prepare data
ix[]
array)ix[]
array)ix[]
... tolin[].i0,lin[].i1
lin[].xs
use whole lines (Red part of image)
Search all
lin[]
with bigger width than wrapped line size. If found then fill the line with it and mark used rectangles as used (also remove used width) and move to next linetry to fill line division (not whole page) (Green part of image)
compute minimal nonzero height of line division and use it as height fill limit and stack divisions next to each other so they fill the whole line. I use division
line/2 + line/4 + line/8 + line/16 ... = ~ line
wrap still unused rectangles to page size (Blue part of image)
[Notes]
This approach is far from optimal and code is not optimized but still efficient enough. For 450 boxes with linear randomness in size
(0.5 .. 10.0)
the average runtime is~2.8ms
which is fine I think. You can improve this by divide and conquer instead of my line division. Fill whole lines and then use the rest to fill area divisions recursively.Hope it helps ... if there is anything unclear comment me ...