# Shortest distance between point and surface

I wonder if there exists a good enough formula to compute the shortest distance between a point $P=(x_0,y_0,z_0)$ and a surface $\pi$ defined by $F(x,y,z)=0$. There is a lot of simmilar questions in this site but any of those concern a general surface.

In particular, if we assume that the 'shortest' vector from $Q$ to $\pi$ is always normal to $\pi$ then, if $Q=(a,b,c)$ is the closest point in $\pi$ to $P$, there exists a real number $\alpha$ such that $$P-Q=\alpha\left(\vec\nabla F(Q)-Q\right)$$ where $\vec\nabla F(Q)$ is $\left<\frac{\partial F}{\partial x},\frac{\partial F}{\partial y},\frac{\partial F}{\partial z}\right>$ at point $Q$. Then if our main assumption is correct, the distance is $\|P-Q\|$, but we still have to compute $\alpha$. This is maybe not the correct way to proceed, so i would appreciate some hints. Thanks in advance.

## 3 Answers

The problem is a little simpler when you know some parametric equation of the surface,

$$Q=Q(u,v),\text{ with }(u,v)\in D$$ for some domain $D$.

You need to minimize $PQ^2$, which by differentiation yields the system

$$(Q(u,v)-P)\cdot\frac{\partial Q(u,v)}{\partial u}=0,\\ (Q(u,v)-P)\cdot\frac{\partial Q(u,v)}{\partial v}=0.$$

This is still nonlinear, but the complexity dropped to two equations in two unknowns.

A simple example:

Let us consider a sphere of radius one at the origin. In spherical coordinates, with an (hopefully) obvious notation $$Q=(c_u s_v, s_u s_v, c_v),$$ and a point on the $z$ axis $$P=(0,0,p).$$

The minimization equations are $$(c_u s_v)(-s_u s_v) + (s_u s_v)(c_u s_v) + (c_v - p) (0) = 0,\\ (c_u s_v)(c_u c_v) + (s_u s_v)(s_u c_v) + (c_v - p) (-s_v) = 0.$$ and simplify as $$0=0,\\s_vp=0.$$

Then $s_v=0,c_v=\pm1$, $u$ can take any value and $$Q_{min}=(0,0,\pm1)$$ where the sign is taken to get the closest point from $P$.

$$d=\min(|p-1|,|p+1|).$$

Note that the case of $p=0$ is singular, as $s_v$ can take any value and $P$ is equidistant to the whole surface.

• Nice explanation. It took me some time to understand the first equation in your example! :D Why did you not just use $\sin(u)$ and $\cos(u)$? :) – Hosein Rahnama Oct 27 '15 at 17:52
• @H.R.: these are convenient shorthands of the same "cost" as $e^u$ for $\exp(u)$. – user65203 Apr 29 '16 at 6:45

First of all, the shortest distance problem and the perpendicularity problem are not necessarily the same. However, I guess that you are looking for the perpendicularity problem. In fact, you are asking to obtain a general closed form formula for a system of nonlinear algebraic equations which is at most cases an impossible task to carry out. Let me show you what I mean exactly by this! Suppose the implicit equation of a surface is given by

$$F({\bf{X}}) = 0\tag{1}$$

we are given a particular point ${{\bf{X}}_P}$, outside the surface, that we wish to find its distance form the surface. In fact, we are searching for some unknown point ${{\bf{X}}_Q}$ on the surface such that the vector ${{\bf{X}}_P} - {{\bf{X}}_Q}$ is perpendicular to the surface at ${{\bf{X}}_Q}$. If I want to write down these words in terms of equations it would be

$$\left\{ \begin{array}{l} {{\bf{X}}_P} - {{\bf{X}}_Q} = \alpha \nabla F({{\bf{X}}_Q})\\ F({{\bf{X}}_Q}) = 0 \end{array} \right.\tag{2}$$

where it is a system of nonlinear algebraic equations. System $(2)$ consists of 4 scalar equations in 4 scalar unknowns. The first vectorial equation includes 3 scalar equations and the second one is just a scalar equation and hence 4 scalar equations. The four unknowns are the three components of the vector ${{\bf{X}}_Q}$ and the scalar $\alpha$.

Can you find a closed form solution for $(2)$ in general?
I am afraid NOT! :)

• This is among standard problems in calculus of variations as isoperimetric problems. – Narasimham Oct 25 '15 at 21:24
• You can do it in the context of vector analysis too! Can you give him a closed form solution!? I don't think so! If you can, then write it down! :) – Hosein Rahnama Oct 25 '15 at 21:27

I am sure this is not useful to you, but I cite it anyway. This describes a MatLab computational solution.

(Image from DougHull.)