问题
I am looking for an algorithm that can help distribute different sized rectangles within a larger rectangle while minimizing overlapping.
I have looked at bin packing algorithms, but they seem to minimize the amount of space between rectangles (in my case the all of the items being packed will be squares).
I guess I want to maximize the distance between all squares and the border of the outer rectangle.
Here is an example of what I am trying to do:
回答1:
What if you packed them as tightly as possible, using an algorithm like the one described here, then expanded evenly to match the target enclosing rectangle?
For example, say you can pack the 3 rectangles above into a 3x2
box, and your outer box is 7x5
. Then take the vector from the center of the box to the center to each rectangle, and multiply the x component by (7/3)
, and the y component by (5/2
), and that gives the new center.
回答2:
This seems to be a generalization of the Knapsack Problem.
Dynamic programming will solve it in close to polynomial time.
来源:https://stackoverflow.com/questions/8481460/algorithm-needed-for-distributing-rectangles-evenly-within-another-rectangle