POJ 1385 Lifting the Stone【多边形重心】

题意:   求多边形重心。

分析: 直接利用公式。

#include<stdio.h>
#include<string.h>
#include<math.h>
struct node
{
    double x,y;
}q[1000005],cen;
double mul(node a,node b,node c)
{
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int main()
{
    double s,a;
    int t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        cen.x=cen.y=0;
        s=0;
        for(i=0;i<n;i++)
            scanf("%lf%lf",&q[i].x,&q[i].y);
        for(i=1;i<n-1;i++)
        {
            a=mul(q[i],q[i+1],q[0]);
            cen.x+=a*(q[0].x+q[i].x+q[i+1].x);
            cen.y+=a*(q[0].y+q[i].y+q[i+1].y);
            s+=a;
        }
        cen.x/=s*3;
        cen.y/=s*3;
        printf("%.2lf %.2lf\n",cen.x,cen.y);
    }
    return 0;
}

 

posted @ 2012-08-17 12:52  'wind  阅读(204)  评论(0编辑  收藏  举报