The centre of polygon tzc

The center of gravity (also known as "center of mass" or "centroid" can be calculated with the following formula:

X = SUM[(Xi + Xi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A
Y = SUM[(Yi + Yi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A

The centroid of a non-self-intersecting closed polygon defined by n vertices (x0,y0), (x1,y1), ..., (xn−1,yn−1) is the point (Cx, Cy), where
X coordinate of the center
Y coordinate of the center
and where A is the polygon's signed area,
Area formula

becenter实现上述计算

#include<cstdio>
#include<cmath>
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);
	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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值