码迷,mamicode.com
首页 > 编程语言 > 详细

极角排序

时间:2020-05-27 15:45:16      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:for   point   clu   operator   col   bool   bsp   坐标系   lse   

极角是极坐标系中的角度

我们对这个角度进行点的排序

利用类的概念设置结构体

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 101001;
const LL INF = 0x3f3f3f3f3f3f3f3f;

int n;  
class Point{
public:
    LL x, y;
    int xiang;    ///象限
public:
    Point(LL a = 0, LL b = 0)
    {
        x = a, y = b;
        if(x >= 0 && y >= 0)    xiang = 1;
        else if(x >= 0 && y <= 0)   xiang = 4;
        else if(x <= 0 && y >= 0)   xiang = 2;
        else    xiang = 3;
    }
    void judge(){//调整象限
        if(x >= 0 && y >= 0)    xiang = 1;
        else if(x >= 0 && y <= 0)   xiang = 4;
        else if(x <= 0 && y >= 0)   xiang = 2;
        else    xiang = 3;
    }
    Point operator-(Point a){
        return Point(x-a.x, y-a.y);
    }
    Point operator+(Point a){
        return Point(x+a.x, y+a.y);
    }
}p[N], np[N];///np 存储的是真正的极角

LL Cross(Point a, Point b){
    return a.x * b.y - a.y * b.x;
}

bool cmp(Point a, Point b){///真真正正的排序
    if(a.xiang == b.xiang)//再按照叉积
        return Cross(a, b) > 0;
    else    ///先按照象限
        return a.xiang < b.xiang;
}

bool cmp2(Point a, Point b){///只是按照叉积排序
    return Cross(a, b) > 0;
}

void Show(){
    cout<<"\n\n";
    for(int i = 0; i < n; i++){
        printf("%d  %lld %lld\n", p[i].xiang, p[i].x, p[i].y);
    }
}

int main(){
    cin>>n;
    for(int i = 0; i < n; i++){
        cin>>p[i].x>>p[i].y;    p[i].judge();
    }
    
    sort(p, p + n, cmp);    ///极点排序
    Show();
    
    return 0;
}

 

极角排序

标签:for   point   clu   operator   col   bool   bsp   坐标系   lse   

原文地址:https://www.cnblogs.com/lucky-light/p/12972856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!