我想要实现的是存储构成圆的圆周的所有点的坐标。我知道这实际上是不可能的,因为圆的圆周上有无限个点,但假设我想存储圆的圆周上每 1 厘米之后找到的坐标。圆的周长是 50 厘米,所以实际上我将在数组中存储 25 个值。
这是我迄今为止尝试过的:
for(var degree=0;degree<360;degree++){
var radians = degree * Math.PI/180;
var x = center + radius * Math.cos(radians);
var y = center + radius * Math.sin(radians);
//x & y are the coordinates of points on the circumference
}
但是我上面的代码没有按预期工作。我们将非常感谢您的帮助!
最佳答案
为此,您需要按适当的步长大小(而不是递增它)来步进 Angular 。因此,让我们通过以下方式定义我们的圈子:
x0,y0
- 圆心
r
- 半径
d
- 点的近似距离
现在我们可以利用圆的周长:
n = ceil (2.0*M_PI*r / d); // integer number of points (rounded up)
da = 2.0*M_PI/n; // floating angular step between points
for (a=0.0,i=0;i<n;i++,a+=da)
{
x = x0 + r*cos(a);
y = y0 + r*sin(a);
// here x,y is your point
}
抱歉,我不使用 javascript 编写代码,因此代码是使用 C++
编写的,因此只需更改语法以匹配您的语法即可...
但是,如果您想要所有光栅化像素,还有其他方法可以做到这一点(Bresenham、midpoint 等...),有些甚至不需要 +,-
之外的其他操作,请参见:
关于javascript - 求圆圆周上各点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61295473/