题意:给你多个点,让你将其逆时针输出。
思路:直接极角排序输出即可。
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
struct node
{
int x,y;
};
node point[1000];
int cross(node p0,node p1,node p2) //计算叉积 p0p1 X p0p2
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(node p1,node p2) //计算 p1p2的 距离
{
return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(node p1,node p2)//进行极角排序,若极角相同按距离排列
{
node origon;
origon.x=0,origon.y=0;
int tmp=cross(origon,p1,p2);
if(tmp>0) return true;
//else if(tmp==0) return true;
else if(tmp==0&&dis(origon,p1)<dis(origon,p2)) return true;
else return false;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int count=0,i;
while(scanf("%d%d",&point[count].x,&point[count].y)!=EOF)
count++;
sort(point,point+count,cmp);
for(i=0;i<count;i++)
printf("(%d,%d)\n",point[i].x,point[i].y);
return 0;
}