Lifting the Stone
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 n n个顶点)质量分布在各个顶点上,各顶点坐标 ( x i , y i ) (x_i,y_i) (xi,yi),质量 m i m_i mi,则多边形重心 X = ∑ i = 1 n x i m i ∑ i = 1 n m i , Y = ∑ i = 1 n y i m i ∑ i = 1 n m i X=\cfrac{\sum_{i=1}^{n}x_im_i}{\sum_{i=1}^{n}m_i},Y=\cfrac{\sum_{i=1}^{n}y_im_i}{\sum_{i=1}^{n}m_i} X=∑i=1nmi∑i=1nximi,Y=∑i=1nmi∑i=1nyimi
- 多边形(
n
n
n个顶点)质量均匀分布,则对多边形做以下操作
(1). 以一个点为顶点做多个三角形,分别求其重心和质量(因为质量均匀分布可以用面积代替)
(2). 然后以求出个各个三角形重心为顶点做出一个多边形,即为第一种情况。
(3). 以第一种多边形求重心公式求其重心
注意问题:
这题
d
o
u
b
l
e
double
double类型的先除后乘上会出现卡精度问题。纠结了一个多小时的我如是说道。〒▽〒
另外我很奇怪一个问题,题中说四舍五入,是被吃了吗? (╯ ̄Д ̄)╯╘═╛
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y
typedef long long ll;
const int maxn = 1e6;
const double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int eps = 1e-8;
const double inf=1e20;
const double pi = acos(-1.0);
const int maxp = 1010;
//Compares a double to zero
int sgn(double x){
if(fabs(x) < eps) return 0;
if(x < 0)return -1;
else return 1;
}
inline double sqr(double x){return x*x;}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){x = _x,y = _y;}
void input(){scanf("%lf%lf",&x,&y);}
void output(){printf("%.2lf %.2lf\n",x,y);}
bool operator == (Point b)const{return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;}
bool operator < (Point b)const{return sgn(x-b.x) == 0 ? sgn(y-b.y)<0 : x<b.x;}
Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
double operator ^ (const Point &b)const{return x*b.y-y*b.x;} //叉积
double operator * (const Point &b)const{return x*b.x+y*b.y;} //点积 //返回长度平方
double distance(Point p){return hypot(x-p.x,y-p.y);} //返回两点间距离
Point operator + (const Point &b)const{return Point(x+b.x,y+b.y);}//
Point operator * (const double &k)const{return Point(x*k,y*k);} //
Point operator / (const double &k)const{return Point(x/k,y/k);} //
};
Point p[maxn+10];
struct polygon{
int n;
void input(int _n){
n = _n;
for(int i = 0; i < n; ++i) p[i].input();
}
Point getbarycentre(){ //得到重心
Point ret(0,0);
double area = 0;
for(int i = 1; i < n-1; ++i){
double tmp = (p[i]-p[0])^(p[i+1]-p[0]);
if(sgn(tmp) == 0) continue;
area += tmp;
ret.x += tmp*(p[0].x+p[i].x+p[i+1].x)/3;
ret.y += tmp*(p[0].y+p[i].y+p[i+1].y)/3;
}
if(sgn(area)) ret = ret/area;
return ret;
}
};
int main(){
int t;
cin>>t;
while(t--){
polygon a;
int n;
cin>>n;
a.input(n);
Point k = a.getbarycentre();
k.output();
//int k1 = (k.x+0.005)*100,k2 = (k.y+0.005)*100;
//printf("%.2lf %.2lf\n",(double)k1/100,(double)k2/100);
}
return 0;
}
/*
*/