Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Distance between 2 points in spherical coordinates

  1. Oct 12, 2009 #1
    Hello people,

    I'm creating an algorithm on Matlab and need to find the distance between two points in spherical coordinates where I have (r1,theta1,phi1) for the first point and (r2,theta2,phi2) for the second point.

    Of course, since I'm programming, I shouldn't use the dummy Cartesian coordinates distance rule because it'll consume the processor for some dummy math! I could think that the problem can be reduced to calculating the angle between the position vectors of these two points, then it'll become very simple because we can then use the law of cosines and find the distance between the 2 points with the modified Pythagorean.

    Is there a way to calculate that angle? the angle between the two position vectors of the two points.

    Any other proposals are welcome :-)

    Thank you in advance, for anything, even just for reading :D
  2. jcsd
  3. Oct 12, 2009 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    This looks a bit too much like homework, so I'm not going to give the answer outright.

    First, a rant.
    When you are programming, efficiency should usually be a low priority concern. In most computer programs, the vast majority of computing time is going to be isolated to a tiny, tiny fraction of the source code. Worry about efficiency for that tiny, tiny fraction. Don't worry about it elsewhere. The reason: Efficiency goes against almost every other software quality concern.
    End rant mode.

    The reason to avoid going through Cartesian coordinates is because that will add error. There is no way to avoid making several calls to the transcendental functions (sine and cosine). Getting accurate results is much more important than saving a few lines of code.

    It is a fairly easy matter to compute the cosine of the angle between the vectors. Compute (on paper, that is) the unit vectors defined by these vectors and use these unit vectors to compute the cosine of the angle between the vectors.
  4. Oct 12, 2009 #3
    LoL! homework? I'm doing my master thesis! LOL!!! HOMEWORK!!! hahahahahahahahahaha!!! does it really seem like a homework?

    I'm working on the subject called porous media and I'm building samples using stochastic conditions! and I'm using the spherical coordinates for some technical issues!

    Please, if you have the answer just say and don't make me wander and waste time I don't have. I tried to search for it on the Internet but they always talk about the "Geodesic" on a sphere which is not what I want :-) I'm not that brilliant in geometry ;-)

    Thank you
  5. Oct 12, 2009 #4
    And by the way, about the efficiency thing! my program keeps our cluster system busy for few days to give a result, our cluster system is equivalent to a supercomputer!!! so efficiency is not that negligible in my case, is it? ;)
  6. Oct 12, 2009 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Actually, the geodesic on a sphere is very close to what you want. The formulae used to compute the distance between two points on a spherical Earth contains the necessary calculations. You want the cosine of the angle between two lines. That geodesic calculation wants the angle itself.

    First, some nomenclature. There are multiple ways to represent things in spherical coordinates. I'll use [itex]\phi[/itex] as the angle between the x-y plane and the vector in question ([itex]\phi=\pi/2[/itex] for the +z axis) and [itex]\theta[/itex] as the angle between the x axis and the projection of the vector onto the x-y plane.

    A vector from the origin to a point [itex](1,\phi,\theta)[/itex] is a unit vector. In Cartesian coordinates, [itex]\hat u = \cos\phi\cos\theta \hat x + \cos\phi\sin\theta \hat y + \sin\phi \hat z[/itex]. The cosine of the angle [itex]\psi[/itex] between two such unit vectors defined by [itex](1,\phi_1,\theta_1)[/itex] and [itex](1,\phi_2,\theta_2)[/itex] is given by the inner product between these unit vectors:

    \cos\psi &= \cos\phi_1\cos\phi_2\cos\theta_1\cos\theta_2 + \cos\phi_1\cos\phi_2\sin\theta_1\sin\theta_2 + \sin\phi_1\sin\phi_2 \\
    &= \cos\phi_1\cos\phi_2\cos(\theta_1-\theta_2) + \sin\phi_1\sin\phi_2

    The law of cosines gives the distance between two points as

    [tex]d^2 = {r_1}^2 + {r_2}^2 + 2 r_1 r_2 \cos \psi[/tex]

    You can use this, but you can do better. The above does not fare so well when points are nearby. The haversine formula (google that) will help in this regard.

    Alternatively, you could just convert to Cartesian and compute the distance.

    Well, how often are you going to be doing this calculation? If it is rarely, then it doesn't matter. Using most efficient and most inefficient algorithms possible won't make a dent in CPU time if you are calling it only rarely.

    Have you profiled your program to see where which parts is sucking all the CPU? One program I worked with involved tens of thousands of lines of code. It turned out that six lines out of those tens of thousands were responsible for more than 90% of the CPU usage. Needless to say, we optimized the heck out of those six lines (and the code immediately around them).

    Have you analyzed your design? A lot of times the best way to make a program more efficient is to find out where you designed things stupidly. (Lots of supposedly brilliant designs are in fact rather stupid designs after the fact.)

    Why are you representing things in spherical coordinates? Would cartesian be a better choice? If you are using object oriented techniques, it might be worthwhile to calculate both the spherical and cartesian representations every time you update. Then you will have both representations available and be able to use the one that makes more sense.

    When it comes to calculating distances between points, the cartesian representation makes a lot more sense than does the spherical representation.
  7. Oct 12, 2009 #6
    Thank you very much pal for the help :). About how often I'm gonna do this, heh, veryyyyyyyyyyyy often, you can say up to billions ;). You've pointed some good idea in including both coordinates, I think this'll reduce the calculation, I'll do my math and consider it ;)

    Thanks a lot pal!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Distance between 2 points in spherical coordinates
  1. Spherical Coordinates (Replies: 0)

  2. Spherical Coordinates (Replies: 4)

  3. Spherical Coordinates (Replies: 1)

  4. Spherical coordinates? (Replies: 5)