Sign up ×

For a piece of code I am writing to smooth out movements I need to calculate the average angle over the past 5 recorded angles given (used to give directionality to an object)

This can be achieved quite simply by calculating the median of the 5 previous angles.

However, the angles given range from 0 degrees to 360 degrees so we get an immediate issue here.

Once you move over 360 degrees the angle is reset back to 0 so if we was to send 0 to the array of previous angles then the following would happen:

(355 + 359 + 360 + 360 + 1) / 5 = 287

Now obviously 287 is completely the wrong angle and gives an abnormal movement once we cross this 360 degree border.

I've tried checking if the previous values are on the 360 side or the 0 side then adjusting the new value accordingly but we get an issue with 1; the performance (there is a very short update time before it effects the user interface) and 2; when we get to 720 it will have to keep looping around again.

I don't have a very good background with maths so I thought I would ask here as my last resort but is there a way/formula I can calculate the average with the 360 to 0 gap in mind and give a result on the correct side of this instead of just giving a false value?

Thanks for looking, please let me know if i need to provide any more information to help you :)


share|cite|improve this question

2 Answers 2

up vote 1 down vote accepted

If the angles are close to each other you can do this. Your thought on choosing the multiple of 360 is correct. If they are very close, you can just check whether the average is close to one sample and add or subtract $72$ degrees $(\frac {360}5)$ as required.

If the angles are not close, the concept of an average is not well defined. What is the average of $0,70,140,210,280?$

share|cite|improve this answer

One straightforward way to solve this is to convert the headings (angles) to Cartesian coordinates, average the x and y components separately, then convert back to a heading. So the answer is: AverageAngle = ATAN2(sum_of_sin(theta), sum_of_cos(theta))

share|cite|improve this answer

Your Answer


