极角排序

来源:互联网 发布:测试手机信号强度软件 编辑:程序博客网 时间:2024/05/21 13:14

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
struct Point
{
double x,y;
}p[100];

double crossDet(Point p1,Point p2,Point p3)
{
      return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
bool cmp2(const Point &a, const Point &b)
{

    if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x;

    if (a.y == 0 && a.x >= 0 && b.y != 0)return true;

    if (b.y == 0 && b.x >= 0 && a.y != 0)return false;

    if (b.y*a.y <= 0)return a.y>b.y;

    Point one;

    one.y = one.x = 0;

    return crossDet(one,a,b) > 0 || (crossDet(one,a,b) == 0 && a.y > b.y);

}
int main()
{
    int n;
   while(~scanf("%d",&n))
   {
      for(int i=0;i<n;i++)
      {
     scanf("%lf%lf",&p[i].x,&p[i].y);

      }
      sort(p,p+n,cmp2);
      for(int i=0;i<n;i++)
      printf("%lf %lf\n",p[i].x,p[i].y);
   }

}

0 0
原创粉丝点击