Reprinted please indicate the source: http://blog.csdn.net/u012860063
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1115
Lifting the stone
Problem descriptionthere are eclipsecret openings in the floor which are covered by a big heavy stone. when the stone is lifted up, a special mechanic detects this and activates initialize ONED 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 center 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 center of gravity for the given polygon.
Inputthe 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 (could t 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.
Outputprint exactly one line for each test case. the line shoshould contain exactly two numbers separated by one space. these numbers are the coordinates of the center 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 center 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 center anyway.
Sample Input
245 00 5-5 00 -541 111 111 111 11
Sample output
0.00 0.006.00 6.00
It is to give you the coordinates of the vertices of a multilateral row and find the center of gravity of the polygon.
A polygon Center of Gravity template question!
The Code is as follows:
// Calculate the polygon Center (using the template of Jilin University) # include <cstdio> # include <cmath> # include <cstring> struct point {Double X, Y;} PP [1000047]; point bcenter (point PNT [], int N) {point P, S; double TP, Area = 0, tpx = 0, TPY = 0; p. X = PNT [0]. x; p. y = PNT [0]. y; For (INT I = 1; I <= N; ++ I) {// point: 0 ~ N-1s.x = PNT [(I = N )? 0: I]. X; S. Y = PNT [(I = N )? 0: I]. 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 N, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% lf ", & PP [I]. x, & PP [I]. y);} Point Ss = bcenter (PP, n); printf ("%. 2lf %. 2lf \ n ", SS. x, SS. y);} return 0 ;}
The template is as follows:
struct point {double x, y; };point bcenter(point pnt[], int n){point p, s;double tp, area = 0, tpx = 0, tpy = 0;p.x = pnt[0].x; p.y = pnt[0].y;for (int i = 1; i <= n; ++i) { // point: 0 ~ n-1s.x = pnt[(i == n) ? 0 : i].x;s.y = pnt[(i == n) ? 0 : i].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;}