Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
Three js calculate sphere from polar coordinates

I am useing three.js an want to calculate a sphere. I am using two for loops one for theta and one for phi and then transforming the polar coordinates to Cartesian coordinates. For every calculated point I added a Point. The result is not a sphere.

This is the nested for loop:

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);
  }
}

And here is the full code, including the result: Link

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

j must be in range [-90, 90] 9instead of [0, 260]. Notice that you are creating slices (360 °) from the South Pole to the North Pole (180 °).

You must compute the sin(theta) and the cos(theta) and multiply both with 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;

Complete algorithm:

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);
   }
}

You create circles (slices) using Polar coordinate (distance, theta). The Cartesian coordinate can be get by:

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

Finally you do something similar for the sphere:

x = xc * cos(phi)
y = yc * cos(phi)
z = sin(phi) * distance
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda