题目大意:给出多边形的边数以及顶点坐标,求多边形的重心。
解题思路:直接就模板了哈,也可以根据定义来切,不过模板要注意顶点坐标是按照顺时针或者逆时针排序的,本题不用,直接输入就是有序的,详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1115
code:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAXN = 1000010;
int n,t;
struct point{
double x,y;
}pnt[MAXN];
point bcenter(point pnt[],int n){
point p,s;
double tp,area=0,tpx=0,tpy=0;
p.x=pnt[0].x;p.y=pnt[0].y;
for(int i=1;i<=n;i++){
s.x=pnt[(i==n)?0:i].x;
s.y=pnt[(i==n)?0:i].y;
tp=(p.x*s.y-s.x*p.y);
area+=tp/2;
tpx+=(p.x+s.x)*tp;
tpy+=(p.y+s.y)*tp;
p.x=s.x;
p.y=s.y;
}
s.x=tpx/(6*area);
s.y=tpy/(6*area);
return s;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
point ans=bcenter(pnt,n);
printf("%.2lf %.2lf\n",ans.x,ans.y);
}
return 0;
}