首页 > 代码库 > HDOJ 1115 Lifting the Stone 多边形重心

HDOJ 1115 Lifting the Stone 多边形重心


来自:http://blog.csdn.net/tiaotiaoyly/article/details/2087498

1,质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心
  X = ∑( xi×mi ) / ∑mi
  Y = ∑( yi×mi ) / ∑mi
  特殊地,若每个点的质量相同,则
  X = ∑xi  / n
  Y = ∑yi  / n

2,质量分布均匀。这个题就是这一类型,算法和上面的不同。
  特殊地,质量均匀的三角形重心:
  X = ( x0 + x1 + x2 ) / 3
  Y = ( y0 + y1 + y2 ) / 3

若我们求出了每个三角形的重心和质量,可以构造一个新的多边形,顶点为所有三角形的重心,顶点质量为三角形的质量。这个新多边形的质量和重心与原多边形相同,即可使用第一种类型的公式计算出整个多边形的重心。

由于三角形的面积与质量成正比,所以我们这里用面积代替质量来计算。

现在有个问题就是,多边形有可能为凹多边形,三角形有可能在多边形之外。如何处理这种情况呢?

很简单,我们使用叉积来计算三角形面积,当三角形在多边形之外时,得到“负面积”就抵消掉了。
S =( x0*y1 + x1*y2 + x2*y0
     - x1*y0  - x2*y1  - x0*y2 ) /2;



Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5733    Accepted Submission(s): 2400


Problem Description
There are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon. 
 

Input
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. 
 

Output
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. 
 

Sample Input
2 4 5 0 0 5 -5 0 0 -5 4 1 1 11 1 11 11 1 11
 

Sample Output
0.00 0.00 6.00 6.00
 

Source
Central Europe 1999
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1001000;

struct Point
{
	double x,y;
}p[maxn];

int n;

int main()
{
	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			double x,y;
			scanf("%lf%lf",&x,&y);
			p[i].x=x; p[i].y=y;
		}

		double x0=p[0].x; double y0=p[0].y;
		double x1=p[1].x; double y1=p[1].y;
		double x,y,x2,y2;
		double suma=0,sumx=0,sumy=0;

		for(int i=2;i<n;i++)
		{
			x2=p[i].x;
			y2=p[i].y;

			/// 三角型的重心
			x=x0+x1+x2;
			y=y0+y1+y2;

			double dx1=(x1-x0),dy1=(y1-y0);
			double dx2=(x2-x0),dy2=(y2-y0);

			/// 三角形面积
			double area=dx1*dy2-dx2*dy1;

			suma+=area;
			sumx+=area*x;
			sumy+=area*y;

			x1=x2; y1=y2;
		}
		printf("%.2lf %.2lf\n",sumx/suma/3.,sumy/suma/3.);
	}
	return 0;
}



HDOJ 1115 Lifting the Stone 多边形重心