Get Spherical Coordinate Angles
in
Programming Questions
•
1 year ago
I have a code that can position a point relative to point(0, 0, 0) using Spherical Coordinates. I'm not sure how to reverse the process to get the angle information based on an arbitrary point. For example, if a point in space is at:
x = 0.25
y = -0.5
z = 1
Then I know that the radius is sqrt(sq(x)+sq(y)+sq(z)) or about 1.145. The elevation and azimuth are a little trickier for me to figure out. The code below positions a sphere (the red one) around another sphere (the stroke only one) at a fixed radius. I would like to make a code that tests every point in a bounding box and I'll need to know the angle relative to the center of the box. (If the box goes from -2 to 2 in the x, y, and z directions then I'll test every point inside of it assuming the box is centered on (0, 0, 0). In other words the box has a length of 4 in each direction.)
NOTE: In the code below I named elevation theta and azimuth phi based on this link:
http://en.wikipedia.org/wiki/Spherical_coordinate_system
- float rad = 200;
- float theta = HALF_PI;
- float phi = 0;
- void setup() {
- size(800, 600, P3D);
- }
- void draw() {
- //theta += TWO_PI/1000;
- phi += TWO_PI/100;
- float x = cos(phi)*sin(theta)*(rad);
- float y = cos(theta)*(rad);
- float z = sin(phi)*sin(theta)*(rad);
- background(255);
- translate(width/2, height/2);
- noFill();
- sphere(rad);
- translate(x, y, z);
- fill(255, 0, 0);
- sphere(20);
- }
1