/*
题意:有n个点,每个点表示原点到该点的向量,让你求出两个向量最小的夹角,输出向量的序号
类型:几何
分析:该题需要用到高精度计算角度的方法.用atan2(y,x)能够求出每个点与x轴正向的夹角,进行排序,
在从小到大枚举角度,注意最后一个角度(最大角)和第一个角度(最小角)的角度差可能是负值,要加上2*PI
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
const long double PI = acos(-1.0);
vector<pair<long double,int> > vec;
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
long double x,y;
cin>>x>>y;
pair<long double,int> angle;
angle.first = atan2(y,x);
angle.second = i+1;
vec.push_back(angle);
}
sort(vec.begin(),vec.end());
long double ans_angle = 2*PI;
int ans1 = 0,ans2 = 0;
for(int i=0;i<n;i++){
long double tmp = (vec[(i+1)%n].first-vec[i].first);
if(tmp<0)tmp+=2*PI;
if(tmp<ans_angle){
ans_angle = tmp;
ans1 = vec[i].second;
ans2 = vec[(i+1)%n].second;
}
}
printf("%d %d\n",ans1,ans2);
}
在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x) 他们返回的值是弧度 要转化为角度再自己处理下。
前者接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。
第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了
例如:
例1:斜率是1的直线的夹角
cout<<atan(1.0)*180/PI;//45°
cout<<atan2(1.0,1.0)*180/PI;//45° 第一象限
cout<<atan2(-1.0,-1.0)*180/PI;//-135°第三象限
后两个斜率都是1 但是atan只能求出一个45°
例2:斜率是-1的直线的角度
cout<<atan(-1.0)*180/PI;//-45°
cout<<atan2(-1.0,1.0)*180/PI;//-45° y为负 在第四象限
cout<<atan2(1.0,-1.0)*180/PI;//135° x为负 在第二象限
常用的不是求过原点的直线的夹角 往往是求一个线段的夹角 这对于atan2就更是如鱼得水了
例如求A(1.0,1.0) B(3.0,3.0)这个线段AB与x轴正方向的夹角
用atan2表示为 atan2(y2-y1,x2-x1) 即 atan2(3.0-1.0,3.0-1.0)
它的原理就相当于把A点平移到原点B点相应变成B'(x2-x1,y2-y1)点 这样就又回到先前了
例三:
A(0.0,5.0) B(5.0,10.0)
线段AB的夹角为
cout<<atan2(5.0,5.0)*180/PI;//45°