# Simple CRectangle question

Hi,
I have two CRectangle objects. When I draw those rectangles, one is inside the other. What I need to do is that after one rectangle is changed (either its dimensions or its position) the other one must change proportionally.
For instance, in every drawing program, if you draw two rectangles, one embedded in the other, and then you select both and group them, when you resize the group, both rectangles are resized in the same manner.

Any help?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Pre-requisite:
We know the ratio in between.

First:
Get the size of the first CRect.
Second:
Get the Center Point of the first CRect.
Third:
Using the ratio in between to get the size of the second CRect correspondingly and cut x,y of the second size in half and substract Center Point by it respectively will give you the top-left point of the second.
Fourth:
Using the CRect( POINT point, SIZE size )to construct the second CRect.

Regards
W.Yinan
Commented:
If you already have the second.You can simply CopyRect to it...
Commented:
Hi W.Yinan,
Thanks for your help but, could you please give me source code?

Consider that I know all the parameters of Rect1 and Rect2 and that Rect1 is resized:

***********************Rect 1
*                     *
*                     *
*   ******* Rect2     *
*   *     *           *
*   *     *           *
*   *******           *
*                     *
*                     *
***********************

Thanks.
Commented:
->Third:
Using the ratio in between to get the size of the second CRect correspondingly and
===============

ratio here means length-to-length(both  Y and X axis)ratio you want ,like 0.7(second/first) or so.You simple multiply it to the size of first...
(sorry for poor english)

Regards
W.Yinan

Author Commented:
sorry, I was using NT account of qocarlos.

Elito
Commented:
->Thanks for your help but, could you please give me source code?
===============
Here is 3:00am , I will try to now.
But not 100% tonight...
Commented:

Commented:
Opps, I misundertand a little.It's not similar rect and dont have the same certerpoint.
Commented:
Hi,

I have put in the following link an example of what I'm trying to do:

http://qomestre.usc.es/test.htm

cheers
Author Commented:
sorry I used again qocarlos account...
Im gonna have problems with him :-)

Elito
Commented:
http://qomestre.usc.es/test.htm
need some qualifications..
Commented:

Sincerely
W.Yinan
Commented:
I'm not sure if this is what you need (because I don't know how to login to the given URL), but here's a function which I think calculates what you need:

// pass the Rect1 before it was moved/resized as orgRect
// pass the Rect2 as refRect
// pass the Rect1 after it was moved/resized as newRect
// returns new Rect2
// (I'm using some float math here because I was too lazy to think about a more performant way without using floats)

CRect CalcRect( CRect orgRect, CRect refRect, CRect newRect )
{
CRect newChildRect;

float xAsp = newRect.Width() / (float)orgRect.Width();
float yAsp = newRect.Height() / (float)orgRect.Height();
CPoint offset( refRect.left - orgRect.left, refRect.top - orgRect.top );

newChildRect.left = newRect.left + (int)( offset.x * xAsp );
newChildRect.top = newRect.top + (int)( offset.y * yAsp );
newChildRect.right = newChildRect.left + (int)( refRect.Width() * xAsp );
newChildRect.bottom = newChildRect.top + (int)( refRect.Height() * yAsp );

return newChildRect;
}

hope this helps,

ZOPPO

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Hi!
Sorry about the delay in getting back to you.
That was exactly what I needed! thanks

elito
Commented:
no problem     :)

have a nice day,

ZOPPO
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.