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


Groups > comp.lang.c++ > #8903

Re: Sperical and Cartesian Coordinates

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c++
Subject Re: Sperical and Cartesian Coordinates
Date 2011-08-16 10:51 +0000
Organization Freie Universitaet Berlin
Message-ID <9av0d9F9heU1@mid.uni-berlin.de> (permalink)
References <4e4a3379$0$2559$703f8584@news.kpn.nl>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web