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-2个三角形,求每个三角形的重心,并以三角形面积为权重,求出各重心坐标的加权平均值,就是多边形的重心

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct point{
double x,y;
point operator - (point rhs){return (point){x-rhs.x,y-rhs.y};}
point operator + (point rhs){return (point){x+rhs.x,y+rhs.y};}
}p[mxn];
double Cross(point a,point b){return a.x*b.y-a.y*b.x;}
int n;
double smm;
double SX,SY;
int main(){
int i,j;
int T=read();
while(T--){
smm=SX=SY=;
n=read();
for(i=;i<=n;i++){
p[i].x=read();p[i].y=read();
}
// p[n+1]=p[1];
for(i=;i<n;i++){
double tmp=Cross(p[i]-p[],p[i+]-p[]);
SX+=(p[].x+p[i+].x+p[i].x)*tmp;
SY+=(p[].y+p[i+].y+p[i].y)*tmp;
smm+=tmp;
}
printf("%.2f %.2f\n",SX/smm/,SY/smm/);
}
return ;
}

POJ1385 Lifting the Stone的更多相关文章

  1. POJ1385 Lifting the Stone 多边形重心

    POJ1385 给定n个顶点 顺序连成多边形 求重心 n<=1e+6 比较裸的重心问题 没有特别数据 由于答案保留两位小数四舍五入 需要+0.0005消除误差 #include<iostr ...

  2. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  3. Lifting the Stone(hdoj1115)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

    题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. Lifting the Stone(求多边形的重心—)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  6. hdu 1115 Lifting the Stone 多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. Lifting the Stone(hdu1115)多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...

  8. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu 1115 Lifting the Stone (数学几何)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServiceImpl

    java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServi ...

  2. JavaScript 循环

    for循环:  如果您希望一遍又一遍运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: document.write(cars[0] + "<br ...

  3. 【PHP】常用的PHP正则表达式收集整理

    匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff]评注:可以用来计算字符串的长度 ...

  4. Python学习笔记:math模块(数学),random模块(随机数)

    math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...

  5. ACM二分搜索中的最大化最小值 总结

    这类题目都有个相似的地方就是需要你去找一个临界点. 分析题目要你求什么,例如时间 那么mid就是时间 看求得这个跟什么相关 例如 poj 3258 求得是距离 这个距离跟两者之间的差相关 那题目要求你 ...

  6. Android开发——常见的内存泄漏以及解决方案(一)

    0. 前言   转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52333954 Android的内存泄漏是Android开发领域永恒的 ...

  7. Unity 与Mono和.Net的关系

    一.分析 首先,我们要知道Unity,Mono,.Net 三者的关系.需要简单说一下.Net. .Net拥有跨语言,跨平台性. 跨语言:就是只要是面向.Net平台的编程语言,用其中一种语言编写的类型就 ...

  8. CQRS之旅——旅程5(准备发布V1版本)

    旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...

  9. BZOJ 1222: [HNOI2001]产品加工

    F[i]表示第一个机器用了i的时间,第二个机器的最小时间 转移即可 #include<cstdio> #include<algorithm> using namespace s ...

  10. day38--MySQL基础二

    1.数据库连表 1.1, 一对多 使用外键做约束.注意:外键列的数据类型要一致. 命令的方式创建外键CREATE table part1( nid int not null auto_incremen ...