0

I've been trying to translate this amazing tutorial, from Python to Javascript. I have no experience with Python, but I made the 2D translation work, is the 3D I'm having trouble with. Not sure how to properly translate the numpy.linspace function and the numpy.sqrt vector operation into javascript. So far this is what I got. Thanks a lot in advance

 function buildSphere() {
  var n = 246;
  var sphereR = 300;
  var theJSON = [];

  for(i = 0; i < n; i++){

    theJSON[i].coordinate = new mo.Point3D(0,0,0);
    var goldenAng = Math.PI * (3 - Math.sqrt(5));
    var theta = i * goldenAng;
    var z = (1-1.0/n, 1.0/n-1, n);    
    var r = (Math.sqrt(1-i)*sphereR) * Math.sqrt(z);

    theJSON[i].coordinate.x = r * Math.cos(theta);
    theJSON[i].coordinate.y = r * Math.sin(theta);
    theJSON[i].coordinate.z = z ;

  }
}

the Python code I'm translating is this:

n = 256

golden_angle = numpy.pi * (3 - numpy.sqrt(5))
theta = golden_angle * numpy.arange(n)
z = numpy.linspace(1 - 1.0 / n, 1.0 / n - 1, n)
radius = numpy.sqrt(1 - z * z)

points = numpy.zeros((n, 3))
points[:,0] = radius * numpy.cos(theta)
points[:,1] = radius * numpy.sin(theta)
points[:,2] = z

Any light shed is immensely appreciated, again thanks!

vulu
  • 1
  • 2

0 Answers0