代码
/**
* 输入 圆心x,y、 半径、均匀分布的数量
* 返回一组坐标
*/
function calculatePointsOnCircle(x, y, r, n) {
const points = [];
const theta = (2 * Math.PI) / n;
for (let i = 0; i < n; i++) {
const cx = r * Math.cos(i * theta);
const cy = r * Math.sin(i * theta);
const pointX = x + cx;
const pointY = y + cy;
points.push([pointX, pointY]);
}
return points;
}
思路
这个函数接收四个参数,分别代表圆心坐标 (x, y),半径 r 和圆边缘点的数量 n。它会返回一个数组,其中包含 n 个点的 [x, y] 坐标值。
该算法的实现是通过将圆等分为 n 份,在极坐标系中计算点的坐标并转换为直角坐标系。