Hello all,
I'm very confused by the values given by cart2sph in zexy 1.1. They conflict with my original expr based equation.
So If I have x=5, y=5 and z=5
cart2sph gives me: r=8.66, phi=0.78, theta=0.61
In my own implementation I get: r=8.66, theta=0.955, phi=0.78
I'm using this equation:
x^2+y^2+z^2=r^2 theta=cos^-1(z/r) phi=tan^-1(y/x)
From the zexy source I see:
x->new_coord[0]=sqrtf(X*X+Y*Y+Z*Z); /* R */ x->new_coord[1]=atan2f(Y, X); /* PHI */ x->new_coord[2]=atan2f(Z, sqrt(X*X+Y*Y)); /* THETA */
Close, but not quite?! sorry my trig is not the best. Can anyone give me a hand with whats going on?
Thanks! Ben
PS: I attached my own expr version for comparison
On 21 Nov, bbogart@ryerson.ca wrote:
I'm very confused by the values given by cart2sph in zexy 1.1. They conflict with my original expr based equation.
So If I have x=5, y=5 and z=5
[...]
I'm using this equation:
x^2+y^2+z^2=r^2 theta=cos^-1(z/r) phi=tan^-1(y/x)
From the zexy source I see:
x->new_coord[0]=sqrtf(X*X+Y*Y+Z*Z); /* R */ x->new_coord[1]=atan2f(Y, X); /* PHI */ x->new_coord[2]=atan2f(Z, sqrt(X*X+Y*Y)); /* THETA */
PHI is all right, but if: atan2f(a,b) := atan(a/b) then THETA is wrong (wrong parameter order), corrected:
$ diff zexy/src/z_coordinates.c ./z_coordinates.c 218c218
x->new_coord[2]=atan2f(sqrt(X*X+Y*Y), Z); /* THETA */
Because of this identity: atan( sqrt( x*x + y*y ) / z ) == acos( z / r ). Your formula also is correct. Lets see what my calculator says:
$ echo 'a( 5 / 5 )' | bc -l .78539816339744830961
echo 'a( sqrt( 5*5 + 5*5 ) / 5 )' | bc -l .95531661812450927816
As to be expected. Any veto?
IOhannes? zexy 1.2? Or is it free on CVS? (ask first ;^)
BTW: atan2(a,b) is a standard c function? Makes sense in case b is zero. So i had had no need to implement it myself?
HTH
On Fri, 22 Nov 2002, Robert Figura wrote:
On 21 Nov, bbogart@ryerson.ca wrote: PHI is all right, but if: atan2f(a,b) := atan(a/b)
that's almost true, yes.
then THETA is wrong (wrong parameter order), corrected:
It is wrong only if the documentation says it should be otherwise. But colatitude is very often used. For example, when I was in 13th grade we were doing jacobian coordinate transforms, and we learned spherical coordinates using colatitude instead of latitude.
BTW: atan2(a,b) is a standard c function? Makes sense in case b is zero. So i had had no need to implement it myself?
atan2 is a standard C function; it's the only inverse trigonometric function you really need, and all other ones may be derived from it. Atan2 is so convenient. I remember coding it by hand in terms of ATN() when i used the BASIC language long ago.
It is true it works even when b is zero, but there's also another neat feature: it has a 2pi radian range instead of just pi -- it uses both signs of a and b to compute the result.
Mathieu Bouchard http://artengine.ca/matju
On Thu, 21 Nov 2002 bbogart@ryerson.ca wrote:
I'm very confused by the values given by cart2sph in zexy 1.1. They conflict with my original expr based equation. So If I have x=5, y=5 and z=5 cart2sph gives me: r=8.66, phi=0.78, theta=0.61 In my own implementation I get: r=8.66, theta=0.955, phi=0.78 [...] Close, but not quite?! sorry my trig is not the best. Can anyone give me a hand with whats going on?
There are two common kinds of spherical coordinates. Some people use longitude&latitude, and some people use longitude&colatitude. Latitude ranges from -Pi/2 to +Pi/2, and Colatitude ranges from 0 to Pi.
Your theta is colatitude, so your point is 0.955 radians south from the north pole. Cart2sph's theta is latitude, so your point is 0.61 radians north of the equator. If values were exact, .61+.955 = Pi/2 radians, the angle from equator to north pole.
Mathieu Bouchard http://artengine.ca/matju