# How to rotate a rectangle OnMousemove

I have an object describing a rectangle using 4 CPoints for its corners, when I'm over a corner I want to hold down the mouse and rotate the object around it's center by moving the mouse.

Dan
###### 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:
Hi,

you can use the formula for rotation

x' = (x * cos A) - (y * sin A)
y' = (x * sin A) + (y * cos A)

1). http://www.codeproject.com/gdi/setworldtransform().asp - Using SetWorldTransform() to rotate basic shapes by any angle
2). http://www.codeproject.com/gdi/rotated_dc.asp - Rotate your graphics

Good Luck
0
Author Commented:
hmm... right
That's if I know the angle, but what if I have only (x0, y0) - center of rotation, and the old and new x,y ???
How do I get the angle between the two lines ?

0
Commented:
You can use the slope equation

Slope = (y'- y)/(x'- x)
0
Commented:
Try this code snippet

dy = y2 - y1;
dx = x2 - x1;

assert (!(dx == 0.0 && dy == 0.0));

if (dx == 0.0)  /* Special case, vertical line */
{
if (dy > 0.0)
return 0.0;
else
return 180.0;
}
if (dy == 0.0) /* Special case, horizontal line */
{
if (dx > 0.0)
return 90.0;
else
return 270.0;
}
if (dx > 0.0)
return 90.0 - atan(dy/dx) * (180 / M_PI);
else if (dx < 0.0)
return 270.0 - atan(dy/dx) * (180 / M_PI);

Good Luck
0
Author Commented:
Yo man,

That's all fine but my math is a bit rusty, know, the slope, know the formula for rotation.
But... how do I get the angle (rads / degrees) ???
0
Commented:
that will return in degree
0

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.

Commented:
Try to call the function as following in your code:

CPoint pt[HANDLE_MAX];
CRect rect(10,10,100,100);

Rotate(rect,90,pt);
...

#include <math.h>
#define HANDLE_MAX 4
#define HANDLE_TL 0
#define HANDLE_TR 1
#define HANDLE_BL 2
#define HANDLE_BR 3

BOOL Rotate(const RECT rect,const int nAngle,LPPOINT pPoint) const
{
int i;
CPoint pt,ptCenter;
const double PI = 3.141592654;

ptCenter.x = int((rect.left + rect.right) / 2);
ptCenter.y = int((rect.top + rect.bottom) / 2);
dRadian = PI * double(nAngle) / double(180);

pPoint[HANDLE_TL] = CPoint(rect.left,rect.top);
pPoint[HANDLE_TR] = CPoint(rect.right,rect.top);
pPoint[HANDLE_BR] = CPoint(rect.right,rect.bottom);
pPoint[HANDLE_BL] = CPoint(rect.left,rect.bottom);

for(i=0;i<HANDLE_MAX;i++)
{
pt.x = pPoint[i].x - ptCenter.x;
pt.y = pPoint[i].y - ptCenter.y;