System.Drawing.GeomUtilities.PolygonCentroid C# (CSharp) Method

PolygonCentroid() static private method

static private PolygonCentroid ( PointF points ) : PointF
points PointF
return PointF
        internal static PointF PolygonCentroid(PointF[] points)
        {
            var C = PointF.Empty;
            var area = 0f;

            //var A6 = 6.0f * (float)PolygonArea(points);

            var first = points[0];
            var last = points[points.Length - 1];
            // make sure we have a closed path
            if (last != first)
                last = first;

            last = first;

            var dotProd = 0f;

            for (int i = 1; i < points.Length; i++)
            {
                var next = points[i];
                dotProd = last.X * next.Y - next.X * last.Y;
                area += dotProd;
                C.X += (last.X + next.X) * dotProd;
                C.Y += (last.Y + next.Y) * dotProd;

                last = next;
            }

            dotProd = last.X * first.Y - first.X * last.Y;
            area += dotProd;

            C.X += (last.X + first.X) * dotProd;
            C.Y += (last.Y + first.Y) * dotProd;

            var aaa = PolygonArea (points);
            // Note: The result is positive if the polygon is clockwise for our coordinate system in
            // which increasing Y goes downward.
            // Positive - clockwise
            // Negative - counterclockwise
            //
            // We want to keep the area positive so we do not get negative numbers
            // depending on the polygon winding. This may not be right though.
            //
            var A6 = 6.0f * (area / 2);
            var reciprocal = 1.0f / A6;

            // We need make sure we are positive here.
            C.X = C.X * reciprocal;
            C.Y = C.Y * reciprocal;

            return C;
        }