# Formula to find which edge collides first when scaling stacked rectangles.

I'm working on an interactive tool to contain the green box in the blue box when the green box is scaled. Scaling can be from any corner, with the opposite corner as the origin.

The green box can be in any position (x, y) within the blue box, and can be any width or height that can be contained in the blue box.

Both boxes can have any aspect ratio.

My current approach is if corner a scaled AND x > y it should follow that edge ad collides with AD first.

This however breaks in some instances : when corner c is scaled while the green box's aspect ratio is greater than the blue box aspect ratio and green box's width is sufficiently large - edge bc can tend to edge BC before CD even if right > bottom

Once I determine which edge collides first, I can calculate the bounding rectangle of the green box and constrain it's scaling. Because of particular constraints, a Points-In-Polygon approach won't work.

What's the best way to handle all the cases for all aspect ratios?

Without loss of generality, let's assume that the origin is at the top-left corner of the inner box and we are therefore dragging the box from the opposite corner from that. The dimensions of the inner box are $$w$$ and $$d$$; the dimensions of the outer box are $$w+\text{left}+\text{right}$$ and $$d+\text{top}+\text{bottom}$$.
In order to figure out which edge collides first with the outer box, we imagine a new box that doesn't have the "top" and "left" parts. This would of course change depending on where the origin is. Now we have the new green box which only has the dimensions $$w+\text{right}$$ and $$d+\text{bottom}$$.
Note that the aspect ratio of the inner box does not change when scaling it. Now we only have to compare the two aspect ratios, $$\frac{w}{d} \qquad \text{and}\qquad \frac{w+\text{right}}{d+\text{bottom}}$$ The bottom edge collides first if $$\frac{w}{d} < \frac{w+\text{right}}{d+\text{bottom}}$$ and the right edge collides first if $$\frac{w}{d} > \frac{w+\text{right}}{d+\text{bottom}}$$ Is this sufficient for you?