Scrambled Polygon--poj2007(极角排序模板)

http://poj.org/problem?id=2007

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<vector>
using namespace std;
#define INF 0xfffffff
#define ESP 1e-8
#define memset(a,b) memset(a,b,sizeof(a))
#define N 2100

struct Point
{
    double x,y;
    int index;
    Point(double x=0,double y=0):x(x),y(y) {}
    Point operator - (const Point &temp)const
    {
        return Point(x-temp.x,y-temp.y);
    }
    Point operator + (const Point &temp)const
    {
        return Point(x+temp.x,y+temp.y);
    }
    int operator ^(const Point &temp)const ///求叉积
    {
        double t=(x*temp.y)-(y*temp.x);
        if(t>ESP)
            return 1;
        if(fabs(t)<ESP)
            return 0;
        return -1;
    }
    double operator * (const Point &temp)const
    {
        return x*temp.x+y*temp.y;
    }
    bool operator == (const Point &temp)const
    {
        return (x==temp.x)&&(y==temp.y);
    }
} p[N];

double dist(Point a1,Point a2)
{
    return sqrt((a1-a2)*(a1-a2));
}
int pos=0;

int cmp(Point a1,Point a2)
{
    int t=(a1-p[pos])^(a2-p[pos]);
    if(t<0) return false;
    else
        return true;
}

int main()
{
    int n;
    n=0;
    while(scanf("%lf %lf",&p[n].x,&p[n].y)!=EOF) n++;
    pos=0;

    for(int i=1; i<n; i++)
    {
        sort(p+i,p+n,cmp);
        pos++;
    }
    for(int i=0; i<n; i++)
        printf("(%.0lf,%.0lf)
",p[i].x,p[i].y);
    printf("
");
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/5478082.html