HDU 1115 Lifting the Stone (多边形重心)

来源:互联网 发布:ubuntu服务器版下载 编辑:程序博客网 时间:2024/05/23 11:03

Lifting the Stone

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

题目大意:
有一个密度均匀的平面N多边形(3 <= N <= 1000000),可能凹也可能凸,但没有边相交叉,
另外已知N个有序(顺时针或逆时针)顶点的坐标值,第j个顶点坐标为(Xi , Yi ),且满
足 (|Xi|, |Yi| <= 20000),求这个平面多边形的重心。

思路:
从第1个顶点出发,分别连接第i, i+1个顶点组成三角形Ti,1 < i < n,
一共n-2个三角形正好是多连形的一个划分,分别求出每个三角形的面积Si,
总面积为各个面积相加
根据物理学知识得:n个点(xi,yi)每个重量是mi,则重心是
X = (x1*M1+x2*M2+…+xn*Mn) / (M1+M2+….+Mn)
Y = (y1*M1+y2*M2+…+yn*Mn) / (M1+M2+….+Mn)
另个需要用的知识有:
已知3点求三角形的面积,设三点分别为p[0].x, p[0].y p[1].x, p[1].y p[1].x, p[1].y
面积s =[ p[0].x*p[1].y-p[1].x*p[0].y + p[1].x*p[2].y-p[2].x*p[1].y + p[2].x*p[0].y-p[0].x*p[2].y ] / 2 ,
这是这3个点是逆时针的值,顺时针取负。
已知3点求重心,x = (p[0].x+p[1].x+p[2].x)/3.0 , y = (p[0].y+p[1].y+p[2].y)/3.0
另外在求解的过程中,不需要考虑点的输入顺序是顺时针还是逆时针,后就抵消了
不必在求每个小三角形的重心时都除以3,可以在最后除。

#include <cstdio>  #include <cmath>  struct Point{      double x, y;  };double Area(Point p0, Point p1, Point p2){//已知三角形三个顶点,求三角形的面积       double k = p0.x * p1.y + p1.x * p2.y          + p2.x * p0.y - p1.x * p0.y           - p2.x * p1.y - p0.x * p2.y;       return k /= 2;  }  int main(){      int n, T;      Point p0, p1, p2, center;      double area, sumarea, sumx, sumy;          scanf("%d", &T);        while( T-- ){          scanf("%d", &n);          scanf("%lf%lf", &p0.x, &p0.y);          scanf("%lf%lf", &p1.x, &p1.y);          sumx = sumy = sumarea = 0;          for(int i=2; i<n; i++){              scanf("%lf%lf", &p2.x, &p2.y);              center.x = p0.x + p1.x + p2.x;              center.y = p0.y + p1.y + p2.y;                area =  Area(p0, p1, p2);              sumarea += area;//图形总面积             sumx += center.x * area;              sumy += center.y * area;               p1 = p2;        }          printf("%.2lf %.2lf\n", sumx / sumarea / 3, sumy / sumarea / 3);      }      return 0;  }  
原创粉丝点击