极角排序

极角排序

这里的极角就是我们高数上所学习的极角坐标系中的极角(先来复习一下),极角坐标系中还包括极轴和极径,如下图:
在这里插入图片描述
其中射线OX叫做极轴,规定正方向(一般为逆时针方向)。点O于空间中一点M连接,所称角θ为极角,这条长度为ρ的线段叫做极轴,这两者组成坐标(ρ,θ)为点M在此坐标系下的极坐标。
极角排序就是根据极坐标对点进行排序的操作

极角排序的常用的三种方法

1.atan2()函数

atan2(double y,double x) 其中y代表已知点的Y坐标,同理x ,返回值是此点与远点连线与x轴正方向的夹角

bool cmp1(point a,point b)
{
    if(atan2(a.y,a.x)!=atan2(b.y,b.x))
        return atan2(a.y,a.x)<atan2(b.y,b.x);
    else return a.x<b.x;
}

2.叉乘

struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
}p[maxn];
typedef Point Vector;
double Cross(Vector v1,Vector v2){
    return v1.x*v2.y - v1.y*v2.x;
}
bool cmp(Point p1,Point p2){
	//这里Cross里的参数应该是这两个点与极点所成的向量
	//我这里默认是(0,0)
    if(Cross(p1, p2)>0) return true;
    return false;
}

3.象限

int Quadrant(point a)  //象限排序,注意包含四个坐标轴
{
    if(a.x>0&&a.y>=0)  return 1;
    if(a.x<=0&&a.y>0)  return 2;
    if(a.x<0&&a.y<=0)  return 3;
    if(a.x>=0&&a.y<0)  return 4;
}
bool cmp3(point a,point b)  //先按象限从小到大排序 再按极角从小到大排序
{
    if(Quadrant(a)==Quadrant(b))//返回值就是象限
        return cmp1(a,b);
    else Quadrant(a)<Quadrant(b);
}

两道水题

HRBUST - 1305 裸的极角排序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define FRER() freopen("in.txt","r",stdin)
#define FREW() freopen("out.txt","w",stdout)
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define debug cout<<"****************"<<endl
#define lowbit(x) x&(-x)
#define eps 1e-6
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 1000 + 7;
bool dcmp(double a,double b){
    if(fabs(a-b)<eps) return true;
    return false;
}
struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
}p[maxn];
typedef Point Vector;
double Cross(Vector v1,Vector v2){
    return v1.x*v2.y - v1.y*v2.x;
}
bool cmp(Point p1,Point p2){
    if(Cross(p1, p2)>0) return true;
    return false;
}
int main(){
    //FRER();
    //FREW();
    int n;
    while(cin>>n&&n){
        for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p+1, p+n,cmp);
        for(int i=0;i<n;i++) printf("(%g,%g)\n",p[i].x,p[i].y);
    }
}

HDU - 3532 略带思维

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define FRER() freopen("in.txt","r",stdin)
#define FREW() freopen("out.txt","w",stdout)
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define debug cout<<"****************"<<endl
#define lowbit(x) x&(-x)
#define eps 1e-6
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 1000 + 7;
const double PI = acos(-1);
bool dcmp(double a,double b){
    if(fabs(a-b)<eps) return true;
    return false;
}
struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
}p[maxn];
double angle[maxn];
int main(){
    //FRER();
    //FREW();
    int n;
    while(cin>>n&&~n){
        for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        int tot = 0;
        double ans = -99999;
        double res;
        for(int i=0;i<n;i++){
            tot = 0;
            res = 99999;
            memset(angle, 0, sizeof(angle));
            for(int j=0;j<n;j++){
                if(i==j) continue;
                angle[tot] = atan2(p[j].y-p[i].y, p[j].x-p[i].x);
                tot++;
            }
            sort(angle, angle+tot);
            for(int j=0;j<tot-1;j++){
                double _res = angle[j+1]-angle[j];
                res = min(res,_res);
            }
            //cout<<res/2/PI*360<<endl;
            ans = max(ans,res);
        }
        printf("%.4f\n",ans/2/PI*360);
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值