16

Do either of the below approaches use the correct mathematics for rotating a point? If so, which one is correct?

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

  // translate point back to origin:
  p.x -= cx;
  p.y -= cy;

  // Which One Is Correct:
  // This?
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;
  // Or This?
  float xnew = p.x * c + p.y * s;
  float ynew = -p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
}
Joshua
  • 6,643
  • 15
  • 55
  • 76
  • 11
    I don't quite understand. What are cx and cy? Also, you have declared your function of type POINT, but it doesn't return a POINT, or indeed anything. – Brian Hooper Jul 02 '10 at 01:09
  • 1
    @Brian Hooper: +1 for pointing out the benefits of meaningful variable names ;) – Cogwheel Jul 02 '10 at 04:43

3 Answers3

33

From Wikipedia

To carry out a rotation using matrices the point (x, y) to be rotated is written as a vector, then multiplied by a matrix calculated from the angle, θ, like so:

https://upload.wikimedia.org/math/0/e/d/0ed0d28652a45d730d096a56e2d0d0a3.png

where (x′, y′) are the co-ordinates of the point after rotation, and the formulae for x′ and y′ can be seen to be

alt text

Community
  • 1
  • 1
Alexandros Gezerlis
  • 2,221
  • 16
  • 21
  • 3
    Don't forget, that if you're working in a typical screen coordinate space that your y axis will be inverted from the mathematical standard (down is +y, up is -y) and you'll need to account for that. – Daniel Bingham Nov 08 '17 at 20:13
26

It depends on how you define angle. If it is measured counterclockwise (which is the mathematical convention) then the correct rotation is your first one:

// This?
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

But if it is measured clockwise, then the second is correct:

// Or This?
float xnew = p.x * c + p.y * s;
float ynew = -p.x * s + p.y * c;
Beta
  • 96,650
  • 16
  • 149
  • 150
1

This is extracted from my own vector library..

//----------------------------------------------------------------------------------
// Returns clockwise-rotated vector, using given angle and centered at vector
//----------------------------------------------------------------------------------
CVector2D   CVector2D::RotateVector(float fThetaRadian, const CVector2D& vector) const
{
    // Basically still similar operation with rotation on origin
    // except we treat given rotation center (vector) as our origin now
    float fNewX = this->X - vector.X;
    float fNewY = this->Y - vector.Y;

    CVector2D vectorRes(    cosf(fThetaRadian)* fNewX - sinf(fThetaRadian)* fNewY,
                            sinf(fThetaRadian)* fNewX + cosf(fThetaRadian)* fNewY);
    vectorRes += vector;
    return vectorRes;
}
YeenFei
  • 3,180
  • 18
  • 26