spherical coordinates

B

Bram Stolk

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


--
------------------------------------------------------------------------------
Bram Stolk, VR Engineer.
SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP AMSTERDAM
email: (e-mail address removed) 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
------------------------------------------------------------------------------
 
P

Peter Maas

Bram said:
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
 
P

project2501

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

Paul McGuire

Paul Rubin said:
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
add?)
 
P

Paul McGuire

Paul McGuire said:
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
add?)
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
 
B

Bram Stolk

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: (e-mail address removed) 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
------------------------------------------------------------------------------
 
P

Peter Maas

Paul said:
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
 
B

Bram Stolk

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

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: (e-mail address removed) 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
------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top