计算直线相对于中心坐标的斜率的算法
请帮忙计算坡度的算法 所以我们有一个笛卡尔坐标系。 X 就在 Y 顶部。有一条线穿过坐标中心。 需要确定相对于轴线 OX 的角度。
所以这就是我正在做的
- 某些函数转移到原点(顶行)和行尾
- 确定dx,dy
- Hildren在atan2中释放两个参数(dy,dx)
- 以弧度返回结果。
但!我atan2只能在180度范围内工作。 180后就往另一个方向走。
那么问题是:求角度的正确算法是什么?我需要获取 dy、dx 值的大小吗?如何计算所有 360 度及以上的反正切?我很高兴听到具体的算法或代码注释。 谢谢!
static inline CGFloat angleBetweenLinesInRadians2 (CGPoint line1Start, CGPoint line1End)
{
CGFloat dx = 0, dy = 0;
dx = line1End.x - line1Start.x; / / whether to do fabs (line1End.x - line1Start.x);
dy = line1End.y - line1Start.y;
CGFloat rads = atan2 (dy, dx); / / whether to do fabs (rads)
return rads;
}
Please help with the algorithm of calculating the slope
So we have a Cartesian coordinate system. X right at Y the top. There is a line which passes through the center of coordinates.
Needed to determine the angle relatively to the axis OX.
So here's what I'm doing
- Certain functions transferred to the origin (top line) and end of line
- Determine dx, dy
- Hildren releases two parameters in atan2 (dy, dx)
- Returns the result in radians.
But! I atan2 works only within 180 degrees. After 180 goes in another direction.
So the question: what is the correct algorithm for finding the angle? Do I need to take dy, dx values in magnitude? How to make the arctangent calculated for all 360 and more? I would be glad to hear specific algorithms, or pieces of code comments.
Thanx!
static inline CGFloat angleBetweenLinesInRadians2 (CGPoint line1Start, CGPoint line1End)
{
CGFloat dx = 0, dy = 0;
dx = line1End.x - line1Start.x; / / whether to do fabs (line1End.x - line1Start.x);
dy = line1End.y - line1Start.y;
CGFloat rads = atan2 (dy, dx); / / whether to do fabs (rads)
return rads;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
atan2() 应该返回区间 [-pi,pi] (即 [-180, 180] )内的值,并使用 x 和 y 的符号来找出象限。 (C++ ref)
所以从技术上讲,你有 360 度。
atan2() is supposed to return a value in the interval [-pi,pi] (i.e. [-180, 180] ), and works with the signs of x and y to figure out the quadrant. (C++ ref)
So technically, you have 360 degrees.
计算 0 到 360 度角度的公式:
f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign( x))*符号(y)
A formula to calculate an angle from 0 to 360 degrees :
f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)