极角排序
Posted lucky-light
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了极角排序相关的知识,希望对你有一定的参考价值。
极角是极坐标系中的角度
我们对这个角度进行点的排序
利用类的概念设置结构体
#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<<" "; for(int i = 0; i < n; i++){ printf("%d %lld %lld ", 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; }
以上是关于极角排序的主要内容,如果未能解决你的问题,请参考以下文章