title: How to Calculate Centroid
body: <p>I am working with geospatial shapes and looking at the centroid algorithm here,</p>
  
  <p><a href="http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon" rel="noreferrer">http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon</a></p>
  
  <p>I have implemented the code in C# like this (which is just this adapted),</p>
  
  <p><a href="https://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon">Finding the centroid of a polygon?</a></p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          List&lt;Point&gt; vertices = new List&lt;Point&gt;();
  
          vertices.Add(new Point() { X = 1, Y = 1 });
          vertices.Add(new Point() { X = 1, Y = 10 });
          vertices.Add(new Point() { X = 2, Y = 10 });
          vertices.Add(new Point() { X = 2, Y = 2 });
          vertices.Add(new Point() { X = 10, Y = 2 });
          vertices.Add(new Point() { X = 10, Y = 1 });
          vertices.Add(new Point() { X = 1, Y = 1 });
  
          Point centroid = Compute2DPolygonCentroid(vertices);
      }
  
      static Point Compute2DPolygonCentroid(List&lt;Point&gt; vertices)
      {
          Point centroid = new Point() { X = 0.0, Y = 0.0 };
          double signedArea = 0.0;
          double x0 = 0.0; // Current vertex X
          double y0 = 0.0; // Current vertex Y
          double x1 = 0.0; // Next vertex X
          double y1 = 0.0; // Next vertex Y
          double a = 0.0;  // Partial signed area
  
          // For all vertices except last
          int i=0;
          for (i = 0; i &lt; vertices.Count - 1; ++i)
          {
              x0 = vertices[i].X;
              y0 = vertices[i].Y;
              x1 = vertices[i+1].X;
              y1 = vertices[i+1].Y;
              a = x0*y1 - x1*y0;
              signedArea += a;
              centroid.X += (x0 + x1)*a;
              centroid.Y += (y0 + y1)*a;
          }
  
          // Do last vertex
          x0 = vertices[i].X;
          y0 = vertices[i].Y;
          x1 = vertices[0].X;
          y1 = vertices[0].Y;
          a = x0*y1 - x1*y0;
          signedArea += a;
          centroid.X += (x0 + x1)*a;
          centroid.Y += (y0 + y1)*a;
  
          signedArea *= 0.5;
          centroid.X /= (6*signedArea);
          centroid.Y /= (6*signedArea);
  
          return centroid;
      }
  }
  
  public class Point
  {
      public double X { get; set; }
      public double Y { get; set; }
  }
  </code></pre>
  
  <p>The problem is that this algorithm when I have this shape (which is an L shape),</p>
  
  <p>(1,1) (1,10) (2,10) (2,2) (10,2) (10,1) (1,1)</p>
  
  <p>It gives me the result (3.62, 3.62). Which is OK, except that point is outside the shape. Is there another algorithm around that takes this into account?</p>
  
  <p>Basically a person is going to be drawing a shape on a map. This shape might span multiple roads (so could be an L shape) and I want to work out the centre of the shape. This is so I can work out the road name at that point. It doesn't make sense to me for it to be outside the shape if they have drawn a long skinny L shape.</p>