# Keeping a dynamic-dimensioned square centered in a rectangle

For some reason this problem which seemed fairly easy on the surface has given me a lot of trouble. I have a square who's side length changes based on an independent variable. The square's upper left corner is at the origin, and the square's lower right corner is at $(l_f,-l_f)$, where $l_f$ is the length of the square.

Our rectangle must always have a width to heigth ratio of $1280/720$. When the square is at it's initial length, the length of the square and the height of the rectangle should be the same. Additionally, the square must always be directly in the middle of the rectangle (that is, the center of the square and the center of the rectangle coincide). And finally, the ratio of the square's length and the rectangles lengths must get linearly smaller as the square's length decreases, and linearly larger as the square's length increases. I need the equations for $(x_i,y_i)$ and $(x_f,y_f)$, where $x_i$ and $y_i$ are the coordinates of the top left corner of the rectangle, and $x_f,y_f$ are the coordinates of the bottom right corner of the rectangle, based on the following variables.

$d =$ an independent variable, in the domain $(0, \infty)$, initial value of $1$

$l_i =$ constant (initial size of square...for my problem it is 250)

$l_f = l_i\cdot d \rightarrow$ (where $l_f$ is the length of the square)

My Attempt

I believe my attempt at getting the y limits is correct. $$y_i = \frac{l_f}{2} - \frac{l_i}{2}$$ $$y_f = \frac{l_f}{2} + \frac{l_i}{2}$$

The $x$ coordinates are the hard ones now. Because the initial height of the rectangle and the length of the square must be the same, we need a corrective term to add onto the $x$ coordinates. The following term worked (not sure if it's neccessary in the end though). $\frac{1}{2} \cdot l_f(\frac{1280}{720} - 1)$. Then, if the independent variable changes from it's default value of 1, we need another term since the width and height of the rectangle can't continue to scale at the same rate, otherwise the 1280x720 ratio is not maintained. I thought this term would do the trick $\frac{1280}{720}(\frac{l_f}{2} - \frac{l_i}{2})$, but using these terms does not give me the correct answer. Using these terms, I get the following:

$$x_i = \left(1 + \frac{1280}{720}\right) \left(\frac{l_f}{2} - \frac{l_i}{2}\right) - \frac{1}{2} \cdot l_f\left(\frac{1280}{720} - 1\right)$$ $$x_f = \left(\frac{l_f}{2} + \frac{l_i}{2}\right) - \frac{1}{2} \cdot l_f\left(\frac{1280}{720} - 1\right) - \frac{1280}{720} \left(\frac{l_f}{2} - \frac{l_i}{2}\right)$$

Like I said this doesn't work though. Let me know if you have any tips, thanks!

-
Should the rectangle shrink to have the height of the square? What are $x_i$, $y_i$, $x_f$ and $y_f$? – Christoph Aug 27 '13 at 11:27
Just made an edit to that =p, to add to what I edited, the rectangle should look "much larger" in comparison to the square as the square's length decreases and vice-versa. The progression should be linear. – Spaderdabomb Aug 27 '13 at 11:28
The edit doesn't answer any of my questions though. – Christoph Aug 27 '13 at 11:29
Sorry, see my above comment. My post should now contain all neccessary info – Spaderdabomb Aug 27 '13 at 11:30
I still don't understand how the rectangle is supposed to scale. Should it have the height of the square? If not, what determines its height? Maybe you could describe what the actual problem is you are working on. – Christoph Aug 27 '13 at 11:49

If I'm understanding everything correctly, here's what I think you're looking for: \begin{align*} y_i &= \dfrac{-l_i}{2} - \dfrac{l_f}{2} \\ y_f &= \dfrac{-l_i}{2} + \dfrac{l_f}{2} \\ x_i &= \dfrac{l_i}{2} - \dfrac{\frac{1280}{720}l_f}{2} \\ x_f &= \dfrac{l_i}{2} + \dfrac{\frac{1280}{720}l_f}{2} \\ \end{align*} Here's what I did. Initially, the centre of the square/rectangle is at $(l_i/2, -l_i/2)$. Note that linearly scaling the square/rectangle by a factor of $d$ will not change this centre point. Now to get the corners, we simply have to either add or subtract half the dimensions of the rectangle. Initially, the rectangle has a height of $l_i$, so scaling it by $d$ gives us a new height of $l_f$. Since we know the aspect ratio, it follows that the new width must be $\frac{1280}{720}l_f$.

-
"Note that linearly scaling the square/rectangle by a factor of d will not change this centre point"....remember the origin is not at the center point, it's at the upper left hand corner of the square, so it indeed changes the location of the center point – Spaderdabomb Aug 27 '13 at 13:31
Also I initially tried what you are doing, but because the square's origin is not at the center, using these formulas actually does not keep the square's center and the rectangle's center aligned. – Spaderdabomb Aug 27 '13 at 13:44

From the answers you gave to my comments, I figured you mixed up the $y$-direction in your question. You say that the rectangle is centered at $(l_f/2, -l_f/2)$, but in your calculation you use $(l_f/2, l_f/2)$ to obtain the $y$-boundaries of your rectangle. (Note that you get $y_f=l_f$ for $d=1$ instead of $y_f=-l_f$ as you told me in a comment.)

Suppose the formulas for the $y$-boundaries are correct in your question and the center of the square is in fact at $(l_f/2, l_f/2)$ so everything fits together, I would guess you want the following $x$-boundaries for the rectangle:

\begin{align*} x_i &= \frac{l_f}2 - \frac{1280}{720} \cdot \frac{l_i}{2} \\ x_f &= \frac{l_f}2 + \frac{1280}{720} \cdot \frac{l_i}{2} \end{align*}

-
This doesn't preserve the ratio of $x$ limits to $y$ limits...=( – Spaderdabomb Aug 27 '13 at 14:58
as the scale increases, the y limits outgrow the x limits pretty quickly, which should never happen – Spaderdabomb Aug 27 '13 at 14:59
I think it does, from your formulas we have $y_f-y_i=l_i$, from my formulas we have $x_f-x_i=\frac{1280}{720} l_i$, so the ratio is correct. – Christoph Aug 27 '13 at 15:51
I have the code running right in front of me. Both the code and just looking at your solutions tells me it doesn't work... – Spaderdabomb Aug 28 '13 at 7:44
Once again, this would work if the origin was at the center of the square, but in fact it's at the upper left hand corner. Remember the size of the square changes as $d$ changes – Spaderdabomb Aug 28 '13 at 7:45