【问题标题】:Using atan2 with offset circle使用带有偏移圆的 atan2
【发布时间】:2021-02-24 14:04:13
【问题描述】:

我正在尝试计算弧长,以便在给定弧长的情况下为圆形边框着色。

用户可以点击圆的边缘,代码应该会自动计算从位置y=radiusx=cx开始的弧长。给定函数atan2(),我无法成功实现这一点,因为如果我理解正确,atan2 返回正 X 轴与从我的圆心到单击点的射线之间的角度。但我需要点击的点和 y 轴之间的角度。

我附上图片,以防它更有意义:

我目前拥有的是:

// Calcualte the deltas for point X and Y which are required for atan2 function
let deltaX = event.pageX - this.centerX
let deltaY = event.pageY - this.centerY

// The Math.atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to the point (x,y), for Math.atan2(y,x)
let angleRadian = Math.atan2(deltaY, deltaX)
// Apply formula to calculate Arc length
let arcLength = radius * angleRadian

我知道 atan2 如上所述计算角度,但我不知道如何修改我的代码以实现我需要它做的事情 - 即获取点单击和 y 轴之间的角度.

【问题讨论】:

    标签: javascript geometry trigonometry


    【解决方案1】:

    只需交换 atan2 参数即可从 OY 轴获取角度。 对于逆时针 (CCW) 系统:

    let angleRadian = Math.atan2(deltaX, deltaY)
    

    (基于公式cos(a)=sin(Pi/2-a), sin(a)=cos(Pi/2-a)

    对于顺时针 (CW) 系统,其中 OY 轴方向向下:

    let angleRadian = Math.atan2(deltaX, -deltaY)
    

    【讨论】:

    • 我已经尝试过了,但它不起作用。我相信我还应该处理一些deltaYdeltaX 是负值的情况,但我不确定如何处理。
    • 绝对有效。但是您的 OY 轴方向是什么 - 向上还是向下?也许你纠结于坐标系。在顺时针系统中,您还必须更改一个参数的符号。处理负偏移量取决于您的目的 - 也许您可以使用结果的绝对值
    • 如果您在我的问题中查看我的图片,方向是顺时针方向。因此,当用户点击圆时,应该根据 y = 半径和用户按顺时针方向点击的点计算圆弧。
    • 我看到 cx=200, cy=200 表示小圆圈,所以决定你的意思是 CCW 系统。 CW 系统使用atan2(deltaX, -deltaY)
    • 哦,抱歉,我只是使用了我在网上看到的“命名”来表示这些方程式。对于 cx 和 cy,我的意思是我的圈子的“中心”,不一定在 0,0 点。
    猜你喜欢
    • 2021-10-04
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多