Well, looking from it at a mathematical standpoint:
your problem is to find the shortest distance between a point in space and a vektor line or plane.
So if you have your coords in arrays like [a1,a2,a3]
and [b1,b2,b3]
the distance between this 2 points in 3 dimensional space is like the Pythagorean theorem with three elements:
sqrt[(a1-b1)²+(a2-b2)²+(a3-b3)²]=d(a,b)
.
I know this does not take the curvature of the earth into account but for "short" distances this is is not important.
If you understand some math the wikipedia article might help you as well. http://en.wikipedia.org/wiki/Euclidean_distance#Three_dimensions
Edit 12.08.14:
To take the curvature of the earth into account, you can do a simple calculation:
(1) You already know the distance of the earth
(2) you know the approx. radius of the earth
With your start point(A) and your destination(B) known, you now build a triangle with the center of the earth(C). You do this to now calculate the angle at (C) (sin/cos/tan). With that angle you can now get the length of the earth (including the curvature).
([boundary of earth]/360°)*[angle at (C)] = the dinstance from (A) to (B) over the curvature of the earth.