求多边形重心模板!!!
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define MAXN 1000005
struct centre
{
double x , y ;
}point[MAXN];
int cas , n ;
struct centre get_centre()
{
struct centre ctr ;
double area = 0 ;
ctr.x = ctr.y = 0 ;
point[n] = point[0] ;
for ( int i = 0 ; i < n ; ++ i )
{
area +=( ( point[i].x * point[i+1].y - point[i].y * point[i+1].x ) / 2.0 ) ; // 以原点为定点,求三角形面积
ctr.x += ( point[i].x * point[i+1].y - point[i].y * point[i+1].x ) * ( point[i].x + point[i+1].x ) ;
ctr.y += ( point[i].x * point[i+1].y - point[i].y * point[i+1].x ) * ( point[i].y + point[i+1].y ) ;
}
ctr.x /= ( area * 6 ) ;
ctr.y /= ( area * 6 ) ;
return ctr ;
}
int main ()
{
scanf ( "%d" , &cas ) ;
while ( cas -- )
{
scanf ( "%d" , &n ) ;
for ( int i = 0 ; i < n ; ++ i )
scanf ( "%lf%lf" , &point[i].x , &point[i].y ) ;
struct centre ctr = get_centre();
printf ( "%.2lf %.2lf\n" , ctr.x , ctr.y ) ;
}
return 0 ;
}