三个js从极坐标计算球体

Three js calculate sphere from polar coordinates

我正在使用 three.js 想要计算一个球体。我使用两个 for 循环,一个用于 theta,一个用于 phi,然后将极坐标转换为笛卡尔坐标。对于每个计算出的点,我都添加了一个点。结果不是球体。

这是嵌套的 for 循环:

const distance = 1000;
for (var i = 0; i < 360; i += 10) {
  for (let j = 0; j < 360; j += 10) {
    let theta = i * (Math.PI / 180);
    let phi = j * (Math.PI / 180);
    let x = Math.sin(theta) * Math.cos(phi) * distance;
    let y = Math.sin(theta) * Math.sin(phi) * distance;
    let z = distance * Math.sin(phi);
    const lightSphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    lightSphere.position.set(x, y, z);
    scn.add(lightSphere);
  }
}

这里是完整的代码,包括结果:Link

j 必须在 [-90, 90] 9 范围内,而不是 [0, 260]。请注意,您正在创建从南极到北极 (180°) 的切片 (360°)。

您必须计算 sin(theta)cos(theta) 并将两者与 cos(phi) 相乘:

let x = Math.sin(theta) * Math.cos(phi) * distance;
let y = Math.sin(theta) * Math.sin(phi) * distance;

let x = Math.cos(theta) * Math.cos(phi) * distance;
let y = Math.sin(theta) * Math.cos(phi) * distance;

完整算法:

const distance = 1000;
for (var i = 0; i < 360; i += 10) {
    for (let j = -90; j < 90; j += 10) {
        let theta = i * (Math.PI / 180);
        let phi = j * (Math.PI / 180);
        let x = Math.cos(theta) * Math.cos(phi) * distance;
        let y = Math.sin(theta) * Math.cos(phi) * distance;
        let z = distance * Math.sin(phi);
        const lightSphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        lightSphere.position.set(x, y, z);
        scn.add(lightSphere);
   }
}

您使用 Polar coordinate (distance, theta). The Cartesian coordinate 创建圆(切片)可以通过以下方式获得:

xc = cos(theta) * distance
yc = sin(theta) * distance

最后你对球体做了类似的事情:

x = xc * cos(phi)
y = yc * cos(phi)
z = sin(phi) * distance