views:

577

answers:

4

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

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?

+5  A: 

EDITED (See below)...


Do the following:

((a + b) mod 360) / 2

So with a=20 and b=350 you have

((20 + 350) mod 360) / 2 = (370 mod 360) / 2 = 10 / 2 = 5

For a=10 and b=20 you have

((10 + 20) mod 360) / 2 = (30 mod 360) / 2 = 30 / 2 = 15


Here's what I propose:

x = ABS(a-b) mod 360
if x >=0 and x <= 180 then
   result = ((a + b) / 2) mod 360
else if x > 180 and x < 270 then
   result = (((a + b) / 2) mod 360) + 180
else
   result = (((a + b) / 2) mod 360) - 180
Stecy
Well the division happens after the modulus operation...I'll update my answer with parenthesis...
Stecy
Thank you! You should post your answer in the question that's mine is marked as a duplicate of, so we can close this one. I think your answer was clearer than most in the other question. Now, I will proceed to have a brief "why didn't I think of that" moment. ;)
mizipzor
You can mark my answer as the accepted answer as well... ;)
Stecy
Fishing for rep are we? ;) Hold on, first I want to prove or disprove Visages point. Should it be mod 360 or mod 180?
mizipzor
Nah, I have more than enough. What's the point of accumulating reps anyway. As long as I have what I need to get the job done. I was only suggesting to accept the answer to have it visible for anyone else at the top of the page... And btw, no offense taken. ;)
Stecy
So with 270 and 270, the average is what, using your formula?
Matt Howells
Uhh.. this appears broken to me. 350 and 2 should be 356, not 176.
darron
This solution doesn't work. Mizipzor is looking for the "closer average". So if the angles are 20° and 200°, you should provide 2 equivalent solutions, 110° and 290°.And if the angles are 20° and 210°, the correct answer is not 115° but 295°
ThibThib
Can we do a duplicate answer? http://stackoverflow.com/questions/491738/how-do-you-calculate-the-average-of-a-set-of-angles/1158977#1158977
ThePower
@ThePower: Read the above comments and look at the time on the two answers. :)
Bryan
+1  A: 

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

Visage
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
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 Rickenbach
+3  A: 

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
I think you want to remove the "return" inside the if() block. GetAngleAverage(350,20) should be 5, not 10.
Adam Liss
Right. Fixed that.
A: 

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) )
ThibThib
Yes, its the closer average Im looking for.
mizipzor