环绕两个角度的平均值 [英] Average of two angles with wrap around

查看:91
本文介绍了环绕两个角度的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何计算一组角度?

Possible Duplicate:
How do you calculate the average of a set of angles?

我有两个角度,a = 20度和b = 350度.这两个角度的平均值为185度.但是,如果我们认为最大角度为360度并允许回绕,则可以看到5度是更接近的平均值.

I have two angles, a=20 degrees and b=350 degrees. The average of those two angles are 185 degrees. However, if we consider that the maximum angle is 360 degrees and allows for wrap around, one could see that 5 degrees is a closer average.

在计算平均值时,我很难想出一个好的公式来处理环绕问题.有人有提示吗?

I'm having troubles coming up with a good formula to handle that wrap around when calculating average. Anyone got any hints?

还是我在这里用脚向自己开枪?这是数学上的坏习惯"吗?

Or am I shooting myself in the foot here? Is this considered "bad practice" in math?

推荐答案

尝试一下(C#示例):

Try this (example in C#):

    static void Main(string[] args)
    {
        Console.WriteLine(GetAngleAverage(0,0));
        Console.WriteLine(GetAngleAverage(269, 271));
        Console.WriteLine(GetAngleAverage(350, 20));
        Console.WriteLine(GetAngleAverage(361, 361));
    }

    static int GetAngleAverage(int a, int b)
    {
        a = a % 360;
        b = b % 360;

        int sum = a + b;
        if (sum > 360 && sum < 540)
        {
            sum = sum % 180;
        }
        return sum / 2;
    }

我认为它可行,输出是

0
270
5
1

这篇关于环绕两个角度的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆