How can I calculate the ratio required to line up these rectangles?

38 Views Asked by At

**Diagram here**

I have two rectangles of different sizes which need to slide horizontally, left and right.

The left side of each rectangle needs to line up above P1 when the rectangles are fully right (diagram A).

Now I know that if the larger rectangle is twice as long as the smaller one (as an example), I need to move it twice as far per increment of movement in order to line up the right edges over P1 (diagram B).

If the smaller rectangle moves n units per increment, the larger box moves:

n * Bigbox / SmallBox

But, how to I calculate how many units to move the larger box in order for the left edges to line up at P1 when the rectangles are fully right, and the right edges to line up at P2 when they are all the way to the left?

1

There are 1 best solutions below

1
On BEST ANSWER

Let the left edge, width, and right edge of the first rectangle be $\ell_1$, $w_1$, and $r_1$. We have the relation $\ell_1 + w_1 = r_1$. Similarly, set $\ell_2$, $w_2$, and $r_2$ for the second rectangle, with the relation $\ell_2 + w_2 = r_2$.

You don't give any scale for "fully left" or "fully right". Your language makes it sound as if there is a third object somewhere that is being used to slide both rectangles. Let's suppose that third object, called $s$ for "setting", goes from $0$ (fully left) to $1$ (fully right).

When $s = 1$, we want $\ell_1 = \ell_2 = p_1$. When $s = 0$, we want $r_1 = r_2 = p_2$, or, what is the same, $\ell_1 + w_1 = \ell_2 + w_2 = p_2$. You don't exactly say as much, but your discussion of relative rates of motion of the two rectangles suggests you want to move the two rectangles linearly as a function of $s$. So let's look at the first rectangle and express the location of its left edge as a function of $s$, $\ell_1(s)$: $\ell_1(0) = p_2 - w_1$ and $\ell_1(1) = p_1$. Using the two point form of a line, we have \begin{align*} \ell_1(s) &= \dfrac{p_1 - (p_2 - w_1)}{1 - 0}(s-1) + p_1 \\ &= (p_1 - p_2 + w_1)(s-1) + p_1 \text{.} \end{align*} Similarly, $\ell_2(0) = p_2 - w_2$ and $\ell_2(1) = p_1$, so $$ \ell_2(s) = (p_1 - p_2 + w_2)(s-1) + p_1 \text{.} $$

It is possible that you intend to set one of the two rectangles directly (i.e., without using $s$) and would like to know where to set the other. This can be done by solving for $s$ as a function of $\ell_1$ and of $\ell_2$, then using that value of $s$ in the other rectangle. We have \begin{align*} s(\ell_1) &= \dfrac{\ell_1 + w_1 - p_2}{p_1 + w_1 - p_2} \text{,} \\ s(\ell_2) &= \dfrac{\ell_2 + w_2 - p_2}{p_1 + w_2 - p_2} \text{,} \\ \ell_1(s(\ell_2)) = \ell_1(\ell_2) &= \dfrac{\ell_2(p_1 - p_2 + w_1) + p_1(w_2 - w_1)}{p_1 - p_2 + w_2} \text{, and} \\ \ell_2(s(\ell_1)) = \ell_2(\ell_1) &= \dfrac{\ell_1(p_1 - p_2 + w_2) + p_1(w_1 - w_2)}{p_1 -p_2 + w_1} \text{.} \end{align*}

Having done this, we can directly answer your question: when you move the first rectangle by $1$ unit, the second rectangle moves by $\dfrac{p_1 - p_2 + w_2}{p_1 -p_2 + w_1}$ units (since this is exactly how much $\ell_2(\ell_1)$ changes when $\ell_1$ is incremented by $1$). Similarly, when we move the second rectangle by $1$ unit, the first rectangle moves by $\dfrac{p_1 -p_2 + w_1}{p_1 - p_2 + w_2}$ units.

(I note that the subexpression $p_1 - p_2$ appears frequently. If I were coding this, I would store that number rather than recompute it many times.)