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-;
const double PI = acos(-1.0);
/*
所有线段投射到给定线段上取交集,如果交集距离大于eps 存在!s
*/
int sgn(double x)
{
if (fabs(x) < eps) return ;
if (x < ) return -;
else return ;
}
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)) <= ;
}
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)) <= &&
sgn((l1.s - l2.e) ^ (l2.s - l2.e))*sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= ;
}
Point a[MAXN];
double CalcArea(Point p[], int n)
{
double res = ;
for (int i = ; i < n; i++)
res += (p[i] ^ p[(i + ) % n]) / ;
return fabs(res);
}
bool isconvex(Point p[], int n)
{
bool s[];
memset(s, false, sizeof(s));
for (int i = ; i < n; i++)
{
s[sgn((p[(i + ) % n] - p[i]) ^ (p[(i + ) % n] - p[i])) + ] = true;
if (s[] && s[])
return false;
}
return true;
}
double ci[MAXN];
Point ti[MAXN];
Point Calgravitycenter(Point p[], int n)
{
Point res(, );
double area = ;
for (int i = ; i < n; i++)
{
ci[i] = (p[i] ^ p[(i + ) % n]) ;
ti[i].x = (p[i].x + p[(i + ) % n].x);
ti[i].y = (p[i].y + p[(i + ) % n].y);
res.x += ti[i].x * ci[i];
res.y += ti[i].y * ci[i];
area += ci[i]/;
}
res.x /= (*area);
res.y /= (*area);
return res;
}
int main()
{
int T,n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf%lf", &a[i].x, &a[i].y);
Point ans = Calgravitycenter(a, n);
printf("%.2lf %.2lf\n", ans.x, ans.y);
} }

Lifting the Stone 计算几何 多边形求重心的更多相关文章

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

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

  2. POJ 1385 Lifting the Stone (多边形的重心)

    Lifting the Stone 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/G Description There are ...

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

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

  4. 多边形求重心 HDU1115

    http://acm.hdu.edu.cn/showproblem.php?pid=1115 引用博客:https://blog.csdn.net/ysc504/article/details/881 ...

  5. hdu1115【多边形求重心模板】

    1.质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心(∑( xi×mi ) / ∑mi, ∑( yi×mi ) / ∑mi) 2.质量分布均匀.这个题就是这一类型,算法和上面的不同. ...

  6. POJ1385 Lifting the Stone 多边形重心

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

  7. hdu 1115 Lifting the Stone

    题目链接:hdu 1115 计算几何求多边形的重心,弄清算法后就是裸题了,这儿有篇博客写得很不错的: 计算几何-多边形的重心 代码如下: #include<cstdio> #include ...

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

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

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

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

随机推荐

  1. html5拨打电话及发短信

    1.最常用WEB页面一键拨号的电话拨打功能 <a href="tel:15088888888">拨号</a> 2.最常用WEB页面一键发送短信功能: < ...

  2. [Swift通天遁地]二、表格表单-(17)制作在表单左侧添加单选和复选组件的表单行

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. 【BZOJ3110】[ZJOI2013]K大数查询(整体二分)

    题目: BZOJ3110 分析: 整体二分模板题-- 先明确一下题意:每个位置可以存放多个数,第一种操作是"加入 (insert) "一个数而不是"加上 (add) &q ...

  4. [转]linux tr命令详解

    转自:http://www.cnblogs.com/huangxingkezhan/archive/2013/01/23/2874031.html 通过使用 tr,您可以非常容易地实现 sed 的许多 ...

  5. Elasticsearch之CURL命令的mget查询

    我这里, 再,创建一个zhouls2的索引库. [hadoop@master elasticsearch-]$ curl -XPUT 'http://master:9200/zhouls2/' {]$ ...

  6. EasyUI系列学习(三)-Draggable(拖动)

    一.创建拖动组件 0.Draggable组件不依赖于其他组件 1.使用标签创建 <div class="easyui-draggable" id="box" ...

  7. SublimeText学习(一)-安装

    1.下载安装包:http://www.sublimetext.com/2 2.开始安装,一直下一步 3.开始汉化 汉化包下载:http://files.cnblogs.com/files/2star/ ...

  8. <stddef.h>

    Common definitions 定义类型: ptrdiff_t 两指针相减的结果,signed integer size_t sizeof操作符的结果,unsigned integer max_ ...

  9. 字符串String的理解

    1.String是一个final的类型 即不可被继承修改,一经生成不可改变.所以在代码中使用String s  = s1 + s2;的时候,执行完之后s所指向的是一个新生成的对象,这里有个地方值得注意 ...

  10. python gdal 矢量转栅格

    data = gdal.Open(templateTifFileName, gdalconst.GA_ReadOnly)geo_transform = data.GetGeoTransform()x_ ...