I'm doing a routine where I get an indefinite number of values from 0-360, which are angles in degrees. I want to calculate the normal, which is the average in the middle of the start and end angle. The problem is around the switch of 360.

Example 1:

0, 45, 90 = Average is 45

Example 2:

90, 180, 270 = Average is 180

Example 3:

315, 0, 45 = The normal here is 0, which is easy to see, but to calculate it i need to add 360 to 0 and 45, calculate, then remove 360 again. (315+360+405)/3 = 360 -> 0.

So how can I do this programmatically to make it work for any angles? I guess the magic part is to know when to add 360 to not mess up in the crossover at 360 degrees.

This is not really a C question, but since I'm coding it in C/ObjC I might as well ask here.