Triangle Partition HDU - 6300 极角排序

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    int index;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

int pos=0;
Point p[4000];
bool cmp(Point a,Point b)
{
    double tmp = (a-p[pos])^(b-p[pos]);
    if(sgn(tmp) == 0)
        return dist(p[pos],a) < dist(p[pos],b);
    else if(sgn(tmp) < 0)return false;
    else return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    int n;
    while(T--)
    {
        scanf("%d",&n);
        double x,y;
        for(int i = 0;i < 3*n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            p[i].index=i+1;
            if( p[i].y < p[0].y || (p[i].y == p[0].y && p[i].x < p[0].x) )
                swap(p[0],p[i]);
        }
        sort(p+1,p+3*n,cmp);
        int k=0;
        for(int i=0;i<n;i++)
        {
        	for(int j=k;j<k+3;j++)
        	{
        		if(j==k)
	        		printf("%d",p[j].index);
	        	else
	        		printf(" %d",p[j].index);
	        }
	        k=k+3;
	        printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/81172695