POJ2007 凸包计算

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();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值