Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8893 > unrolled thread

Sperical and Cartesian Coordinates

Started by"ldries46" <bertus.dries@planet.nl>
First post2011-08-16 11:07 +0200
Last post2011-08-16 10:51 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  Sperical and Cartesian Coordinates "ldries46" <bertus.dries@planet.nl> - 2011-08-16 11:07 +0200
    Re: Sperical and Cartesian Coordinates Leclerc <gordan.sikic.remove@this.inet.hr> - 2011-08-16 11:33 +0200
    Re: Sperical and Cartesian Coordinates jt@toerring.de (Jens Thoms Toerring) - 2011-08-16 10:51 +0000

#8893 — Sperical and Cartesian Coordinates

From"ldries46" <bertus.dries@planet.nl>
Date2011-08-16 11:07 +0200
SubjectSperical and Cartesian Coordinates
Message-ID<4e4a3379$0$2559$703f8584@news.kpn.nl>
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 

[toc] | [next] | [standalone]


#8896

FromLeclerc <gordan.sikic.remove@this.inet.hr>
Date2011-08-16 11:33 +0200
Message-ID<j2ddh6$k99$1@sunce.iskon.hr>
In reply to#8893
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.

[toc] | [prev] | [next] | [standalone]


#8903

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-08-16 10:51 +0000
Message-ID<9av0d9F9heU1@mid.uni-berlin.de>
In reply to#8893
ldries46 <bertus.dries@planet.nl> 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  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web