Finding the point on a circle closest to a line in 3-space I've read that this problem is soluble generally for any circle and any line in 3D space, although I haven't found the solution itself.
However I'm interested in a special case where the circle lies in the $z = h$ plane for some constant $h$, and the centre of the circle is $(0,0,h)$. I'm hoping this might yield a simplified solution.
Suppose we have a straight line in Cartesian $xyz$-space such that
$$ x = x_0 + v_x t, \quad \quad y = y_0 + v_y t, \quad \quad z = z_0 + v_z t$$
the circle in question is defined by
$$ x = r_0 \cos{\phi}, \quad \quad y = r_0 \sin{\phi}, \quad \quad z = h$$
I can see that we could write down a function $D(\phi, t)$ for the distance between some point on the line and some point on the circle, but it's not clear to me how to minimise this function without resorting to a numeric solution.
Does an analytic solution exist in this case?
Thanks in advance!
 A: *

*This version of the problem is no simpler than the generic version, in the sense that the generic problem can, by a distance-preserving change of coordinates (translation plus orthogonal transformation), be changed into this one. 

*You can further simplify, by allowing scaling up or down uniformly, to the case where the circle is the unit circle in the $xy$-plane. 

*In that case, it's probably easiest to write the squared distance between a point $C$ of the circle, $C = (x, y, 0)$ with $x^2 + y^2 = 1$ and a point $S(t) = P + td$ of the line (where $P$ is a point on the line, and $d$ is a direction vector) and minimize that. 
Let's look at that second problem. Instead of thinking of $C$ as being on the circle, let it be any point in space. What's the closest point $Q$ on the line that contains $P$ and has unit-vector direction-vector $d$? Answer: it's the one where $C-Q$ is perpendicular to $d$, so writing $Q = P + td$, we get
$$
[(P + td) - C] \cdot d = 0
$$
which you can solve to get
$$
t = \frac{(C-P) \cdot d}{d \cdot d} 
$$
which is just $(C-P) \cdot d$, because $d$ is a unit vector. So we can say 
$$
F(C) = P + ((C-P) \cdot d)d
$$
is the point of the line closest to $C$. Note that this is linear in the coordinates of $C$. 
Now restrict to points $(x, y)$ of the unit circle, parameterized, if you like, by $(\cos s, \sin s$. Then $F( (\cos s, \sin s) ) = L + M \cos s + N \sin s$ for some constant points/vectors $L, M, N$ that you are welcome to work out. You can compute the squared distance from this to $(\cos s, \sin s)$, which is then a quadratic in the cosine and sine. Take the derivative with respect to $s$ and set it to zero and solve, and you have found both the closest and farthest points. 
In certain special cases (like the $z$-axis, passing through the middle of the unit circle in the xy plane, the coefficients $M$ and $N$ will both be zero vectors, so the quadratic will be a constant...this corresponds to all points on that circle being equidistant from the line. 
This squared distance is 
$$
U(C, t) = \|(P-C) + td\|^2
$$
The expression on the right, for a fixed $C$, is a quadratic in $t$ and has two solutions; with a little dancing around some special cases (like the line defined by $x = y = 0$, where there isn't a "point of the circle closest to the line", because all points are equally distance) you can actually pick the $t$ that gives the smaller squared-distance. (Every "raytracer" in computer graphics does this, but with a sphere instead of a circle). And from this $t$, you can find the closest point. 
So now you have a function that takes an arbitrary point $C$ and produces the coordinates of the closest point on $L$. 
