Search code examples
algorithmgame-physicsbin-packing

How to distribute a list of rectangles of varying width and height, maximizing distance between items, inside a rectangular container?


I need a function that takes in two inputs:

  • a bounding box with a width and height
  • an array of rectangular items with varying width and height (all smaller than the bounding box)

It would output an array of the same items with an additional position property (can mutate the original items), with x and y such that distances between items are maximized (note - not distance between the points, need to take into consideration of the dimensions of the item).

It doesn't have to be the mathematically proven best solution, a good-enough heuristic is okay

I've looked at bin-packing, but it seems to do the opposite of what I want.

Any language is acceptable, even pseudocode.

I'm not sure where even to start. Right now I'm just randomizing positions and occasionally end up with overlapping items, which is undesirable.


Solution

  • As you only need a heuristic, and not an optimal solution (which nobody can give you ATM, as "distances between items are maximized" is a vague term):

    Though bin-packing seems to be the "opposite", it can be used. Take a smaller (smallest) box that they fit into, and do the packing, then stretch it along with the calculated positions.