现在的位置: 首页 > 综合 > 正文

HDU 1115 Lifting the Stone 多边形的重心

2018年05月02日 ⁄ 综合 ⁄ 共 2509字 ⁄ 字号 评论关闭

Lifting the Stone

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

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
/*
HDOJ 1115 多边形的重心
不可直接用: x =(x1+x2+...+xn/n ,y=(y1+y2+..。+yn)/n;
 
切分成N-2个三角形,然后分别算出每个三角形的重心和面积,
面积通过叉积算出;
 
N边形的重心坐标是
x = (Σ(Ai*pi.x))/S   Σ的i是从1到N-2  S是多边形面积  Ai是面积  
pi.x是第i块三角形的重心,三角形的重心就是坐标加起来除以3 同理可得y

http://www.cnblogs.com/jbelial/archive/2011/08/08/2131165.html

*/ 
#include<iostream>
#include<stdio.h>
using namespace std;

struct point{    
    int x,y;    
};        
   
point d[1000001];
 
double multi(point a,point b,point c)//面积  
{      
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);      
}      
       
int main()
{
	int n,i,t;
	double ansX,ansY,s,area;
	
	//freopen("test.txt","r",stdin);
	scanf("%d",&t); 
	while(t--)	
	{	
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d%d",&d[i].x,&d[i].y);
		
		s=0;
		ansX=0;
		ansY=0;
		
		for(i=1;i<n-1;i++)	
		{
			area=multi(d[0],d[i],d[i+1])/2;//面积 
			s+=area;//总面积s 
			ansX+=(d[0].x+d[i].x+d[i+1].x)*area/3;//重心*面积 再累加 
			ansY+=(d[0].y+d[i].y+d[i+1].y)*area/3;
		}
		
		ansX/=s;
		ansY/=s;
		
		printf("%.2lf %.2lf\n",ansX,ansY);
	}
	return 0;
}

抱歉!评论已关闭.