Codeforces 598C. Nearest vectors【高精度几何】

C. Nearest vectors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

Examples
input
4
-1 0
0 -1
1 0
1 1
output
3 4
input
6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6
output
6 5



/*
    题意:有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°


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值