我怎样才能逆时针旋转

How can I rotate counterclockwise

本文关键字:逆时针 旋转      更新时间:2023-10-16

这是一个用于顺时针旋转的函数。参数是我们想要旋转的度数。如何将其更改为逆时针旋转?

void rotateClockwise( int degree ) {    
  int currentDegree = getDegree();
  int desiredDegree = currentDegree + degree;
  if( desiredDegree > 359 ) {
    desiredDegree -= 359;
  }
  do {
    newDegree = getDegree(); // Returns current degree
    desiredDegreeSINE = sin(desiredDegree * (PI/180));
    currentDegreeSINE = sin(newDegree * (PI/180));
    if( desiredDegreeSINE > 0 && currentDegreeSINE < 0 ) {
      newDegree = newDegree - 360;
    }
    if( newDegree >= desiredDegree ) {
      // Stop rotating
      break;
    } else {
      // Keep rotating
    }
  } while(true);
}

我们每转1度。

void rotateCounterClockwise( int degree ) {
   return rotateClockwise(360 - (360 + degree) % 360);
}
int rotateClockwise(int degree) {
    return (getDegree() + degree) % 360;
}

int rotateCounterClockwise(int degree) {
    int desiredDegree = (getDegree() - degree%360);
    return desiredDegree >= 0 ? desiredDegree : 360+desiredDegree;
}

简化它。

你已经有了一个旋转方向——这是你旋转角度的标志。

#include <iostream>
struct thing
{
    int rotate(int alpha)
    {
        angle = (angle + alpha) % 360;
        if (angle < 0)
            angle += 360;
        // by all means pre-calculate sin and cos here if you wish.
        return angle;
    }
    int angle;
};
int main(int argc, char * argv[])
{
    auto t = thing { 10 };
    std::cout << t.rotate( 5 ) << std::endl;
    std::cout << t.rotate( -30 ) << std::endl;
    std::cout << t.rotate( 360 + 30 ) << std::endl;
    std::cout << t.rotate( -360 - 40 ) << std::endl;
    return 0;
}

预期结果:

15
345
15
335