POJ2007 凸包计算

来源:互联网 发布:乐乎pt上不去 编辑:程序博客网 时间:2024/06/06 20:05

Scrambled Polygon

和POJ1696蚂蚁那道题几乎一个意思……看看图就懂了。不多说思路了,直接排序就好,O(N^2);


CODE:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))using namespace std;const double EPS = 1e-8;inline int sign(double a){    return a < -EPS ? -1 : a > EPS;}struct Point{    double x, y;    Point()    {    }    Point(double _x, double _y) :        x(_x), y(_y)    {    }    Point operator+(const Point&p) const    {        return Point(x + p.x, y + p.y);    }    Point operator-(const Point&p) const    {        return Point(x - p.x, y - p.y);    }    Point operator*(double d) const    {        return Point(x * d, y * d);    }    Point operator/(double d) const    {        return Point(x / d, y / d);    }    bool operator<(const Point&p) const    {        int c = sign(x - p.x);        if (c)            return c == -1;        return sign(y - p.y) == -1;    }    double dot(const Point&p) const    {        return x * p.x + y * p.y;    }    double det(const Point&p) const    {        return x * p.y - y * p.x;    }    double alpha() const    {        return atan2(y, x);    }    double distTo(const Point&p) const    {        double dx = x - p.x, dy = y - p.y;        return hypot(dx, dy);    }    double alphaTo(const Point&p) const    {        double dx = x - p.x, dy = y - p.y;        return atan2(dy, dx);    }    void read()    {        scanf("%lf%lf", &x, &y);    }    double abs()    {        return hypot(x, y);    }    double abs2()    {        return x * x + y * y;    }    void write()    {        cout << "(" << x << "," << y << ")" << endl;    }};Point temp;bool cmp(Point a,Point b){    return a.y<b.y;}bool cmp_angel(Point a,Point b){    int t=crossOp(temp,a,b);    if(t<0)    {  //说明a的再b的顺时针方向上,说明极角小        return false ;    }    else if(t>0)    {        return true;    }    else    {        int da=temp.distTo(a);        int db=temp.distTo(b);        if(da<=db)        {            return false ;        }        else        {            return true;        }    }}Point p[52];int main(){    int n,m,i=0;    while(cin>>n>>m)    {        p[i].x=n;        p[i].y=m;        i++;    }    int l=i;    temp=p[0];    temp.write();    for(int j=1; j<l; j++)    {        sort(p+j,p+l,cmp_angel);        temp=p[j];        temp.write();    }}



原创粉丝点击