# Distance between 2 points in spherical coordinates

1. Oct 12, 2009

### TheDestroyer

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 :-)

2. Oct 12, 2009

### Staff: Mentor

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.

3. Oct 12, 2009

### TheDestroyer

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

4. Oct 12, 2009

### TheDestroyer

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? ;)

5. Oct 12, 2009

### Staff: Mentor

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 $\phi$ as the angle between the x-y plane and the vector in question ($\phi=\pi/2$ for the +z axis) and $\theta$ 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 $(1,\phi,\theta)$ is a unit vector. In Cartesian coordinates, $\hat u = \cos\phi\cos\theta \hat x + \cos\phi\sin\theta \hat y + \sin\phi \hat z$. The cosine of the angle $\psi$ between two such unit vectors defined by $(1,\phi_1,\theta_1)$ and $(1,\phi_2,\theta_2)$ is given by the inner product between these unit vectors:

\aligned \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 \endaligned

The law of cosines gives the distance between two points as

$$d^2 = {r_1}^2 + {r_2}^2 + 2 r_1 r_2 \cos \psi$$

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.

6. Oct 12, 2009

### TheDestroyer

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!