最快的方法来计算角度与x轴
fastest way to compute angle with x-axis
计算直线与x轴夹角的最快方法是什么?
我需要定义一个函数,它在PI:2PI区间内内射(我必须在最上面的点和下面的任何点之间夹角)
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);
我不需要直接的角度(弧度或度),只要从两个点比较这些值,我就可以知道哪个角度更大。(例如例1中的角度介于-1和1之间(它是余弦参数))
检查y是否像atan2一样为零;那么商x/y就很好了。(假设我没理解错)。
我刚刚写了最快的方法来排序向量的角度没有实际计算角度的一般问题,寻找函数单调的角度,没有任何代码或连接到c++或类似的。根据目前接受的答案,我现在建议
double angle = copysign( // magnitude of first argument with sign of second
1. - targetVectorX/(fabs(targetVectorX) + fabs(targetVectorY)),
targetVectorY);
与目前接受的答案相比,最大的好处是您不必担心无限值,因为所有非零向量(即targetVectorX
和targetVectorY
不同时等于零)将导致有限的伪角值。对于实角[−π…π],得到的伪角将在[−2…2]范围内,因此符号和不连续就像你从atan2
中得到的那样。
相关文章:
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 有没有一种惯用的方法可以在不存储变换或不必要地重新计算的情况下找到数组变换的最小/最大值?
- 寻求提高Microsoft密封库计算效率的方法
- 在 C++20 计算范围内相邻对的最简洁明了的方法是什么?
- 计算浮点余数的最佳方法
- 计算将整数n分解为4平方和的方法的数量
- 计算两个uint8_t变量之间差值的最快方法是什么?
- 使用 MC 方法计算积分
- 计算 <Classtype*> 向量中所有项的布尔值的最有效方法,如果全部为真则返回 true
- C++ std::string 中是否有任何函数可以计算两个字符串的相同起始字符的总数或任何最佳方法
- C/C++ 方法中最快/最短计算二进制数字总和/又名二进制中的 1 数
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- 什么是计算C中PI的最有效方法
- 是否有一种计算CGAL中Surface_mesh对象的PCA的方法
- 是否有任何数据类型或方法可以计算当前单元格中先前数组单元格的总和
- 有没有一种简单的方法来计算库中记录的函数
- 复数程序编译时的计算方法错误
- 数据包CRC计算方法
- C++中错误的计算方法
- 编译器本身使用并行计算方法运行