Solved

How to rotate a rectangle OnMousemove

Posted on 2003-11-05
8
1,415 Views
Last Modified: 2013-11-20
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.

Any help please....

Dan
0
Comment
Question by:dan_hed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9692218
Hi,

you can use the formula for rotation

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

Try these links also

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 Comment

by:dan_hed
ID: 9692308
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9692330
You can use the slope equation

Slope = (y'- y)/(x'- x)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9692343
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 Comment

by:dan_hed
ID: 9692354
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
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 250 total points
ID: 9692359
What about the above code?
that will return in degree
0
 
LVL 3

Expert Comment

by:freewell
ID: 9693133
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;
      double dRadian,dRadianCos,dRadianSin;
      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);
      dRadianCos = cos(dRadian);
      dRadianSin = sin(dRadian);

      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;

            pPoint[i].x = int(double(ptCenter.x) + double(pt.x)*dRadianCos - double(pt.y)*dRadianSin);
            pPoint[i].y = int(double(ptCenter.y) + double(pt.x)*dRadianSin + double(pt.y)*dRadianCos);
      }

      return TRUE;
}
0
 

Author Comment

by:dan_hed
ID: 9693258
Hi roshmon,
Not quit it but managed to solve the problem, reminded me of the math I forgot long ago :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question