I have a single large rectangle of dimensions L*W
, and n smaller rectangles
that each have the same dimension l * w
. Every small rectangle has the same dimensions
.
My goal is to fit all n of smaller
rectangles into the large rectangle while making the most efficient use of space in the large rectangle possible. l and w can be scaled up or down as needed, as long as the proportion is kept the same.
How can it be determined how the smaller rectangles should be scaled to fit them all in the large rectangle?
Here is an algorithm that finds the max value of a scaling factor F
such that all small a x b
rectangles, when scaling by F
will fit in the containing rectangle A x B
:
(p, q)
of positive integers such thatp <= n
q <= n
n = p * q - r
for some integer r >= 0
satisfying r < p
or p < q
compute f = min(A/(a*p), B/(b*q)).
2. Let F
be the maximum of all factors f
computed in 1.
The computation of all pairs (p, q)
may proceed as follows:
p := 0
p := p + 1
p > n
, stopq := (n + p - 1) / p
(integer division). Next pair (p, q)
. Idea of the algorithm
Every pair (p, q)
represents a particular layout of the scaled rectangles with p
rectangles in an horizontal row and q
rows, the last one possibly incomplete. Here is an example for n = 13
written as 3 * 5 - 2
:
Since p
scaled rectangles of width f*a
must fit in a rectangle of width A
, we have: p*f*a <= A
or f <= A/(p*a)
. Similarly f <= B/(q*b).
Therefore the maximum scale for this configuration is min(A/(p*a), B/(q*b)).