17

I have a collection of different sized squares and rectangles that I want to fit together using PHP into one large square/rectangle. The squares are usually images that I want to make into a montage - but sometimes they are simply math objects.

Are there any PHP algorithms for this and what is this type of thing called?

Update: After more searching I think what I want is called the bin packing problem. However, I would also like to add a certain amount of randomization for certain types of packing problems (like images) to allow human interest.

11
  • Are these objects fixed in size... i.e. You want to find the range of images that will fit into a defined "box". Or are you wanting to scale these images (retaining aspect) into a pre-defined box? Commented Jul 13, 2011 at 16:06
  • 1
    I think this question is very close to what I need: stackoverflow.com/questions/4904049/php-array-performance Commented Jul 13, 2011 at 16:25
  • 4
    There are algorithms, and there are implementations of these algorithms for specific languages, like PHP. I suggest making this question language-agnostic because it's QI (quite interesting). Commented Jul 13, 2011 at 20:25
  • 3
    There's always math.stackexchange.com to get some mathemagicians into it. Apparently they have ben talking about this for a while as well: math.stackexchange.com search for the bin packing problem Commented Jul 15, 2011 at 10:45
  • 1
    Look to the garment industry where fitting oddly sized pattern pieces optimally on yardage of cloth is a priority, plus the lumber industry who fit cuts into logs. Commented Jul 16, 2011 at 2:45

3 Answers 3

9
+100

2D Bin packing is NP-hard problem. There are however approximation algorithms.

Look at this code (and explanation). It contains multiple algorithms and there is a GUI:

Solving the 2D Packing Problem

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the -1 but the linked site requires you register and login to view it, which isnt normally a problem but they require address.. company, company size.. ect. Shouldnt have to provide that for an answer.
0

I have a wrote a 1d bin-packing algorithm in php. You want to look for best-fit, first-fit, and so on. But it's not for a 2d problem, maybe you want to look for the knapsack-problem?

6 Comments

I found a PHP-version of the knapsack-problem, but I'm not sure how it relates to a 2D area to fill yet.
@Xeon: At least you have an algorithm with 2 variables, weight and cost. Maybe you can modify it? Another app is drools-planer.
The problem is that formula uses only one constraint and one priority. So it's just a glorified 1D algorithm. I need something that handles 2 constraints (with an optional priority).
@Xeon: I see, did you tried a Floyd-Warshall algorithm or Edmond's maximum matching algorithm?
@Xeon: Maybe a spatial-index or a quadtree? A si reduces the 2d complexity to a 1d complexity.
|
0

I think you can use Semulated Annealing algorithm. I have used it to fill rectangular newspaper pages by rectangular advertisements. As you said you can start it by randomized solution and then you can slowly reach to a good solution. See here http://codetuner.blogspot.com/2010/03/simulated-annealing-approach-to.html. I have used it to solve pagination problem. I think you can use it for you r requirement too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.