Distance from a point to a plane

In Euclidean space, the distance from a point to a plane is the distance between a given point and its orthogonal projection on the plane, the perpendicular distance to the nearest point on the plane.

It can be found starting with a change of variables that moves the origin to coincide with the given point then finding the point on the shifted plane $$ax + by + cz = d$$ that is closest to the origin. The resulting point has Cartesian coordinates $$(x,y,z)$$:
 * $$\displaystyle x = \frac {ad}, \quad \quad \displaystyle y = \frac {bd}, \quad \quad \displaystyle z = \frac {cd}$$.

The distance between the origin and the point $$(x,y,z)$$ is $$\sqrt{x^2+y^2+z^2}$$.

Converting general problem to distance-from-origin problem
Suppose we wish to find the nearest point on a plane to the point ($$X_0, Y_0, Z_0$$), where the plane is given by $$aX + bY + cZ = D$$. We define $$x = X - X_0$$, $$y = Y - Y_0$$, $$z = Z - Z_0$$, and $$d = D - aX_0 - bY_0 - cZ_0$$, to obtain $$ax + by + cz = d$$ as the plane expressed in terms of the transformed variables. Now the problem has become one of finding the nearest point on this plane to the origin, and its distance from the origin. The point on the plane in terms of the original coordinates can be found from this point using the above relationships between $$x$$ and $$X$$, between $$y$$ and $$Y$$, and between $$z$$ and $$Z$$; the distance in terms of the original coordinates is the same as the distance in terms of the revised coordinates.

Restatement using linear algebra
The formula for the closest point to the origin may be expressed more succinctly using notation from linear algebra. The expression $$ax+by+cz$$ in the definition of a plane is a dot product $$(a,b,c)\cdot(x,y,z)$$, and the expression $$a^2+b^2+c^2$$ appearing in the solution is the squared norm $$|(a,b,c)|^2$$. Thus, if $$\mathbf{v}=(a,b,c)$$ is a given vector, the plane may be described as the set of vectors $$\mathbf{w}$$ for which $$\mathbf{v}\cdot\mathbf{w}=d$$ and the closest point on this plane to the origin is the vector
 * $$\mathbf{p}=\frac{\mathbf{v}d}{|\mathbf{v}|^2}$$.

The Euclidean distance from the origin to the plane is the norm of this point,
 * $$\frac{|d|}{|\mathbf{v}|} = \frac{|d|}{\sqrt{a^2+b^2+c^2}}$$.

Why this is the closest point
In either the coordinate or vector formulations, one may verify that the given point lies on the given plane by plugging the point into the equation of the plane.

To see that it is the closest point to the origin on the plane, observe that $$\mathbf{p}$$ is a scalar multiple of the vector $$\mathbf{v}$$ defining the plane, and is therefore orthogonal to the plane. Thus, if $$\mathbf{q}$$ is any point on the plane other than $$\mathbf{p}$$ itself, then the line segments from the origin to $$\mathbf{p}$$ and from $$\mathbf{p}$$ to $$\mathbf{q}$$ form a right triangle, and by the Pythagorean theorem the distance from the origin to $$q$$ is
 * $$\sqrt{|\mathbf{p}|^2+|\mathbf{p}-\mathbf{q}|^2}$$.

Since $$|\mathbf{p}-\mathbf{q}|^2$$ must be a positive number, this distance is greater than $$|\mathbf{p}|$$, the distance from the origin to $$\mathbf{p}$$.

Alternatively, it is possible to rewrite the equation of the plane using dot products with $$\mathbf{p}$$ in place of the original dot product with $$\mathbf{v}$$ (because these two vectors are scalar multiples of each other) after which the fact that $$\mathbf{p}$$ is the closest point becomes an immediate consequence of the Cauchy–Schwarz inequality.

Closest point and distance for a hyperplane and arbitrary point
The vector equation for a hyperplane in $$n$$-dimensional Euclidean space $$\mathbb{R}^n$$ through a point $$\mathbf{p}$$ with normal vector $$\mathbf{a} \ne \mathbf{0}$$ is $$(\mathbf{x}-\mathbf{p})\cdot\mathbf{a} = 0$$ or $$\mathbf{x}\cdot\mathbf{a}=d$$ where $$d=\mathbf{p}\cdot\mathbf{a}$$. The corresponding Cartesian form is $$a_1x_1+a_2x_2+\cdots+a_nx_n=d$$ where $$d=\mathbf{p}\cdot\mathbf{a}=a_1p_1+a_2p_2+\cdots a_np_n$$.

The closest point on this hyperplane to an arbitrary point $$\mathbf{y}$$ is
 * $$\mathbf{x}=\mathbf{y}-\left[\dfrac{(\mathbf{y}-\mathbf{p})\cdot\mathbf{a}}{\mathbf{a}\cdot\mathbf{a}}\right]\mathbf{a}=\mathbf{y}-\left[\dfrac{\mathbf{y}\cdot\mathbf{a}-d}{\mathbf{a}\cdot\mathbf{a}}\right]\mathbf{a}$$

and the distance from $$\mathbf{y}$$ to the hyperplane is
 * $$\left\|\mathbf{x}-\mathbf{y}\right\| = \left\|\left[\dfrac{(\mathbf{y}-\mathbf{p})\cdot\mathbf{a}}{\mathbf{a}\cdot\mathbf{a}}\right]\mathbf{a}\right\|=\dfrac{\left|(\mathbf{y}-\mathbf{p})\cdot\mathbf{a}\right|}{\left\|\mathbf{a}\right\|}=\dfrac{\left|\mathbf{y}\cdot\mathbf{a}-d\right|}{\left\|\mathbf{a}\right\|}$$.

Written in Cartesian form, the closest point is given by $$x_i=y_i-ka_i$$ for $$1\le i\le n$$ where
 * $$k=\dfrac{\mathbf{y}\cdot\mathbf{a}-d}{\mathbf{a}\cdot\mathbf{a}}=\dfrac{a_1y_1+a_2y_2+\cdots a_ny_n-d}{a_1^2+a_2^2+\cdots a_n^2}$$,

and the distance from $$\mathbf{y}$$ to the hyperplane is
 * $$\dfrac{\left|a_1y_1+a_2y_2+\cdots a_ny_n-d\right|}{\sqrt{a_1^2+a_2^2+\cdots a_n^2}}$$.

Thus in $$\mathbb{R}^3$$ the point on a plane $$ax+by+cz=d$$ closest to an arbitrary point $$(x_1,y_1,z_1)$$ is $$(x,y,z)$$ given by
 * $$\left.\begin{array}{l}x=x_1-ka\\y=y_1-kb\\z=z_1-kc\end{array}\right\}$$

where
 * $$k=\dfrac{ax_1+by_1+cz_1+d}{a^2+b^2+c^2}$$,

and the distance from the point to the plane is
 * $$\dfrac{\left|ax_1+by_1+cz_1+d\right|}{\sqrt{a^2+b^2+c^2}}$$.