思路:
先找左下点,然后排序找其右下第一个点,重复即可
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
int pos;
int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
struct Point
{
double x,y;
int index_;
}node[60];
Point operator - (Point a, Point b)
{
Point c;
c.x= a.x - b.x;
c.y= a.y - b.y;
return c;
}
double operator ^ (Point a,Point b)
{
return a.x *b.y - a.y * b.x;
}
double operator *(Point a, Point b)
{
return a.x*b.x + a.y*b.y;
}
double dist(Point a, Point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
double t = (a-node[pos])^(b-node[pos]);
if(sgn(t) == 0)
{
return dist(node[pos],a) < dist(node[pos],b);
}
if(sgn(t) < 0) return false;
else return true;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i = 0 ; i < n; i++)
{
cin>>node[i].index_>>node[i].x>>node[i].y;
if(node[i].y < node[0].y ||(node[i].y == node[0].y && node[i].x < node[0].x))
swap(node[i],node[0]);
}
pos = 0;
for(int i= 1; i < n; i++)
{
sort(node + i, node + n,cmp);
pos++;
}
cout<<n;
for(int i = 0;i < n; i++)
{
cout<<' '<<node[i].index_;
}
cout<<endl;
}
}