Let P be an arbitrary point. Let S be a segment.
Is there any way of computing the shortest distance between P and S without using cross product?
I found a formula that uses cross product. However, I need to calculate the distance for millions of points. (~ 100 million points)
I am using MATLAB, and using the profiler I realized that the cross product function is taking most of the computation time.
Any ideas would be greatly appreciated.
Let the line segment be $x(t) = x_0 + t d_0$ with $t \in [0,1]$.
You want to solve $\min_{t \in [0,1]} \|x(t) -p_0\|$.
Let $\phi(t) = \|x(t) -p_0\|^2 = ||x_0-p_0\|^2+t^2 \|d_0\|^2-2 t \langle p_0 -x_0, d_0 \rangle$. $\phi$ is a convex quadratic, hence it has a unique minimum, and distance increases as $t$ moves away from the minimizing $t$. Hence we can solve the unconstrained problem first, and then 'project' the solution $t'$ back onto $[0,1]$.
The unconstrained solution is found by solving $\phi'(t') = 0$, which gives $t' = \frac{\langle p_0 -x_0, d_0 \rangle}{\|d_0\|^2}$. Now let $t_0 = \min(1, \max(0, t'))$. Then the shortest distance is given by $\|x(t_0) - p_0\|$.