2

I want to rotate a set of points by 90° clockwise, like this:

enter image description here

But I use the code as I show here, and set the angle as 90°, center as (100,100), and I get a result like this:

enter image description here

I check the formula, is the same as my code:

cv::Point2f rotate2d(const cv::Point2f& inPoint, const double& angRad)
{
    cv::Point2f outPoint;
    //CW rotation
    outPoint.x = std::cos(angRad)*inPoint.x - std::sin(angRad)*inPoint.y;
    outPoint.y = std::sin(angRad)*inPoint.x + std::cos(angRad)*inPoint.y;
    return outPoint;
}

cv::Point2f rotatePoint(const cv::Point2f& inPoint,
                        const cv::Point2f& center,
                        const double& angRad)
{
    return rotate2d(inPoint - center, angRad) + center;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
kookoo121
  • 1,116
  • 2
  • 12
  • 21

1 Answers1

2

The problem is probably hiding in the retuning value of your second function:

cv::Point2f rotatePoint(const cv::Point2f& inPoint, const cv::Point2f& center, const double& angRad){
return rotate2d(inPoint - center, angRad) + center;
               ------------^      --------------^
}

You firstly apply it and then translate the center back an forth. If you want to rotate around a pivot point: you subtract it (perform rotation around the origin) and then add it after the transformation is applied. You can do this directly in your first function, by modifying the transformations to:

int x = cos(angRad) * (inPoint.x - axisOfRotation.x)
      - sin(angRad) * (inPoint.y - axisOfRotation.y) + axisOfRotation.x;
                                                       -------^
int y = sin(angRad) * (inPoint.x - axisOfRotation.x) 
      + cos(angRad) * (inPoint.y - axisOfRotation.y) + axisOfRotation.y;
                        // added to both coordinates   -------^

Where: inPoint is the point to be rotated, axisOfRotation the pivot point and angRad = angInDegrees * PI / 180. the angle of clockwise rotation.

Check this for further insight.

Community
  • 1
  • 1
Ziezi
  • 6,375
  • 3
  • 39
  • 49