Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sperical and Cartesian Coordinates

Reply
Thread Tools

Sperical and Cartesian Coordinates

 
 
ldries46
Guest
Posts: n/a
 
      08-16-2011
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

 
Reply With Quote
 
 
 
 
Leclerc
Guest
Posts: n/a
 
      08-16-2011
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.
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-16-2011
ldries46 <(E-Mail Removed)> 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/Spheric...rdinate_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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
skuratiov@gmail.com
Guest
Posts: n/a
 
      04-18-2013
On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
> ldries46 <(E-Mail Removed)> 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/Spheric...rdinate_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 ___ (E-Mail Removed)
> \__________________________ http://toerring.de


Will not work if x == 0.0
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      04-18-2013
On 2013-04-18, (E-Mail Removed) <(E-Mail Removed)> wrote:
> On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <(E-Mail Removed)> 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/Spheric...rdinate_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 .
 
Reply With Quote
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      04-18-2013
wrote in message
news:(E-Mail Removed)...
>
>On Tuesday, August 16, 2011 1:51:21 PM UTC+3, Jens Thoms Toerring wrote:
>> ldries46 <(E-Mail Removed)> 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/Spheric...rdinate_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 ___ (E-Mail Removed)
>> \__________________________ 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.

 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      04-18-2013
"Fred Zwarts \(KVI\)" <(E-Mail Removed)> 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 ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      04-18-2013
"Juha Nieminen" wrote in message news:kkokd8$2s8h$(E-Mail Removed)...
>
>"Fred Zwarts \(KVI\)" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      04-18-2013
On Thu, 18 Apr 2013 11:39:06 +0000, Jens Thoms Toerring wrote:

> "Fred Zwarts \(KVI\)" <(E-Mail Removed)> 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
pi radians = 180 degrees.

>> 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)?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cartesian to lat and lon Andrew Rich Perl Misc 3 09-08-2008 07:54 PM
Cartesian and polar coordinates library Bart Braem Ruby 5 08-23-2006 06:59 PM
tuples and cartesian coordinates Gerrit Holl Python 5 12-18-2003 03:55 AM
Converting canvas coordinates to window coordinates in tkinter......... Suresh Kumar Python 0 07-04-2003 10:14 AM
Converting canvas coordinates to window coordinates....... Suresh Kumar Python 0 07-03-2003 04:53 AM



Advertisments