# Find if one rectangle can fit inside the other

Given two rectangles $R_1(l_1, b_1)$ and $R_2(l_2, b_2)$ where $l$ and $b$ are their length and breadth respectively, how to check if $R_1$ can fit inside $R_2$ or vice versa.

If $R_1$ and $R_2$ lie in the same plane and there exists an orientation of $R_1$ such that it lies completely inside $R_2$, then $R_1$ fits inside $R_2$.

• Nice question. The rectangle $R_1$ rotated by $\theta$ radians fits in an axis-aligned box of length $l_1\lvert\cos\theta\rvert+b_1\lvert\sin\theta\rvert$ and width $l_1\lvert\sin\theta\rvert+b_1\lvert\cos\theta\rvert$. So you want to determine whether there is any $\theta$ such that these are less than $l_2$ and $b_2$ respectively. Of course, one can restrict $\theta$ to $[0,\pi/2]$ and drop the absolute value signs. This leaves you with inequality constraints on a couple of trigonometric functions, and I don't feel like working it out after that...
– user856
Sep 3, 2012 at 12:05
• This reminds me of a story where a train allowed only items of a max length/breadth/depth, so to get his over-long umbrella onto the train the passenger put it into a box.
– binn
Sep 3, 2012 at 12:55

• This theorem is wrong. Check for rectangles (a x b) = (8 x 8) and (p x q) = (10 x 10). Both conditions (p > a and b >= fraction) are satisfy (it is necessary and sufficient condition) but square (10x10) doesn't fit into square (8x8). Mar 10, 2017 at 14:42

For all who can't read the complete article, here's the most elegant solution (necessary and sufficient condition for a rectangle pxq to fit in a rectangle axb, provided p≥q, a≥b and p>a): =((a+b)/(p+q))^2+((a-b)/(p-q))^2

Two obvious day-to-day applications: rug on a floor, tray in an oven.

• p≥q? What happened with formula if p=q? Mar 10, 2017 at 14:47
• @PiotrWasilewicz I agree the condition is incorrect: if you cannot fit a square rug in a room, you won't fit it by rotation. But I love the answer. Mar 13, 2017 at 20:15
• @WeatherVane But answer is totally incorect! Check for a = 8, b = 1, p = 10 and q = 9. This theorem sais that rectangle 10 x 9 fits in rectangle 8 x 1. Mar 14, 2017 at 8:38
• @PiotrWasilewicz yes I see that now, was just starting to implement it - it seems to work for some values. Perhaps there must be a condition too that a*b > p*q. Mar 14, 2017 at 8:57
• @WeatherVane "work for some values" = doesn't work. I think a*b > p*q is not only condition that you need (but I'm not sure yet). Futhermore if you really only need a*b > p*q (and we have also p>a and also a ≥ b) we have theorem which is working not for "some values" it will be not enough for most examples. Mar 14, 2017 at 9:54