Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partition a rectangle into near-squares of given areas

I have a set of N positive numbers, and a rectangle of dimensions X and Y that I need to partition into N smaller rectangles such that:

  • the surface area of each smaller rectangle is proportional to its corresponding number in the given set
  • all space of big rectangle is occupied and there is no leftover space between smaller rectangles
  • each small rectangle should be shaped as close to square as feasible
  • the execution time should be reasonably small

I need directions on this. Do you know of such an algorithm described on the web? Do you have any ideas (pseudo-code is fine)?

Thanks.

like image 807
Marko Dumic Avatar asked Mar 28 '10 14:03

Marko Dumic


People also ask

Can you divide a rectangle into squares?

Yes. Given your first constraint - that the sides be of integral length. Simply divide an n x m inch rectangle into 1 inch squares and you'll have n*m squares.

How do you partition a rectangle into 4 equal parts?

We could divide our rectangle into two equal parts, or halves. Then, we can halve each half again until we have four equal parts. If we take two-halves and halve them again, we get quarters. So, this is the rectangle that is divided into quarters.


1 Answers

What you describe sounds like a treemap:

Treemaps display hierarchical (tree-structured) data as a set of nested rectangles. Each branch of the tree is given a rectangle, which is then tiled with smaller rectangles representing sub-branches. A leaf node's rectangle has an area proportional to a specified dimension on the data.

That Wikipedia page links to a page by Ben Shneiderman, which gives a nice overview and links to Java implementations:

Then while puzzling about this in the faculty lounge, I had the Aha! experience of splitting the screen into rectangles in alternating horizontal and vertical directions as you traverse down the levels. This recursive algorithm seemed attractive, but it took me a few days to convince myself that it would always work and to write a six line algorithm.

Wikipedia also to "Squarified Treemaps" by Mark Bruls, Kees Huizing and Jarke J. van Wijk (PDF) that presents one possible algorithm:

How can we tesselate a rectangle recursively into rectangles, such that their aspect-ratios (e.g. max(height/width, width/height)) approach 1 as close as possible? The number of all possible tesselations is very large. This problem falls in the category of NP-hard problems. However, for our application we do not need the optimal solution, a good solution that can be computed in short time is required.

You do not mention any recursion in the question, so your situation might be just one level of the treemap; but since the algorithms work on one level at a time, this should be no problem.

like image 151
Thomas Avatar answered Sep 22 '22 07:09

Thomas