-180和180之间的角度差

时间:2016-05-19 07:12:45

标签: java geometry

我知道找到[0,180]之间两个角度的角度差异的方法,但是我试图找到一种方法来找到[-179,180]之间两个角度的角度差异。

我的代码如下;它没有正常工作:

private int distance2(int alpha, int beta) {
    int phi = beta - alpha;// % 360;    // This is either the distance or 360 - distance
    int distance = 0;
    if (phi < -360) {
        distance = 360 + phi;
    } // end of if
    else{
        distance = phi;  
    }
    return distance;
}

2 个答案:

答案 0 :(得分:0)

这只是做

package angle;

public class Example {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println(distance(-179,180));
}

private static double distance(double alpha, double beta) {
    if(beta>=0 && alpha >=0 && beta<=180 && alpha<=180){
        double phi = (beta - alpha) % 360;       // This is either the distance or 360 - distance
        double distance = phi > 180 ? 360 - phi : phi;
        return distance;
    }else if(beta<0 || alpha < 0){
        double phi = (beta - alpha) % 360;       // This is either the distance or 360 - distance
        double distance = phi;
        return distance;
    }
    return 0;
}
}

打印359.0你可以将它缩小到360距离以获得最小距离

替换为double distance = phi&gt; 180? 360 - phi:phi;

另请参阅

How do I calculate the difference of two angle measures?

答案 1 :(得分:0)

如果你确定alpha和beta都在[-180,180]间隔内,你知道吗

-360&lt; = alpha - beta&lt; = 360

所以你可以使用:

int distance = (beta - alpha) % 360;  // forces distance in ]-360, 360[ range

如果您无法确定输入值,只需将第一行替换为:

 isClicked = false;   

     private void setupFollowButton(Button button, final Boolean isClicked) {
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(isClicked) {
                        v.setBackgroundColor(Color.parseColor("#FF0000"));
                        isClicked = false;
                    } else {
                        v.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        isClicked = true;
                    }
                }
            });
        }