hdu1115(求多边形重心模板)

求多边形重心模板!!!

 

#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 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值