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 & HelpSyntax Questions › points on a sphere
Page Index Toggle Pages: 1
points on a sphere (Read 469 times)
points on a sphere
May 8th, 2009, 7:22am
 
Hi everyone,

I've been trying to get a bunch of points randomly spread over a sphere but my sphere keeps ending up more of an ellipse. Any ideas? Many Thanks.

Code:
Location[] points = new Location[500];
int myWidth = 768;
int myHeight = 576;
int myX, myY, myZ;
int radius = 100;
float rotY = TWO_PI/25;

void setup()
{
 size(myWidth, myHeight, P3D);
 createPoints();
}

void draw()
{
 
 frameRate(25);
 translate(768/2, 576/2, 0);
 rotateY(rotY);
 
 background(215);
 
 for(int i = 0; i <= points.length-1; i++)
 {
   pushMatrix();
   translate(points[i].x, points[i].y, points[i].z);
   //rotateY(-rotY);
   rect(points[i].x, points[i].y, 5, 5);
   popMatrix();
 }

 rotY = rotY + TWO_PI/25;
}

void createPoints()
{
 for(int i = 0; i <= points.length-1; i++)
 {
   float angleA = random(0, TWO_PI);
   float angleB = random(0, TWO_PI);
     
   myX = int(radius*sin(angleA)*cos(angleB));
   myY = int(radius*sin(angleA)*sin(angleB));
   myZ = int(radius*cos(angleA));
   
   points[i] = new Location (myX, myY, myZ);
 }
}

class Location
{
 int x, y, z;
 
 Location (int myX, int myY, int myZ)
 {
   x = myX;
   y = myY;
   z = myZ;    
 }
}
Re: points on a sphere
Reply #1 - May 8th, 2009, 8:13am
 
angleA has to be between 0 and PI, not TWO_PI

Re: points on a sphere
Reply #2 - May 9th, 2009, 1:24am
 
just change your rect to

   rect(0, 0, 5, 5);

because right now your are translating once with point.x and point.y offsets, and a second time with the rect, which explain why your x/y radius are *2.
Re: points on a sphere
Reply #3 - May 9th, 2009, 10:54am
 
Yeah, I found it was a problem with the rect. I changed it to a sphere and suddenly saw what I was doing wrong. Thanks for the reply.
Page Index Toggle Pages: 1