The centre of polygon (多边形重心)


描述

Given a polygon, your task is to find the centre of gravity for the given polygon.

输入

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line.

输出

Print exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway.

样例输入


2
4
5 0
0 5
-5 0
0 -5
4
1 1
11 1
11 11
1 11

样例输出


0.00 0.00
6.00 6.00
YYYYYYYYYYYYYYYYYYYYYYYYYYYYY

#include<stdio.h>
#include<math.h>
struct point
{
	double x,y;
}pp[1000008];
point becenter(point pnt[],int m)//借的模板;
{
	point p,s;
	double tp,area=0,tpx=0,tpy=0;
	int i;
	p.x=pnt[0].x,p.y=pnt[0].y;
	for(i=1;i<=m;i++)
	{
		s.x=pnt[i%m].x;
		s.y=pnt[i%m].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);
	//printf("tpx=%.2lf tpy=%.2lf area=%.2lf\n",tpx,tpy,area);
	//printf("%.2lf %.2lf\n",s.x,s.y);
	return s;
}
int main()
{
	int i,j;
	double l,k,d,n;
	int ncase,m;
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%d",&m);
		for(i=0;i<m;i++)
		scanf("%lf%lf",&pp[i].x,&pp[i].y);
		point c=becenter(pp,m);
		printf("%.2lf %.2lf\n",c.x,c.y);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值