0
$\begingroup$

illustration

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?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

enter image description here

I've re-drawn the picture in order to demonstrate the approach better.

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?

$\endgroup$
1
  • $\begingroup$ This works great, thanks for your input :) $\endgroup$
    – fitzmode
    Commented Mar 13, 2023 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .