title: Averaging circular values (particularly Hues in HSL color scheme)
body: <p>So I was trying to puzzle out how to calculate the average hue of a number of objects whose colors are represented by HSL values.  Thankfully, I stumbled across <a href="https://stackoverflow.com/questions/8169654/how-to-calculate-mean-and-standard-deviation-for-hue-values-from-0-to-360">this Stack Overflow post</a>, and set to work implementing the algorithm provided in the top answer (I am working in C++).</p>
  
  <p>Unfortunately, my implementation doesn't seem to work.  Here it is, in full; note that though I write "Hue" I am using angles, in degrees, as per the initial implementation (switching from 0-360 angles to 0-256 hues, once I know my code works, shouldn't be hard).</p>
  
  <pre><code>#include &lt;iostream&gt;    
  #include &lt;vector&gt;
  #include &lt;cmath&gt;
  
  #define PI (4*atan(1))
  
  int main()
  {
      ///
      /// Calculations adapted from this source:
      /// https://stackoverflow.com/questions/8169654/how-to-calculate-mean-and-standard-deviation-for-hue-values-from-0-to-360
  
      std::vector&lt;double&gt; Hues = {355, 5, 5, 5, 5};
  
      //These will be used to store the sum of the angles
      double X = 0.0;
      double Y = 0.0;
  
      //Loop through all H values
      for (int hue = 0; hue &lt; Hues.size(); ++hue)
      {
          //Add the X and Y values to the sum X and Y
          X += cos(Hues[hue] / 180 * PI);
          Y += sin(Hues[hue] / 180 * PI);
      }
  
      //Now average the X and Y values
      X /= Hues.size();
      Y /= Hues.size();
  
      //Get atan2 of those
      double AverageColor = atan2(X, Y) * 180 / PI;
  
      std::cout &lt;&lt; "Average: " &lt;&lt; AverageColor &lt;&lt; "\n";
      return 0;
  }
  </code></pre>
  
  <p>Instead of the expected answer of 3 (since 355 should be equivalent to -5 in this scheme), I get 86.9951.</p>
  
  <p>Can somebody point out what I'm doing wrong?  This seems very basic.</p>