链接
题意:
给出凸包上的一些点,现在需要你按逆时针排序输出,并且输出的第一个点是(0,0)
解法:
极角排序
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x)*(x))
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=100 ;
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {};
bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}
bool operator<(const Point& b)const
{
return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
}
};
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double dis(Point A,Point B)
{
return sqrt(sqr(A.x-B.x)+sqr(A.y-B.y));
}
Point p[maxn+10];int n;
bool cmp(const Point &a, const Point &b)//逆时针排序
{
int ret=dcmp(Cross(a-p[0],b-p[0]));
if(ret) return ret>0;
return dis(a,p[0])<dis(b,p[0]);
}
int main()
{
std::ios::sync_with_stdio(false);
double x,y;n=0;
while(cin>>x>>y)
{
p[n++]=Point(x,y);
}
sort(p+1,p+n,cmp);
for0(i,n) printf("(%.0f,%.0f)\n",p[i].x,p[i].y);
return 0;
}