Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda