仅在象限 I 和 IV 的圆周上均匀分布点

Distribute points evenly on circle circumference in quadrants I and IV only

我只想在象限 I 和 IV 的圆周上均匀分布 n 个点。

作为参数,我有点数 n、圆心坐标 cxcy 以及半径 r

我可以像使用下面的公式一样将点分布在整个圆周上,但我正在寻找仅在象限 I 和 IV 中分布点的公式

var n = 5;
var cx = 1;
var cy = 1;
var r = 2;

//I store each point's coordinates in this array below
var coordinates = [];

for (var i=0; i < n; i++) {
    //defining point's angle with the center of the circle in radiant
    //this angle distribute the points evenly over all 4 quadrants
    var angle = ((360/n) * i) * (Math.PI/180);

    //calculating the point's coordinates on the circle circumference
    var pointX = cx + r * Math.cos(angle);
    var pointY = cx + r * Math.sin(angle);

    //storing the point's coordinates
    coordinates.push([pointX, pointY]);
}

像这样?

var n = 5;
var r = 2;
var cx = 1;
var cy = 1;
var coordinates = [];

for(var i=0; i<n; ++i){
  var a = (i+.5) / n * Math.PI;
  coordinates.push([
    cx + Math.sin(a) * r,
    cy - Math.cos(a) * r
  ]);
}
var n = 5;
var cx = 1;
var cy = 1;
var r = 2;

//I store each point's coordinates in this array below
var coordinates = [];

for (var i=0; i < n; i++) {
    //defining the angle of the point with the center of the circle in radiant
    var angle = ((360/n) * i) * (Math.PI/180);

    //calculating the coordinates of the point on the circle circumference
    var pointX = cx + r * Math.cos(angle);
    var pointY = cx + r * Math.sin(angle);



    // Here, we are going to use a boolean expression to determine if 
    // [pointX, pointY] is within quadrant 1 or 4.
    // We can start with this boolean equation:
    // (pointX >= cx && pointY >= cy) || (pointX >= cx && pointY <= cy)
    // But this problem can be simplified to only pointX >= cx

    if(pointX >= cx){
        //storing the point's coordinates
        coordinates.push([pointX, pointY]);
    } 
}

以下是我将采取的解决此问题的步骤:

  1. 找到两者之间的角度。每个点 var incrementBy = 180 / n
  2. 开始角度为 270º,结束角度为 90º
  3. 遍历通过

代码

 var increment = 180 / n
 var startAngle = 270
 for (var i = 0; i < n; i++)
 {
     var angle = startAngle + increment * i
     var rads = angle * Math.pi / 180

     var tx = cx + r * Math.cos(rads)
     var ty = cy + r * Math.sin(rads)

     coords.push([tx, ty])
 }

注意

我没有费心去转换传统的象限(相对于 JS 的 y 轴向下移动)。如果需要,那么在计算之后,只需反转 ty 值即可。当你递增回到 Quad I 时,当角度值超过 360º 时,我也没有费心去减小角度值。

这是平均分配对象的方法

var boxes = []
let count = 10;
for (let i = 0; i < count; i++) {
let size = 0.8;
let radius = 3;
let box = new THREE.Mesh(new THREE.BoxGeometry( size, size, size ),new THREE.MeshPhysicalMaterial(0x333333))
let gap = 0.5;
let angle = i * ((Math.PI * 2) / count);
let x = radius * Math.cos(angle);
let y = 0;
let z = radius * Math.sin(angle);
box.position.set(x,y,z);
boxes.push(box);`enter code here`
scene.add(box)
}

here is how it looks for 10 blocks

here is how it looks for 5 blocks