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
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 1000009 #define N 21 #define MOD 1000000 #define INF 1000000009 const double eps = 1e-8; const double PI = acos(-1.0); /* 所有线段投射到给定线段上取交集,如果交集距离大于eps 存在!s */ int sgn(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; else return 1; } struct Point { double x, y; Point() {} Point(double _x, double _y) :x(_x), y(_y) {} Point operator - (const Point& r)const { return Point(x - r.x, y - r.y); } double operator ^(const Point& r)const { return x*r.y - y*r.x; } double operator * (const Point& r)const { return x*r.x + y*r.y; } }; double dist(Point a, Point b) { return sqrt((a - b)*(a - b)); } struct Line { Point s, e; Line() {} Line(Point _a, Point _B) :s(_a), e(_B) {} }; bool Seg_inter_line(Line l1, Line l2) { return sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0; } bool cross(Line l1, Line l2) { return max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) && max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) && max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) && max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) && sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0 && sgn((l1.s - l2.e) ^ (l2.s - l2.e))*sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= 0; } Point a[MAXN]; double CalcArea(Point p[], int n) { double res = 0; for (int i = 0; i < n; i++) res += (p[i] ^ p[(i + 1) % n]) / 2; return fabs(res); } bool isconvex(Point p[], int n) { bool s[3]; memset(s, false, sizeof(s)); for (int i = 0; i < n; i++) { s[sgn((p[(i + 1) % n] - p[i]) ^ (p[(i + 2) % n] - p[i])) + 1] = true; if (s[0] && s[2]) return false; } return true; } double ci[MAXN]; Point ti[MAXN]; Point Calgravitycenter(Point p[], int n) { Point res(0, 0); double area = 0; for (int i = 0; i < n; i++) { ci[i] = (p[i] ^ p[(i + 1) % n]) ; ti[i].x = (p[i].x + p[(i + 1) % n].x); ti[i].y = (p[i].y + p[(i + 1) % n].y); res.x += ti[i].x * ci[i]; res.y += ti[i].y * ci[i]; area += ci[i]/2; } res.x /= (6*area); res.y /= (6*area); return res; } int main() { int T,n; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf", &a[i].x, &a[i].y); Point ans = Calgravitycenter(a, n); printf("%.2lf %.2lf ", ans.x, ans.y); } }