2016-04-05 45 views
0

時計回りの回転機能です。パラメータは回転させたい度数です。 どのように反時計回りに変更できますか?反時計回りの回転方法

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度ずつ回転しています。

+0

まず、あなたはdesiredDegreeを使用する必要があります - = 360と正弦は何ですか? – user4759923

+0

'newDegree

+0

どのようにローテーションを行いますか?確かにあなたは回転のために負の角度と角度> = 360の両方を使うことができます、コンピュータはそれらを恐れていません!人間の読書やデバイスのために本当に '0..359'の角度が必要な場合、'(angle%360) ' – MBo

答えて

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

実際には反時計回りの回転ではありません。 –

+1

@ J.Doe時計回りに独自の定義がありますか? – UmNyobe

0
int rotateClockwise(int degree) { 
    return (getDegree() + degree) % 360; 
} 

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

それを簡素化します。

すでに回転方向があります。回転している角度の記号です。

#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 
関連する問題