How to Calculate Centroid

后端 未结 4 1581
长情又很酷
长情又很酷 2021-02-06 03:15

I am working with geospatial shapes and looking at the centroid algorithm here,

http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

I have implemented the

4条回答
  •  天涯浪人
    2021-02-06 04:01

    public static Point GetCentroid( Point[ ] nodes, int count )
    {
        int x = 0, y = 0, area = 0, k;
        Point a, b = nodes[ count - 1 ];
    
        for( int i = 0; i < count; i++ )
        {
            a = nodes[ i ];
    
            k = a.Y * b.X - a.X * b.Y;
            area += k;
            x += ( a.X + b.X ) * k;
            y += ( a.Y + b.Y ) * k;
    
            b = a;
        }
        area *= 3;
    
        return ( area == 0 ) ? Point.Empty : new Point( x /= area, y /= area );
    }
    

提交回复
热议问题