【问题标题】:How to find x and y coordinates on a flipped circle using javascript methods如何使用 javascript 方法在翻转的圆上找到 x 和 y 坐标
【发布时间】:2017-04-26 18:22:55
【问题描述】:

我试图找到一个圆上的 X、Y 点,其中 0 度从圆的顶部开始并顺时针移动。通常,要在已知半径和角度的圆上找到 x、y 坐标,您可以简单地使用公式 x = r(cos(degrees‎°)), y = r(sin(degrees‎°))。圆看起来像这样,度数将从 0‎° 逆时针扩展。

但是,我使用的是一个圆,其中 0‎° 从顶部开始,随着绕圆顺时针移动,度数会扩大。假设 var r = 60;var degrees = 130; 我可以使用什么公式(或 javascript 方法)来确定 X、Y 值。注意:我可以假设原点为 0、60,因为 r = 60。谢谢。

【问题讨论】:

    标签: javascript geometry trigonometry angle


    【解决方案1】:

    由于整个圆有 2 个辐射点,因此您可以使用以下公式计算圆的点坐标:

    x = 半径 * Math.sin(Math.PI * 2 * 角度 / 360);

    y = 半径 * Math.cos(Math.PI * 2 * 角度 / 360);

    var radius = 60;
    var angle  = 140;
    var x = radius * Math.sin(Math.PI * 2 * angle / 360);
    var y = radius * Math.cos(Math.PI * 2 * angle / 360);
    console.log('Points coors are  x='+ 
       Math.round(x * 100) / 100 +', y=' +
       Math.round(y * 100) / 100)

    【讨论】:

      【解决方案2】:

      诀窍是将您的问题转化为您知道如何解决的问题。您可以通过从角度减去 90 度并否定 y 来做到这一点,即 x=r cos(theta-90) 和 y = -r sin(theta-90)。在 JavaScript 中:

      function circleXY(r, theta) {
        // Convert angle to radians
        theta = (theta-90) * Math.PI/180;
      
        return {x: r*Math.cos(theta),
                y: -r*Math.sin(theta)}
      }
      
      for (var theta=0; theta<=360; theta += 30) {
        var answer = circleXY(60, theta);
        console.log('(x, y) = ' + '(' + answer.x + ', ' + answer.y + ') for theta=' + theta);
      }

      产生以下结果:

      (x, y) = (3.67394039744206e-15, 60) 对于 theta=0

      (x, y) = (30.000000000000007, 51.96152422706631) 对于 theta=30

      (x, y) = (51.96152422706632, 29.9999999999999996) 对于 theta=60

      (x, y) = (60, 0) 对于 theta=90

      (x, y) = (51.96152422706632, -29.999999999999996) 对于 theta=120

      (x, y) = (30.000000000000007, -51.96152422706631) 对于 theta=150

      (x, y) = (3.67394039744206e-15, -60) 对于 theta=180

      (x, y) = (-29.999999999999986, -51.96152422706632) 对于 theta=210

      (x, y) = (-51.96152422706632, -29.999999999999996) 对于 theta=240

      (x, y) = (-60, -7.34788079488412e-15) 对于 theta=270

      (x, y) = (-51.96152422706631, 30.000000000000007) 对于 theta=300

      (x, y) = (-30.00000000000003, 51.961524227066306) 对于 theta=330

      (x, y) = (-1.1021821192326178e-14, 60) 对于 theta=360

      【讨论】:

        【解决方案3】:

        应该是

        x = Math.cos(Math.PI * 2 * 角度/360); 和 y = Math.sin(Math.PI * 2 * 角度/360);

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 2021-10-05
        • 1970-01-01
        • 2012-02-09
        • 2015-12-31
        • 2022-12-10
        相关资源
        最近更新 更多