【问题标题】:Three js calculate sphere from polar coordinates三个js从极坐标计算球体
【发布时间】:2021-11-18 07:03:00
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: javascript reactjs three.js trigonometry


    【解决方案1】:

    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 coordinatedistancetheta)创建圆圈(切片)。 Cartesian coordinate 可以通过以下方式获得:

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

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

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

    【讨论】:

    • 谢谢它工作正常。但我并不真正理解背后的理论。您知道详细解释的获取资源吗?为什么用cos代替sin?我使用维基百科的公式将极坐标转换为笛卡尔坐标
    • @otto 这只是几何学。您可以使用Polar coordinatedistancetheta)创建圆圈(切片)。 Cartesian coordinate 可以通过 xc = cos(theta) * distance, yc = sin(theta) * distance 和球体 x = xc * cos(phi), y = yc * cos(phi), z = sin(phi) * 距离
    猜你喜欢
    • 2019-08-09
    • 2017-11-13
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多