# Correct the position of a rectangle after scaling it.

I have 2 rectangles that can scale in size. They both scale from there own center point. The blue rect can be positioned any where on the canvas.

After upscaling I get something like this.

However I want the blue rectangle to respect its original position relative to the red rectangle. So my question is what formula can I use to correct the blue rectangle position. The Image below reflects the corrected position.

The following values are given. The position(X), width(A) and height(B) of the red rectangle. The position(Y), width(C) and height(D) of the red rectangle.

-
From the drawings it looks like the distance between the centers stays the same after the transformation is applied. Is this the case? –  Abramo Dec 10 '13 at 11:05
The rectangles scale from there centers so yes the distance between the centers stay the same in the first transformation. However after the scaling I want to do a new transformation thats sets to the position of the blue rect to relatively the same position as it was. –  Baijs Dec 10 '13 at 11:54
It's not clear what "respect the original position" means for you. You want to keep fixed the distance between the boundaries? –  Emanuele Paolini Dec 10 '13 at 12:15

Let $x$ and $y$ denote the centers of $X$ and $Y$, respectively. The vector connecting them can be computed as $$v = y-x.$$ If $s$ is the scaling factor, to achieve what you need you just need to modify the center of $Y$, namely replacing $y$ with the new center $$a + s\cdot v = (a_1, a_2) + (s\cdot v_1,\; s\cdot v_2).$$ See the pictures below for the geometric intuition.