**Derivation**

We're given an existing line segment AB in space and a point C. We want to find a point P on line AB such that line segment CP is perpendicular to AB. (We won't worry about whether it is on the line segment AB, just on the line AB; we omit a "range check".) We observe that the perpendicular distance between a point and a line is the shortest possible distance and so we will treat this as a minimization problem, using the methods of calculus. Let A = (A

_{x}, A

_{y}, A

_{z}) and similar for points B and C. Then line AB is described by the parametric equations:

x(t) = A

_{x}+ (B

_{x}– A

_{x}) t,

y(t) = A

_{y}+ (B

_{y}– A

_{y}) t,

z(t) = A

where P(t) = (x(t), y(t), z(t)). We want to find P(t) such that line segment CP(t) is as small as possible, which is equivalent to finding when the square of the distance is minimized. So, we define

D(t) = (C

= (C

Taking the derivative yields:

D'(t) = 2 (C

We solve for t when D'(t) = 0 as follows:

0 = (C

[(B

t = [(C

which can be stated more succinctly in vector notation as

\[t = \frac{(B-A)\cdot (C-A)}{(B-A)\cdot (B-A)}\]

from whence derives the below Maxima code:_{z}+ (B_{z}– A_{z}) t,where P(t) = (x(t), y(t), z(t)). We want to find P(t) such that line segment CP(t) is as small as possible, which is equivalent to finding when the square of the distance is minimized. So, we define

D(t) = (C

_{x }– x(t))^{2}+ (C_{y }– y(t))^{2}+ (C_{z }– z(t))^{2}= (C

_{x}– A_{x}– (B_{x}– A_{x}) t)^{2}+ (C_{y}– A_{y}– (B_{y}– A_{y}) t)^{2}+ (C_{z}– A_{z}– (B_{z}– A_{z}) t)^{2}.Taking the derivative yields:

D'(t) = 2 (C

_{x}– A_{x}– (B_{x}– A_{x}) t) (– (B_{x}– A_{x})) + 2 (C_{y}– A_{y}– (B_{y}– A_{y}) t) (– (B_{y}– A_{y})) +2 (C_{z}– A_{z}– (B_{z}– A_{z}) t) (– (B_{z}– A_{z}))We solve for t when D'(t) = 0 as follows:

0 = (C

_{x}– A_{x}– (B_{x}– A_{x}) t) (B_{x}– A_{x}) + (C_{y}– A_{y}– (B_{y}– A_{y}) t) (B_{y}– A_{y}) + (C_{z}– A_{z}– (B_{z}– A_{z}) t) (B_{z}– A_{z})[(B

_{x}– A_{x})^{2}+ (B_{y}– A_{y})^{2}+ (B_{z}– A_{z})^{2}] t = (C_{x}– A_{x})(B_{x}– A_{x}) + (C_{y}– A_{y})(B_{y}– A_{y}) + (C_{z}– A_{z})(B_{z}– A_{z})t = [(C

_{x}– A_{x})(B_{x}– A_{x}) + (C_{y}– A_{y})(B_{y}– A_{y}) + (C_{z}– A_{z})(B_{z}– A_{z})]/[(B_{x}– A_{x})^{2}+ (B_{y}– A_{y})^{2}+ (B_{z}– A_{z})^{2}],which can be stated more succinctly in vector notation as

\[t = \frac{(B-A)\cdot (C-A)}{(B-A)\cdot (B-A)}\]

To prove that the point thus obtained is indeed the desired "Snap Perpendicular" point, it would be sufficient to show that the dot product of the vectors (A – B) and (C – P) is equal to zero, which check I omit.