问题描述
计算直线和x轴之间的角度最快的方法是什么?
What is the fastest way to calculate angle between a line and the x-axis?
我需要定义一个函数,该函数在PI:2PI间隔为内射(我必须在最高点和下方任何点之间成角度).
I need to define a function, which is Injective at the PI:2PI interval (I have to angle between point which is the uppermost point and any point below).
PointType * top = UPPERMOST_POINT;
PointType * targ = TARGET_POINT;
double targetVectorX = targ->x - top->x;
double targetVectorY = targ->y - top->y;
第一次尝试
//#1
double magnitudeTarVec = sqrt(targetVectorX*targetVectorX + targetVectorY*targetVectorY);
angle = tarX / magTar;
第二次尝试
//#2 slower
angle = atan2(targetVectorY, targetVectorX);
我不需要直接的角度(弧度或度数),只要从2个点比较这些值,我就能分辨出哪个角度更大. (例如,示例一中的角度在-1和1之间(它是余弦参数))
I do not need the angle directly (radians or degrees), just any value is fine as far as by comparing these values of this kind from 2 points I can tell which angle is bigger. (for example angle in example one is between -1 and 1 (it is cosine argument))
推荐答案
像y2一样检查y为零.那么x/y的商就可以了. (假设我正确理解了您).
Check for y being zero as atan2 does; then The quotient x/y will be plenty fine. (assuming I understand you correctly).
这篇关于用x轴计算角度的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!