# How to find point on line closest to sphere?

Given:

• $r_0$ - Position vector of a point outside of the sphere in ${\rm I\!R^3}$.
• $v$ - Line of sight vector from the point outside of the sphere in ${\rm I\!R^3}$.
• $R$ - Radius of the sphere.

Calculate:

• $r$ - Position vector of closest point on the sphere to the line created by extending the LOS vector from r_0, whose Frobenius norm is R.

My approach:

Assumption: Given that this is a sphere, the point on the sphere that is closest to the line created by extending the LOS vector must be on the perpendicular segment connecting the center of the sphere to said line. I do not think this holds true for a spheroid, but that's a possible follow up question for later.

1. $p = r_0 \times v$ yields the vector tangent to the plane formed by $r_0$ and $v$ and is thus orthogonal to both $r_0$ and $v$.
2. $r' = p \times v$ yields the vector pointing from the center of the sphere to the line created by the vector $v$
3. $r'$ differs from $r$ by a scaling factor $C$, such that $||Cr'|| = R$. We can solve for $C = R/||r'||$
4. Scale $r'$ to get $r$ by $r = Cr'$

I feel like I'm missing something however or that my assumption might not be correct.

• This is a classic Lagrange multiplier problem: en.wikipedia.org/wiki/Lagrange_multiplier Sep 8 '16 at 12:19
• Cool, so that sounds like a fun way to try it...if that's the case, then my function to minimize would be the magnitude of the difference between the line and the sphere? Subject to the constraint of the equation of the sphere. I think the trouble I have with this part is coming up with the functional form of the line. Using a parametric form of the line based off the origin of the line and the los vector gives me a t that makes this a little nasty. Sep 8 '16 at 13:00
• Yes, its an ugly set of equations. The simplest way I can think of is posted below. Sep 9 '16 at 5:14

The line has one parameter $t$ : $(x_0+s_x t, y_0 + s_y t, z_0 + s_z t)$

The radius is perpendicular to the sphere.

The shortest distance will be perpendicular to the sphere and the line.

$$d = \sqrt{(x_0+s_x t - x_c)^2 + (y_0 + s_y t - y_c)^2 + (z_0 + s_z t - z_c)^2 } - R$$

The distance is a function in one parameter $t$.

$$\frac{dd}{dt} = \frac{s_x (x_0+s_x t - x_c) + s_y (y_0 + s_y t - y_c) + s_z (z_0 + s_z t - z_c) }{\sqrt{(x_0+s_x t - x_c)^2 + (y_0 + s_y t - y_c)^2 + (z_0 + s_z t - z_c)^2 }}$$

The minimum occurs at $\displaystyle \frac{dd}{dt} = 0$

$$s_x (x_0+s_x t - x_c) + s_y (y_0 + s_y t - y_c) + s_z (z_0 + s_z t - z_c) = 0$$

$$t = \frac{s_x(x_c-x_0) + s_y(y_c-y_0) + s_z(z_c-z_0)}{s_x^2 + s_y^2 + s_z^2}$$

Calculate $d$.

The point on the sphere can be taken as a ratio along the center to the point on the line:

$$\left( x_c + \frac{(x_o + s_x t - x_c)R}{R+d} , y_c + \frac{(y_o + s_y t - y_c)R}{R+d} , z_c + \frac{(z_o + s_z t - z_c)R}{R+d} \right)$$