16

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

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?

Tim Cooper
  • 10,023
  • 5
  • 61
  • 77
Mizipzor
  • 51,151
  • 22
  • 97
  • 138

3 Answers3

1

if you have a look at the angular circle, you will see that there are 2 opposite "angles" that corresponds to your "average".

So both 185° and 5° are correct.

But you mentionned the closer average. So in that case, you may choose the angle that is closer.

Usually, the "average" of angles concerns the counterclockwise direction. The "average" is not the same if you switch your two angles (or if you use the clockwise direction).

For example, with a=20° and b=350°, you are looking for the angle that comes after a and before b in the counterclockwise direction, 185° is the answer. If you are looking for the angle that comes before a and after b in the counterclockwise direction (or after a and before b in the counterclock wise direction), is the answer.

The answer of this post is the right way to do.

So the pseudo-code for the solution is

if (a+180)mod 360 == b then
  return (a+b)/2 mod 360 and ((a+b)/2 mod 360) + 180 (they are both the solution, so you may choose one depending if you prefer counterclockwise or clockwise direction)
else
  return arctan(  (sin(a)+sin(b)) / (cos(a)+cos(b) )
Community
  • 1
  • 1
ThibThib
  • 8,010
  • 3
  • 30
  • 37
  • Yes, its the closer average Im looking for. – Mizipzor Jul 21 '09 at 12:45
  • with a of 180 and b of 270,, I get math:atan( (math:sin(180)+math:sin(270)) / (math:cos(180)+math:cos(270))). -1.1946710584651132 Not expected – quantumpotato Nov 04 '17 at 02:43
  • I've tested this and it's correct. But it's unfortunate that we have to use all these trig functions. – Tim Cooper Sep 15 '20 at 08:25
  • For two angles only, compute the minimal difference: `d = ((b-a+180) mod 360) - 180`, then add half the difference to `a`: `avg = (a+d/2) mod 360`. – Jellby Mar 09 '23 at 14:35
-2

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;
    }

I think it works, the output is

0
270
5
1
weiqure
  • 3,247
  • 2
  • 27
  • 31
-4

Just take a normal average and then take it mod 180. In your example this gives 5 degrees, as expected.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
  • Stecys answer states to take mod 360, which seems to work in the few attempts I tried it in. Where does 360 fails and 180 succeeds? – Mizipzor Jul 21 '09 at 12:47
  • Taking the average like (a+b)/2 and then mod 180 is exactly the same as first doing (a+b) mod 360 and then divide by 2. Both are equivalent. – Ralph M. Rickenbach Jul 21 '09 at 13:14
  • 5
    This does not work. Example: angle1 = 280, angle2 = 10, their mean is 325. Your formula gives 145. – lenooh Jan 13 '16 at 20:38
  • 1
    Not quite. In the sense that the average is the angle that bisects the two angles stated you can see with the aid of a simple diagram that the tow answers are equivalent.. – pauljwilliams Jan 14 '16 at 11:06
  • 2
    I downvoted because the formula doesn't work. This question is deceptively difficult. – Tim Cooper Sep 16 '20 at 04:20