Sperical and Cartesian Coordinates

L

ldries46

I need to convert Spherical and Cartesian coordinates and back. In MATLAB
there are two routines mentioned cart2sph and sph2cart, in references about
these routines C/C++ versions are mentioned .
Is there anyone who knows where to find these routines (in which library) or
where to download them. Also other routines that make the conversion are
welcome.

L. Dries
 
L

Leclerc

I need to convert Spherical and Cartesian coordinates and back. In
MATLAB there are two routines mentioned cart2sph and sph2cart, in
references about these routines C/C++ versions are mentioned .
Is there anyone who knows where to find these routines (in which
library) or where to download them. Also other routines that make the
conversion are welcome.

L. Dries

You don't need libs for something like that.
at MATLAB prompt type (without quotes) 'edit cart2sph' and/or 'edit
sph2cart', and you will see equations governing transform.
 
J

Jens Thoms Toerring

ldries46 said:
I need to convert Spherical and Cartesian coordinates and back. In MATLAB
there are two routines mentioned cart2sph and sph2cart, in references about
these routines C/C++ versions are mentioned .
Is there anyone who knows where to find these routines (in which library) or
where to download them. Also other routines that make the conversion are
welcome.

You can look up the formulas e.g. here

http://en.wikipedia.org/wiki/Spherical_coordinate_system

and it's straightforward to implement:

/******************************//**
* \brief Conversion from cartesian to spherical coordinates (if the
* resulting radius is 0 also the azimutal and inclination
* angles get set to 0).
*
* @param r Reference to return the radius
* @param theta Reference to return the inclination angle (in radian)
* @param phi Reference to return the azimutal angle (in radian)
* @param x x coordinate
* @param y y coordinate
* @param z z coordinate
******************************/

void
cartesianToSpherical( double & r,
double & theta,
double & phi,
double x,
double y,
double z )
{
if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
{
theta = acos( z / r );
phi = atan2( y, x );
}
else
theta = phi = 0.0;
}


/******************************//**
* \brief Conversion from spherical to cartesian coordinates
*
* @param x Reference to return the x coordinate
* @param y Reference to return the y coordinate
* @param z Reference to return the z coordinate
* @param r Radius (must be non-negative)
* @param theta Inclination angle (in radian)
* @param phi Azimutal angle (in radian)
******************************/

void
sphericalToCartesian( double & x,
double & y,
double & z,
double r,
double theta,
double phi )
{
if ( r < 0.0 )
throw "Negative radius in sphericalToCartesian()";

x = r * sin( theta ) * cos( phi );
y = r * sin( theta ) * sin( phi );
z = r * cos( theta );
}
Regards, Jens
 
S

skuratiov

You can look up the formulas e.g. here

http://en.wikipedia.org/wiki/Spherical_coordinate_system

and it's straightforward to implement:

/******************************//**
* \brief Conversion from cartesian to spherical coordinates (if the
* resulting radius is 0 also the azimutal and inclination
* angles get set to 0).
*
* @param r Reference to return the radius
* @param theta Reference to return the inclination angle (in radian)
* @param phi Reference to return the azimutal angle (in radian)
* @param x x coordinate
* @param y y coordinate
* @param z z coordinate
******************************/

void
cartesianToSpherical( double & r,
double & theta,
double & phi,
double x,
double y,
double z )
{
if ( ( r = sqrt( x * x + y * y + z * z ) ) != 0.0 )
{
theta = acos( z / r );
phi = atan2( y, x );
}
else
theta = phi = 0.0;
}


/******************************//**
* \brief Conversion from spherical to cartesian coordinates
*
* @param x Reference to return the x coordinate
* @param y Reference to return the y coordinate
* @param z Reference to return the z coordinate
* @param r Radius (must be non-negative)
* @param theta Inclination angle (in radian)
* @param phi Azimutal angle (in radian)
******************************/

void
sphericalToCartesian( double & x,
double & y,
double & z,
double r,
double theta,
double phi )
{
if ( r < 0.0 )
throw "Negative radius in sphericalToCartesian()";

x = r * sin( theta ) * cos( phi );
y = r * sin( theta ) * sin( phi );
z = r * cos( theta );
}
Regards, Jens

Will not work if x == 0.0
 
F

Fred Zwarts \(KVI\)

wrote in message
Will not work if x == 0.0

A very delayed response ....
In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
divide by x.
I am not sure what happens if both x and y equal 0.
 
J

Jens Thoms Toerring

Fred Zwarts \(KVI\) said:
In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
divide by x.

Exactly, that'w why one uses atan2(y, x) instead of atan(y/x).
I am not sure what happens if both x and y equal 0.

I guess that could be a problem - the C++ standard doesn't tell
much about the behaviour of atan2(), but since it seems to be
aligned to the C standard that might be the one relevant here.
And the C99 (as well as the C89) standard says that a domain
error may occur if both arguments are zero. So it would be
prudent to check for this special case (whichh I missed). The
value of phi is not well-defined mathematically in that case
- a reasonable choice would seem to be 0 (but any value would
do in principle), while theta is either 0 when z > 0 or pi for
z < 0.
Best regards, Jens
 
F

Fred Zwarts \(KVI\)

"Juha Nieminen" wrote in message news:[email protected]...
What do you mean "necessarily"? It doesn't, period.

Are you sure it never does? Have you checked the implementation code on all
platforms?

What I mean is that there might be case where a division by x is a possible
implementation. Take, e.g., the case that |y| is much smaller than |x|. Then
atan2(y,x) equals y/x within rounding errors.

Of course for |x| much smaller than |y| other methods will be used.
 
N

Nobody

Exactly, that'w why one uses atan2(y, x) instead of atan(y/x).

No, one uses atan2(y,x) instead of atan(y/x) because the former chooses
the correct quadrant when x is negative, whereas the latter will be off by
pi radians = 180 degrees.

If x and y both equal zero, then phi is undefined. This isn't an
implementation issue (what is the longitude of the north pole)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top