spherical coordinates

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

  1. Bram Stolk

    Bram Stolk Guest

    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: 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
    #1
    1. Advertising

  2. Bram Stolk

    Peter Maas Guest

    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
    #2
    1. Advertising

  3. Bram Stolk

    project2501 Guest

    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
    #3
  4. Bram Stolk

    Paul Rubin Guest

    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
    #4
  5. Bram Stolk

    Paul McGuire Guest

    "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
    add?)
     
    Paul McGuire, Apr 14, 2004
    #5
  6. Bram Stolk

    Paul McGuire Guest

    "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
    > 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
     
    Paul McGuire, Apr 14, 2004
    #6
  7. Bram Stolk

    Bram Stolk Guest

    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
    #7
  8. Bram Stolk

    Peter Maas Guest

    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
    #8
  9. Bram Stolk

    Bram Stolk Guest

    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
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luc The Perverse
    Replies:
    12
    Views:
    1,335
    Roedy Green
    May 5, 2006
  2. Suresh  Kumar
    Replies:
    0
    Views:
    614
    Suresh Kumar
    Jul 3, 2003
  3. Suresh  Kumar
    Replies:
    0
    Views:
    528
    Suresh Kumar
    Jul 4, 2003
  4. mitu
    Replies:
    1
    Views:
    433
    Richard Herring
    Jan 27, 2010
  5. Marc-André Cournoyer

    [ANN] Thin 0.7.0 Spherical Cow release

    Marc-André Cournoyer, Feb 25, 2008, in forum: Ruby
    Replies:
    0
    Views:
    112
    Marc-André Cournoyer
    Feb 25, 2008
Loading...

Share This Page