计算直线相对于中心坐标的斜率的算法

发布于 2024-09-30 13:52:24 字数 686 浏览 0 评论 0原文

请帮忙计算坡度的算法 所以我们有一个笛卡尔坐标系。 X 就在 Y 顶部。有一条线穿过坐标中心。 需要确定相对于轴线 OX 的角度。

所以这就是我正在做的

  1. 某些函数转移到原点(顶行)和行尾
  2. 确定dx,dy
  3. Hildren在atan2中释放两个参数(dy,dx)
  4. 以弧度返回结果。

但!我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

  1. Certain functions transferred to the origin (top line) and end of line
  2. Determine dx, dy
  3. Hildren releases two parameters in atan2 (dy, dx)
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-10-07 13:52:24

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.

美胚控场 2024-10-07 13:52:24

计算 0 到 360 度角度的公式:

f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign( x))*符号(y)

    -180/pi()*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))

    x=x2-x1 and y=y2-y1 .

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)

    -180/pi()*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))

    x=x2-x1 and y=y2-y1 .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文