如何使用 javascript 方法在翻转的圆上找到 x 和 y 坐标

How to find x and y coordinates on a flipped circle using javascript methods

我试图在一个圆上找到 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。谢谢。

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

x = radius * Math.sin(Math.PI * 2 * angle / 360);

y = radius * Math.cos(Math.PI * 2 * angle / 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)

诀窍是将您的问题转化为您知道如何解决的问题。您可以通过从角度减去 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.999999999999996) 对于 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

应该是

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