2

Let's say I have 256 images with an average size of 70x150 (So, size if variable). And I have a Graphic-instance (Created from a BufferedImage with a given size) on which I want to draw the images. But I want to draw them at the lowest possible surface. So, not simply in a grid, but really puzzled in each other. But!: they may not overlap each other.

Maybe this something only a human brain can manage.
It is worth asking it, I think....

Thanks in advance,
Martijn

For example:

+------++------+
|      ||      |
| img1 || img2 |
|      |+------+
|      |+-----------------+
+------+|                 |
+---+   |                 |
| 3 |   |                 |
|   |   |    img 4        |
+---+   |                 |
        |                 |
        +-----------------+
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I don't really understand your question. Are you asking how to find the smallest rectangle in a list of rectangle? Or howw to actually draw your example? Or whats the smallest size you can draw with a Graphic instance`? – RoflcoptrException Dec 13 '10 at 19:14
  • 2
    I don't know the exact terminology to use, but this problem sounds a lot like a 2D version of the hard drive or memory space management problem: given a new file/data you want to store, where do you put it? Best fit, worst fit, first fit, etc. Maybe you could read up on that to get some ideas. – DGH Dec 13 '10 at 19:15
  • @DGH: Indeed, that is a relevant comparison. – Martijn Courteaux Dec 13 '10 at 19:21

2 Answers2

3

This question has been asked before: What algorithm can be used for packing rectangles of different sizes into the smallest rectangle possible in a fairly optimal way?

A good survey, from a previous answer, is available at: http://www.csc.liv.ac.uk/~epa/surveyhtml.html

Community
  • 1
  • 1
2

Basically, you're asking for a solution to a knapsack problem.

There is none optimal algorithm for a Knapsack Problem of arbitrary size, since it's NP-hard problem.

There are plentiful of sub-optimal algorithms:

Rekin
  • 9,731
  • 2
  • 24
  • 38