# Sperical and Cartesian Coordinates

Discussion in 'C++' started by ldries46, Aug 16, 2011.

1. ### ldries46Guest

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
ldries46, Aug 16, 2011

2. ### LeclercGuest

On 16.8.2011. 11:07, ldries46 wrote:
> 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.
Leclerc, Aug 16, 2011

3. ### Jens Thoms ToerringGuest

ldries46 <> wrote:
> 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 )

x = r * sin( theta ) * cos( phi );
y = r * sin( theta ) * sin( phi );
z = r * cos( theta );
}
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
Jens Thoms Toerring, Aug 16, 2011
4. ### Guest

On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
> ldries46 <> wrote:
> > 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
> --
> \ Jens Thoms Toerring ___
> \__________________________ http://toerring.de

Will not work if x == 0.0
, Apr 18, 2013
5. ### Ike NaarGuest

On 2013-04-18, <> wrote:
> On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <> wrote:
>> > 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 );
>> }

> Will not work if x == 0.0

Can you elaborate on that?
Both sqrt(x*x + y*y + z*z) and atan2(y, x) are well-defined for x==0 .
Ike Naar, Apr 18, 2013
6. ### Fred Zwarts \(KVI\)Guest

wrote in message
news:...
>
>On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <> wrote:
>> > I need to convert Spherical and Cartesian coordinates and back. In
>> > MATLAB
>> > there are two routines mentioned cart2sph and sph2cart, in references
>> > 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
>> --
>> \ Jens Thoms Toerring ___
>> \__________________________ http://toerring.de

>
>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.
Fred Zwarts \(KVI\), Apr 18, 2013
7. ### Jens Thoms ToerringGuest

"Fred Zwarts \(KVI\)" <> wrote:
> 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
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
Jens Thoms Toerring, Apr 18, 2013
8. ### Fred Zwarts \(KVI\)Guest

"Juha Nieminen" wrote in message news:kkokd8\$2s8h\$...
>
>"Fred Zwarts \(KVI\)" <> wrote:
>> In general x == 0.0 is not a problem. atan2( y, x ) does not necessarily
>> divide by x.

>
>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.
Fred Zwarts \(KVI\), Apr 18, 2013
9. ### NobodyGuest

On Thu, 18 Apr 2013 11:39:06 +0000, Jens Thoms Toerring wrote:

> "Fred Zwarts \(KVI\)" <> wrote:
>> 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).

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

>> I am not sure what happens if both x and y equal 0.

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)?
Nobody, Apr 18, 2013