极角排序
这里的极角就是我们高数上所学习的极角坐标系中的极角(先来复习一下),极角坐标系中还包括极轴和极径,如下图:
其中射线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);
}
}