Averaging angles

后端 未结 4 581
执笔经年
执笔经年 2020-12-19 15:12

I looked at some solutions here but none provide what I need, so:

I need to average an array of angles(0 to 359.9, no negatives) (A1 + A2 + A3 + An) / n

The

相关标签:
4条回答
  • 2020-12-19 15:42

    A ray leaving a circle can be represented in more than one manner. It could be 0 degrees, 360 degrees, 720 degrees, etc. You need to determine what is the correct acceptable answer for you case, and covert intermediate answers to the final answer before presenting it.

    1 + 359 + 2 + 358 = 360 + 360 = 720 degrees in total
    720 / 4 = 360 / 2 = 180 degrees on average
    

    It's not that the answer should be zero, it's that 359 degrees is not equivalent to -1 degrees because the angle goes around the circle "the other way".

    0 讨论(0)
  • 2020-12-19 15:47

    Do you want to round it off i.e. 0 should come after 359 degree?

    take a modulus of the result with 360.

    avg = ( (a1 + a2 + an) /n ) % 360
    
    0 讨论(0)
  • 2020-12-19 15:56

    Add unit vectors of each angle, and convert the resulting vector back into an angle. If the result vector is of zero length, the inputs cancelled each other out and the result is indeterminate.

    A unit vector has a length of 1, and its x and y lengths are given by the cosine and sine of the angle. Thus you average your examples as in the following pseudo-code:

    x = cos(radians(1)) + cos(radians(359)) + cos(radians(2)) + cos(radians(358));
    y = sin(radians(1)) + sin(radians(359)) + sin(radians(2)) + sin(radians(358));
    angle = degrees(atan2(y, x));
    
    0 讨论(0)
  • 2020-12-19 16:03

    Averaging degrees is a little trickier than it sounds. I had to do this at work the other day and want to share my findings.

    1. Adding 360 to any of your values should not change your average.
      e.g. avg(2 4 18) = avg(362,4,18) = 8
    2. Shifting all values by a constant value should shift the average equally much.
      e.g. avg(2-3,4-3,18-3) = avg(359,1,15) = 8-3
    3. If your degrees "cancel out", e.g. 0,120,240, the average is undefined
      (i.e. there are several equally good answers).
      I see no way to deal with this other than giving an error message.

    After falling into several traps, I ended up with the following definition: Average is the value which minimizes the variance.

    Once given the average, variance can be computed by:

    1. Shifting all values by the same angle such that the average is at 180.
      (This is equivalent to "cutting" the circle at the opposite side of the average.)
    2. Normalizing all the angles to be between 0 and 360.
    3. Computing the normal average.
    4. Shifting the average back by the angle you shifted the values before (reversing step 1).

    This takes O(n^2) time.

    However, if all your values lie within a span of 180 degree, you can shift the values such that 0 is contained in the biggest value gap and then compute the normal average. This takes O(n) time.

    0 讨论(0)
提交回复
热议问题