-1

I want to calculate a sphere (and render it with my JavaScript programme. (The render works successfully))

I found this question: Plotting a point on the edge of a sphere and afterwards I has developed this code:

for(var s = 0; s < 6; s++){
    for(var t = 0; t <= 3; t++){
        var x = d * Math.cos(s) * Math.sin(t);
        var y = d * Math.sin(s) * Math.sin(t);
        var z = d * Math.cos(t);
        console.log("("+x+","+y+","+z+")");
    }
}

It looks like:

It is doesn't look like a sphere!

So I changed the s++ to s+=0.1 and the t++ to t+=0.1.

Now it looks like:

Now it looks better, BUT my BIG PROBLEM: in the middle the lines should intersect on the Z axis. I think you can see the problem better on the first image.

Thanks for your answers!

EDIT: SOLUTION: unedited answer from @MBo:

for(var ss = 0; ss < 24; ss++){
    for(var tt = 0; tt <= 12; tt++){
        s = Math.Pi * ss / 12;
        t = Math.Pi * tt / 12;
        ...x y z stuff
Freddy C.
  • 119
  • 2
  • 11
  • `s` and `t` are supposed to be angles in range `[0, 2*PI]`. But you treat them as integers? – Ripi2 Mar 15 '18 at 17:17
  • `t <= 3` - are you rounding Pi to 3? https://en.wikipedia.org/wiki/Indiana_Pi_Bill – Igor Mar 15 '18 at 17:18

1 Answers1

1

s and t are polar coordinates angles, they must be in ranges 2*Pi~6.28 and Pi~3.14 correspondingly.

So make correct steps, for example:

var lonsteps = 24
var latsteps = 12 
for(var ss = 0; ss < lonsteps; ss++){
    for(var tt = 0; tt <= latsteps; tt++){
        s = 2 * Math.PI * ss / lonsteps;
        t = Math.PI * tt / latsteps;
        ...x y z stuff
MBo
  • 77,366
  • 5
  • 53
  • 86
  • Your old answer works, but your new answer doesn't work with negative values. Please change it back. – Freddy C. Mar 15 '18 at 17:32
  • @Freddy C OK, I forgot that shift by Pi/2 requires exchange of cos(t) and sin(t). Changed back. – MBo Mar 15 '18 at 17:40
  • Thanks a lot! I gave you the green checkmark. Please also change the Math.Pi to Math.PI that's an error too. – Freddy C. Mar 15 '18 at 17:42