spherical coordinates

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Apr 14 07:19:08 EDT 2004


"Paul McGuire" <ptmcg at austin.rr._bogus_.com> wrote in message
news:dX8fc.587$lp3.280 at fe2.texas.rr.com...
> "Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message
> news:7x1xmqua94.fsf at ruckus.brouhaha.com...
> > Peter Maas <peter.maas at mplusr.de> 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/ccp/multiworld/multipleIVP/spherical/body.htm





More information about the Python-list mailing list