POJ 1385 多边形的重心

来源:互联网 发布:夜访吸血鬼 结局 知乎 编辑:程序博客网 时间:2024/04/27 22:28

C - Lifting the Stone
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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

245 00 5-5 00 -541 111 111 111 11

Sample Output

0.00 0.006.00 6.00

这题还是挺容易的,就是求出多边形的重心,计算几何中127,128有详细解释,里面也有模板,不过觉得奎神的模板意思比较明白,所以就在奎神的基础上加了从PDF上的127页上加了两个 函数进去,以补充多边形所有的函数需要。PDF中的cmp判断数为正负的,奎神里面的sgn对应这个cmp函数;还有一个就是判断重心的时候,PDF上交的模板先判断面积为不为0的情况,为0就直接返回初始化的点了。

#include <iostream>#include <cstdio>#include <vector>#include <cmath>#include <algorithm>#define MAX 111116#define eps 1e-7using namespace std;int sgn(const double &x){ return x < -eps? -1 : (x > eps);}inline double sqr(const double &x){ return x * x;}inline int gcd(int a, int b){ return !b? a: gcd(b, a % b);}struct Point{    double x, y;    Point(){}    Point(const double &x, const double &y):x(x), y(y){}    Point operator -(const Point &a)const{ return Point(x - a.x, y - a.y); }    Point operator +(const Point &a)const{ return Point(x + a.x, y + a.y); }    Point operator * (const double &a)const{ return Point(x * a, y * a); }    Point operator / (const double &a)const{ return Point(x / a, y / a); }    friend double det(const Point &a, const Point &b){ return a.x * b.y - a.y * b.x;}    friend double dot(const Point &a, const Point &b){ return a.x * b.x + a.y * b.y;}    friend double dist(const Point &a, const Point &b){ return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}    void in(){ scanf("%lf %lf", &x, &y); }    void out(){ printf("%.2f %.2f\n", x, y); }};struct Line{    Point s, t;    Line() {}    Line(const Point &s, const Point &t):s(s), t(t) {}    void in() { s.in(),t.in(); }    double pointDistLine(const Point &p)    {        if(sgn(dot(t - s, p - s)) < 0)return dist(p, s);        if(sgn(dot( s - t, p - t)) < 0)return dist(p, t);        return fabs(det(t - s, p - s)) / dist(s, t);    }    bool pointOnLine(const Point &p)    {        return sgn(det(t - s, p - s)) == 0 && sgn(dot(s - p, t - p)) <= 0;    }};struct Poly //多边形类{    vector<Point>a;    void in(const int &r)    {        a.resize(r);        for(int i = 0; i < r; i++) a[i].in();    }    //计算多边形的周长    double perimeter()    {        double sum=0;        int n=a.size();        for(int i=0;i<n;i++) sum+=dist(a[i],a[(i+1)%n]);        return sum;    }    //计算多边形的面积    double getDArea()    {        int n = a.size();        double ans = 0;        for(int i = 0; i < n; i++) ans += det(a[i], a[(i + 1)%n]);        return ans / 2;    }    //计算多边形的重心坐标    Point getMassCenter()    {        Point center(0, 0);        if(sgn(getDArea())==0) return center; //面积为0情况,当然这题说了面积不可能为0可不写        int n = a.size();        for(int i = 0; i < n; i++)            center =center + (a[i] + a[(i + 1) % n]) * det(a[i], a[(i + 1) % n]);        return center / getDArea() / 6;    }    //计算点t是否在多边形内,返回0指在外,1指在内,2指在边界上    int pointOnline(Point t)    {        int num=0,i,d1,d2,k,n=a.size();        for(i=0;i<n;i++)        {            Line line=Line(a[i],a[(i+1)%n]);            if(line.pointOnLine(t)) return 2;            k=sgn(det(a[(i+1)%n]-a[i],t-a[i]));            d1=sgn(a[i].y-t.y);            d2=sgn(a[(i+1)%n].y-t.y);            if(k>0&&d1<=0&&d2>0) num++;            if(k<0&&d2<=0&&d1>0) num--;        }        return num!=0;    }}poly;int main(){    int T;    cin>>T;    while(T--)    {        int n;        cin>>n;        poly.in(n);        poly.getMassCenter().out();    }    return 0;}


0 0
原创粉丝点击