We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Problems with spherical coordinates
Page Index Toggle Pages: 1
Problems with spherical coordinates (Read 995 times)
Problems with spherical coordinates
Apr 15th, 2006, 8:27pm
 
Hi!

I don't know why the following code doesn't draw a sphere consisting of a few smaller spheres.

I made comments in the code to further explain what's the problem.


Quote:


void setup() {
 framerate(40);
 size(300,300,P3D);
 background(255);

 c = new Circle();
 c.p = 75;
}

Circle c;

void draw() {    

 translate(width/2,height/2,0);


 // ********************* //
 // Why do these lines not draw a Sphere consisting of
 // a few smaller spheres?
 for (float p = 0; p<TWO_PI; p += PI/10) {
   c.phi = p;
   for (float t = 0; t<TWO_PI; t += PI/10) {
            c.theta = t;
            c.display();
   }
 }

 // if you uncomment the following lines and
 // comment the above lines, the program does
 // draw a sphere like I want it
 
 /*c.theta += PI/10;
 
  if (c.theta > TWO_PI) {
  c.theta = 0;
  c.phi += PI/10;
  }
 
  c.display();*/

}

class Circle {

 float p,theta,phi;
 float x,y,z;

 Circle() {
   p = 0;
   theta = 0;
   phi = 0;
   x = 0;
   y = 0;
   z = 0;
 }

 void display() {

   SphericalToCartesian();

   noStroke();
   fill(50);
   translate(x,y,z);
   sphere(3);
 }

 void SphericalToCartesian() {    
   z = p*sin(phi)*cos(theta);
   x = p*sin(phi)*sin(theta);
   y = p*cos(phi);
 }
}



Re: Problems with spherical coordinates
Reply #1 - Apr 15th, 2006, 9:35pm
 
Another problem is how to position the spheres steady-going.
By now, for example on the top or bottom will be just as many spheres as in the middle, but the radius of the ring they form is much smaller. Anyone got an idea how to solve this problem?
Re: Problems with spherical coordinates
Reply #2 - Apr 17th, 2006, 3:05pm
 
Hey,
didnt really understand the problem of the first post, but your second problem i've also stumbled over; how to position the spheres 'steady going'.
What you need is some kind of geodesic form, like the icosahedron or anything similar, depending on you resolution.  these are spheres where the points are equally spaced apart. I was not able to find a formula for this, so i used Blender to make one, and exported it as an .OBJ file and read it using the OBJLoader library.
Hope this helps

Henrik
Re: Problems with spherical coordinates
Reply #3 - Apr 17th, 2006, 3:18pm
 
Hi, thanks for your answer.

I know I didn't explain the first problem very good. When you copy the sourcecode into processing and start it, you'll know what the problem is.



Re: Problems with spherical coordinates
Reply #4 - Apr 17th, 2006, 5:59pm
 
You weren't using popMatrix() and pushMatrix();

You intended to start from 0,0,0 and jump out to a point on the sphere. But because you didn't clear the translation from memory and put yourself back at 0,0,0 on the next iteration you proceeded onwards from the edge of the sphere to some ungodly co-ords.

The following code works, it looks quite pretty, well done.
Quote:


Circle c;
void setup() {
 framerate(40);
 size(300,300,P3D);
 background(255);
 c = new Circle();
 c.p = 75;
}
void draw() {    
 translate(width/2,height/2,100);
 for (float p = 0; p<TWO_PI; p += PI/10) {
   c.phi = p;
   for (float t = 0; t<TWO_PI; t += PI/10) {
  c.theta = t;
  c.display();
   }
 }
}

class Circle {
 float p,theta,phi;
 float x,y,z;
 Circle() {
   p = 0;
   theta = 0;
   phi = 0;
   x = 0;
   y = 0;
   z = 0;
 }
 void display() {
   SphericalToCartesian();
   noStroke();
   fill(50);
   pushMatrix();
   translate(x,y,z);
   sphere(3);
   popMatrix();
 }
 void SphericalToCartesian() {    
   z = p*sin(phi)*cos(theta);
   x = p*sin(phi)*sin(theta);
   y = p*cos(phi);
 }
}



A cheap fix for the extra spheres at the polar caps would be to reduce the iterations. Another method might be to look at the source code for Processing's sphere() method. I tried looking myself on dev.processing.org and I've no idea where they keep the bloody thing. Good luck.
Re: Problems with spherical coordinates
Reply #5 - Apr 17th, 2006, 6:52pm
 
thank you, that was what I was looking for.

I also took a look at dev.processing.org, but I find it very confusing. Unfortunately there's no zipped source code available for processing.
Page Index Toggle Pages: 1