0

+PIから-PIラジアンまで変化するデータ値があります。

次のように、古い値から新しい値に移動するために必要な最小回転(ラジアン)を取得する必要があります。

float rotationRequired(float oldValue, float newValue){
      return newValue - oldValue;
}

ただし、-179度から+179度に移動するために、時計回りに2度だけ、完全な円を回転させる必要がないため、単純に減算しても効果はありません。円周率の-PI=+ PIは、技術的に同じ回転であるためです。また、値は任意の範囲、つまり740 = 360 + 360 + 20、つまり20のみにすることができます。

私は値をsincos値に分割し、減算してからatan

double oldY =  Math.sin(oldValue);
double oldX =  Math.cos(oldValue);

double newY =  Math.sin(newValue);
double newX =  Math.cos(newValue);

float delta = (float) Math.atan2( (newY - oldY),(newX - oldX) );

しかし、それでも正しい結果が得られないので、誰かが別の方法を提案できますか?

4

2 に答える 2

2

減算を実行し、必要に応じて360を加算または減算して、結果を+/- 180に制限します(%演算子はここで役立つ場合があります...)

于 2013-01-29T16:56:24.727 に答える
0

角度を度に変換し、この方法を使用して、必要な最小回転と方向を提案しました。

public static int suggestRotation(int o, int n){
    //--convert to +0 to +360 range--
    o = normalize(o);
    n = normalize(n);

    //-- required angle change --
    int d1 = n - o;

    //---other (360 - abs d1 ) angle change in reverse (opp to d1) direction--
    int d2 = d1 == 0 ? 0 : Math.abs(360 - Math.abs(d1))*(d1/Math.abs(d1))*-1;

    //--give whichever has minimum rotation--
    if(Math.abs(d1) < Math.abs(d2)){
        return d1;
    }else {
        return d2;
    }

}

private static int normalize(int i){
    //--find effective angle--
    int d = Math.abs(i) % 360;

    if(i < 0){
    //--return positive equivalent--
        return 360 - d;
    }else {
        return d;
    }
}
于 2013-01-29T20:52:51.727 に答える