Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > spherical coordinates

Reply
Thread Tools

spherical coordinates

 
 
Bram Stolk
Guest
Posts: n/a
 
      04-14-2004
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: http://www.velocityreviews.com/forums/(E-Mail 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
------------------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Peter Maas
Guest
Posts: n/a
 
      04-14-2004
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 (E-Mail Removed)
-------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
project2501
Guest
Posts: n/a
 
      04-14-2004
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


 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      04-14-2004
Peter Maas <(E-Mail Removed)> 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.
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      04-14-2004
"Paul Rubin" <http://(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Maas <(E-Mail Removed)> 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
add?)


 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      04-14-2004

"Paul McGuire" <(E-Mail Removed)._bogus_.com> wrote in message
news:dX8fc.587$(E-Mail Removed)...
> "Paul Rubin" <http://(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Peter Maas <(E-Mail Removed)> 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
> 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/c...rical/body.htm


 
Reply With Quote
 
Bram Stolk
Guest
Posts: n/a
 
      04-14-2004
On Wed, 14 Apr 2004 11:06:49 GMT
"Paul McGuire" <(E-Mail Removed)._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: (E-Mail 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
------------------------------------------------------------------------------
 
Reply With Quote
 
Peter Maas
Guest
Posts: n/a
 
      04-14-2004
Paul McGuire wrote:
>>Peter Maas <(E-Mail Removed)> 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 (E-Mail Removed)
-------------------------------------------------------------------
 
Reply With Quote
 
Bram Stolk
Guest
Posts: n/a
 
      04-14-2004
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" <(E-Mail Removed)._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: (E-Mail 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
------------------------------------------------------------------------------
 
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
Travelling Salesman with Spherical Coordinates. Luc The Perverse Java 12 05-05-2006 09:06 PM
Gregwired Panosaurus Spherical Tripod Head Review at XYZ Computing Silverstrand Front Page News 0 01-03-2006 04:42 AM
Re: Sony 717 and SPHERICAL aberation Max Burke Digital Photography 7 09-12-2003 10:40 PM
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