极角排序

    xiaoxiao2025-01-19  13

    #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);    }

    }

    转载请注明原文地址: https://ju.6miu.com/read-1295633.html
    最新回复(0)