NYOJ3—多边形重心问题(计算几何)&&hdu1115 Lifting the Stone

来源:互联网 发布:java field 编辑:程序博客网 时间:2024/06/13 07:04

多边形重心问题

时间限制: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

#include<cstdio>int cases,n;double a[10002],b[10002],t,xx,yy,area;inline double fun(int i){return (a[i]*b[i+1]-a[i+1]*b[i])/2;}inline double ffabs(double i){return i>0?i:-i;}int main(){scanf("%d",&cases);while(cases--){xx=yy=area=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i],&b[i]);a[n+1]=a[1],b[n+1]=b[1];for(int i=1;i<=n;i++){t=fun(i);area+=t;xx+=t*(a[i]+a[i+1]);yy+=t*(b[i]+b[i+1]);}xx=xx/3/area;yy=yy/3/area;area=ffabs(area);if(area<1e-8)printf("0.000 0.000\n");elseprintf("%.3lf %.3lf\n",area,xx+yy);}return 0;}#include<cstdio>int cases,n;double a[10002],b[10002],t,xx,yy,area;inline double fun(int i){return (a[i]*b[i+1]-a[i+1]*b[i])/2;}inline double ffabs(double i){return i>0?i:-i;}int main(){scanf("%d",&cases);while(cases--){xx=yy=area=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i],&b[i]);a[n+1]=a[1],b[n+1]=b[1];for(int i=1;i<=n;i++){t=fun(i);area+=t;xx+=t*(a[i]+a[i+1]);yy+=t*(b[i]+b[i+1]);}xx=xx/3/area;yy=yy/3/area;area=ffabs(area);if(area<1e-8)printf("0.000 0.000\n");elseprintf("%.3lf %.3lf\n",area,xx+yy);}return 0;}

*①质量集中在顶点上
* n
个顶点坐标为(xi,yi),质量为mi,则重心
* X = ∑( xi×mi ) / ∑mi
* Y = ∑( yi×mi ) / ∑mi
* 特殊地,若每个点的质量相同,则
* X = ∑xi / n
* Y = ∑yi / n
*②质量分布均匀
* 特殊地,质量均匀的三角形重心:
* X = ( x0 + x1 + x2 ) / 3
* Y = ( y0 + y1 + y2 ) / 3
*③三角形面积公式:S = ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2- y1) ) / 2 ;
*做题步骤:1、将多边形分割成n-2个三角形,根据③公式求每个三角形面积。
* 2、根据②求每个三角形重心。
* 3、根据①求得多边形重心。
**/

两种方法:

一种是将n个点,以其中一个点为标准,分成n-2个三角形,再进行求重心。

另一种是以原点为依据分成n+1个三角形,再进行求重心。

Lifting the Stone

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others)
Total Submission(s):7885    Accepted Submission(s): 3331



ProblemDescription

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

 

 

Input

The input consists of T test cases. The number of them (T) isgiven on the first line of the input file. Each test case begins with a linecontaining a single integer N (3 <= N <= 1000000) indicating the numberof points that form the polygon. This is followed by N lines, each containingtwo integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are thecoordinates 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 (exceptthe neighboring ones) and that they never cross. The area of the polygon isnever zero, i.e. it cannot collapse into a single line. 

 

 

Output

Print exactly one line for each test case. The line shouldcontain exactly two numbers separated by one space. These numbers are thecoordinates of the centre of gravity. Round the coordinates to the nearestnumber with exactly two digits after the decimal point (0.005 rounds up to0.01). Note that the centre of gravity may be outside the polygon, if its shapeis not convex. If there is such a case in the input data, print the centreanyway. 

 

 

SampleInput

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

 

 

SampleOutput

0.00 0.00
6.00 6.00

 

#include<cstdio>int cases,n;double a[10002],b[10002],t,xx,yy,area;inline double fun(int i){return (a[i]*b[i+1]-a[i+1]*b[i])/2;}inline double ffabs(double i){return i>0?i:-i;}int main(){scanf("%d",&cases);while(cases--){xx=yy=area=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i],&b[i]);a[n+1]=a[1],b[n+1]=b[1];for(int i=1;i<=n;i++){t=fun(i);area+=t;xx+=t*(a[i]+a[i+1]);yy+=t*(b[i]+b[i+1]);}xx=xx/3/area;yy=yy/3/area;printf("%.2lf %.2lf\n",xx,yy);}return 0;}


1 0