Distance from a point to a plane (source code)

= Distance from a point to a plane
{title2=$D=|\mathbf n\cdot\mathbf p-d|/\|\mathbf n\|$}
{wiki}

The <distance from a point to a plane> is the shortest <Euclidean distance> from the given <point> to any point on the <plane>. For $P=\{\mathbf x:\mathbf n\cdot\mathbf x=d\}$ with nonzero <normal vector> $\mathbf n$, a point $\mathbf p$ has
$$
\boxed{D=\frac{|\mathbf n\cdot\mathbf p-d|}{\|\mathbf n\|}.}
$$
The numerator is the residual of the <plane equation>, not yet a <length>: its size depends on the chosen <normal vector>. Dividing by that vector's <Euclidean norm> removes this arbitrary scale.

For the original example $P:2x-y+2z=3$ and $\mathbf p=(4,1,2)$, the <normal vector> is $\mathbf n=(2,-1,2)$ and $\|\mathbf n\|=3$. The residual is $8-1+4-3=8$, so \b[the distance is $8/3$]. Using the equivalent <plane equation> $-6x+3y-6z=-9$ instead gives numerator $24$ and <Euclidean norm> $9$, hence the same $8/3$. A point already on the <plane> has zero residual and zero distance.

Why is the perpendicular route shortest? Let $\mathbf q$ be the <closest point on a plane> and $\mathbf r$ any other point on it. The displacement $\mathbf p-\mathbf q$ is <orthogonal> to $\mathbf q-\mathbf r$, so the <Pythagorean theorem> gives
$$
\|\mathbf p-\mathbf r\|^2=\|\mathbf p-\mathbf q\|^2+\|\mathbf q-\mathbf r\|^2\geq\|\mathbf p-\mathbf q\|^2.
$$
Equality holds only for $\mathbf r=\mathbf q$. This is the closest-point principle of <orthogonal decomposition>, applied after a <translation> of an affine <plane> to its direction <linear subspace>.<Interactive Linear Algebra by Dan Margalit and others/Orthogonal Projection>[Interactive Linear Algebra, §6.3.1: Orthogonal decomposition theorem, Closest vector and distance]{ref}

\Image[/plane-perpendicular-distance-and-equation-rescaling.png]
{title=The perpendicular route to a <plane> is shortest, and rescaling its <plane equation> preserves the distance}
{description=Original cross-section using the worked distance $8/3$. The comparison point lies two length units along the <plane> from the <closest point on a plane>, giving a longer route of $10/3$. The second panel reverses the <unit normal> without moving the <plane>.}

The formula is useful whenever the deviation from a flat reference should be measured geometrically. A raw <linear equation> residual cannot be compared across arbitrary equation scalings; a perpendicular distance can. The same reasoning works for a <hyperplane> in any finite-dimensional <Euclidean space>, with the appropriate number of coordinates.