多边形重心问题
时间限制:
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组数据;
分析:
多边形面积公积公式:s=(pp[i%n].x*pp[i-1].y-pp[i%n].y*pp[i-1].x。。。。)/2;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define O_O(x) while(x--)
using namespace std;
const int maxn=10010;
const double inf=1e-8;
struct node
{
double x;
double y;
}pp[maxn];
int main()
{
int t;
SI(t);
O_O(t)
{
int n;
SI(n);
double sum=0.0,temp=0.0,x=0.0,y=0.0;
for(int i=0;i<n;i++)
scanf("%lf%lf",&pp[i].x,&pp[i].y);
for(int i=1;i<=n;i++)
{
temp=(pp[i%n].x*pp[i-1].y-pp[i%n].y*pp[i-1].x)/2.0;
sum+=temp;
x+=temp*(pp[i%n].x+pp[i-1].x)/3.0;
y+=temp*(pp[i%n].y+pp[i-1].y)/3.0;
}
if(fabs(sum-0)<inf)
printf("0.000 0.000\n");
else printf("%.3lf %.3lf\n",fabs(sum),(x+y)/sum);
}
return 0;
}