http://acm.nyist.net/JudgeOnline/problem.php?pid=3
多边形重心问题
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
5
-
描述
-
在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形;
如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和;-
输入
-
第一行有一个整数0<n<11,表示有n组数据;
每组数据第一行有一个整数m<10000,表示有这个多边形有m个顶点;
输出
- 输出每个多边形的面积、重心横纵坐标的和,小数点后保留三位; 样例输入
-
3 3 0 1 0 2 0 3 3 1 1 0 0 0 1 4 1 1 0 0 0 0.5 0 1
样例输出
-
0.000 0.000 0.500 1.000 0.500 1.000
-
第一行有一个整数0<n<11,表示有n组数据;
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct point{
double x,y;
point(){}
point(double a,double b):x(a),y(b){}
friend point operator+(const point &a,const point &b) {
return point(a.x+b.x,a.y+b.y);
}
friend point operator*(const point &a,const double &b){
return point(b*a.x,b*a.y);
}
friend point operator/(const point &a,const double &b){
return point(a.x/b,a.y/b);
}
}a[10010];
const double eps=1e-8;
int cmp(double x){
if(fabs(x)<eps)return 0;
if(x>0)return 1;
return -1;
}
double det(point a,point b){
return a.x*b.y-b.x*a.y;
}
double area(int n){
double sum=0;
a[n]=a[0];
for(int i=0;i<n;i++)sum+=det(a[i+1],a[i]);
return fabs(sum/2);
}
point MassCenter(int n){
point ans=point(0,0);
if(cmp(area(n))==0)return ans;
a[n]=a[0];
for(int i=0;i<n;i++)
ans=ans+(a[i]+a[i+1])*det(a[i+1],a[i]);
return ans/area(n)/6.;
}
int main()
{
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&m);
for(int j=0;j<m;j++)
scanf("%lf%lf",&a[j].x,&a[j].y);
point ans=MassCenter(m);
printf("%.3lf %.3lf\n",area(m),ans.y+ans.x);
}
return 0;
}