本文介绍了如何计算两个角度量度的差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中计算两个角度量度(以度为单位)的差,使结果在[0° ;, 180°]范围内?

How can I calculate the difference of two angle measures (given in degrees) in Java so the result is in the range [0°, 180°]?

例如:

350° to 15° = 25°
250° to 190° = 60°

推荐答案

    /**
     * Shortest distance (angular) between two angles.
     * It will be in range [0, 180].
     */
    public static int distance(int alpha, int beta) {
        int phi = Math.abs(beta - alpha) % 360;       // This is either the distance or 360 - distance
        int distance = phi > 180 ? 360 - phi : phi;
        return distance;
    }

这篇关于如何计算两个角度量度的差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:27