# spherical coordinates

Discussion in 'Python' started by Bram Stolk, Apr 14, 2004.

1. ### Bram StolkGuest

Hi there,

Which module could I use if I want to do spherical coordinates in Python?

Thanks,

Bram

--
------------------------------------------------------------------------------
Bram Stolk, VR Engineer.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: Phone +31-20-5923059 Fax +31-20-6683167

"For the costs of subsidized agriculture in the EU, we can have all 56 million
European cows fly around the world. First Class." - J. Norberg
------------------------------------------------------------------------------

Bram Stolk, Apr 14, 2004

2. ### Peter MaasGuest

Bram Stolk wrote:
> Which module could I use if I want to do spherical coordinates in Python?

math. Formulas:

--- 2D

r = sqrt(x**2 + y**2)
phi = atan(y/x)

x = r*cos(phi)
y = r*sin(phi)

--- 3D

r = sqrt(x**2 + y**2 + z**2)
phi = atan(y/x)
theta = atan(z/sqrt(x**2+y**2)

x = r*cos(phi)*cos(theta)
y = r*sin(phi)*cos(theta)
z = r*sin(theta)

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
-------------------------------------------------------------------

Peter Maas, Apr 14, 2004

3. ### project2501Guest

what do you want to do with spherical coordinates? surely they are simply
a transform from cartesian coordinates?

On Wed, 14 Apr 2004 11:13:34 +0200, Bram Stolk wrote:

> Hi there,
>
>
> Which module could I use if I want to do spherical coordinates in Python?
>
> parnassus, google, and groups.google.com did not give me a good pointer.
>
>
> Thanks,
>
> Bram

project2501, Apr 14, 2004
4. ### Paul RubinGuest

Peter Maas <> writes:
> r = sqrt(x**2 + y**2)
> phi = atan(y/x)

Better use phi=atan2(y,x) in case x=0. Similarly for the other atan calls.

Paul Rubin, Apr 14, 2004
5. ### Paul McGuireGuest

"Paul Rubin" <http://> wrote in message
news:...
> Peter Maas <> writes:
> > r = sqrt(x**2 + y**2)
> > phi = atan(y/x)

>
> Better use phi=atan2(y,x) in case x=0. Similarly for the other atan

calls.

These are formulas for cylindrical coordinates. The OP was asking for
spherical coordinates rho, theta, and phi, where:

rho = distance from origin (similar to r in cylindrical coords)
theta = angle from the positive x axis of the xyz vector projection onto the
x-y plane (just like theta in cylindrical coords)
phi = angle of the xyz vector from the x-y plane

To convert from spherical to Cartesian:

x = rho * sin(phi) * cos(theta)
y = rho * sin(phi) * sin(theta)
z = rho * cos(phi)

From Cartesian to spherical:

rho = sqrt(x**2 + y**2 + z**2)
theta = atan2(y, x)
if rho != 0.0:
phi = acos( z / rho )
else:
phi = pi / 2 * sgn(z)

I can imagine that all these conversions could be a performance killer if
done entirely in Python, and could stand to be done as a C extension. This
is probably why the OP was asking if such a package already exists.

-- Paul

(Hmm, the math module doesn't have a sgn() function. Is this difficult to

Paul McGuire, Apr 14, 2004
6. ### Paul McGuireGuest

"Paul McGuire" <._bogus_.com> wrote in message
news:dX8fc.587\$...
> "Paul Rubin" <http://> wrote in message
> news:...
> > Peter Maas <> writes:
> > > r = sqrt(x**2 + y**2)
> > > phi = atan(y/x)

> >
> > Better use phi=atan2(y,x) in case x=0. Similarly for the other atan

> calls.
>
> These are formulas for cylindrical coordinates. The OP was asking for
> spherical coordinates rho, theta, and phi, where:
>
> rho = distance from origin (similar to r in cylindrical coords)
> theta = angle from the positive x axis of the xyz vector projection onto

the
> x-y plane (just like theta in cylindrical coords)
> phi = angle of the xyz vector from the x-y plane
>
> To convert from spherical to Cartesian:
>
> x = rho * sin(phi) * cos(theta)
> y = rho * sin(phi) * sin(theta)
> z = rho * cos(phi)
>
> From Cartesian to spherical:
>
> rho = sqrt(x**2 + y**2 + z**2)
> theta = atan2(y, x)
> if rho != 0.0:
> phi = acos( z / rho )
> else:
> phi = pi / 2 * sgn(z)
>
> I can imagine that all these conversions could be a performance killer if
> done entirely in Python, and could stand to be done as a C extension.

This
> is probably why the OP was asking if such a package already exists.
>
> -- Paul
>
> (Hmm, the math module doesn't have a sgn() function. Is this difficult to
>
>

D'oh - that's what I get for pulling formulas off the web and not reviewing
the material!!!

phi = angle of the xyz vector from the positive z-axis

phi = acos( z / rho)

Sorry!

http://www.math.montana.edu/frankw/ccp/multiworld/multipleIVP/spherical/body.htm

Paul McGuire, Apr 14, 2004
7. ### Bram StolkGuest

On Wed, 14 Apr 2004 11:06:49 GMT
"Paul McGuire" <._bogus_.com> wrote:

>
> I can imagine that all these conversions could be a performance killer if
> done entirely in Python, and could stand to be done as a C extension. This
> is probably why the OP was asking if such a package already exists.
>

Well, performance is not my first concern.
I just want encapsulated classes for convenience, that handle all
sorts of spherical coordinate specifics.

For instance... interpolation between spherical coordinates. You can avoid
going to/from cartesian if you properly handle the wrap-around at 180 and 360
degrees.

Also, I want to be able to recursively subdivide the theta,phy space, and
do stratification in theta,phy space, and al sorts of other operations, on
the surface of a given sphere.

An encapsulating class for these kind of coordinates would be a help, I
thought.

Bram

--
------------------------------------------------------------------------------
Bram Stolk, VR Engineer.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: Phone +31-20-5923059 Fax +31-20-6683167

"For the costs of subsidized agriculture in the EU, we can have all 56 million
European cows fly around the world. First Class." - J. Norberg
------------------------------------------------------------------------------

Bram Stolk, Apr 14, 2004
8. ### Peter MaasGuest

Paul McGuire wrote:
>>Peter Maas <> writes:
>>
>>>r = sqrt(x**2 + y**2)
>>>phi = atan(y/x)

>>
>>Better use phi=atan2(y,x) in case x=0. Similarly for the other atan

>
> calls.
>
> These are formulas for cylindrical coordinates.

r and phi are 2D spherical coordinates. You have to add a 3rd coordinate
(usually called z) to get cylindrical coordinates.

> The OP was asking for
> spherical coordinates rho, theta, and phi, where:

The OP was asking for spherical coordinates without mentioning
variables or dimensions. I posted the raw 2D and 3D formulas without
bothering about error handling or other implementation issues.

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
-------------------------------------------------------------------

Peter Maas, Apr 14, 2004
9. ### Bram StolkGuest

Organization: SARA
X-Newsreader: Sylpheed version 0.9.8claws (GTK+ 1.2.10; i686-pc-linux-gnu)
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

On Wed, 14 Apr 2004 11:06:49 GMT
"Paul McGuire" <._bogus_.com> wrote:

>
> I can imagine that all these conversions could be a performance killer if
> done entirely in Python, and could stand to be done as a C extension. This
> is probably why the OP was asking if such a package already exists.
>

Well, performance is not my first concern.
I just want encapsulated classes for convenience, that handle all
sorts of spherical coordinate specifics.

For instance... interpolation between spherical coordinates. You can avoid
going to/from cartesian if you properly handle the wrap-around at 180 and 360
degrees.

Also, I want to be able to recursively subdivide the theta,phy space, and
do stratification in theta,phy space, and al sorts of other operations, on
the surface of a given sphere.

An encapsulating class for these kind of coordinates would be a help, I
thought.

Bram

--
------------------------------------------------------------------------------
Bram Stolk, VR Engineer.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: Phone +31-20-5923059 Fax +31-20-6683167

"For the costs of subsidized agriculture in the EU, we can have all 56 million
European cows fly around the world. First Class." - J. Norberg
------------------------------------------------------------------------------

Bram Stolk, Apr 14, 2004