Minimum distance between point and face Given a point in 3D space of the form (x, y, z) and a triangle consisting of 3 vectors (also in the (x, y, z) format), how would I calculate the minimum distance between the point and the face of the triangle?
I understand it involves the normal of the plane that the triangle lies on however I'm unsure how to calculate the plane and then the magnitude of the plane's normal, to the point.
Thanks for any help!
 A: A good way to solve it is using vectors:
First we have to find the normal to the triangle. Let's say the triangle is defined by vectors $v_1, v_2, v_3$ and our point is $p$
Then to calculate normal vector, just do:
$$ n = (v_2 - v_1) * ( v_3 - v_1 ) $$ (where '*' denotes cross product)
and normalize it:
$$ n_n = \frac{n}{||n||} $$
Now we have to find the intersection between the line with direction $ n_n$ passing through $ p $ and the plane of the triangle.
The plane of the triangle is defined by:
$$  n_n · x = n_n · v_1 $$ for each point $ x $
and the line passing through $ p $ and direction $ n_n $ is:
$$ x = p + t·n_n $$
so the intersection point must satisfy:
$$ n_n · (p + t·n_n ) = n_n · v_1 $$
simplifying:
$$ n_n·p + t = n_n·v_1 $$
and:
$$ t = n_n·v_1 - n_n·p $$
So, the intersection point with the plane is:
$$ p_0 = p + t·n_n $$
and the distance:
$$ \text{distance} = || p-p_0 || $$
Now we need to know if the intersection point lies inside our triangle. To do this we can use barycentric coordinates. In the point $ p_0 $ lies inside the triangle its barycentric coordinates must lie in the interval $ [0,1] $.
We can determine this point just doing:
$$ α·v_1 + \beta·v_2 + \gamma·v_3 = p_0 $$
and solving the system for $ α, \beta $ and $ \gamma $. They all must lie in the $[0,1]$ interval.
EDIT ---------------------
As the comment from  Samuel Danielson states, if some of the barycentric coordinates is not in the $ [0,1] $ interval, it means that the point $p0$ found is the point of the plane containing the triangle with the minimum distance to the point $p$. This means that the point in the triangle with minimum distance to $p$ lies in one vertex or one side of the triangle.
So, in case that some of the barycentric are outside the $ [0,1] $ interval, we can find which is the point (in one edge or in one vertex) of the triangle nearest to $p$:
Look at the sign of $ α, \beta $ and $ \gamma $. If only one of them is positive, then the nearest point is the corresponding vertex:
$v_1$ if the only positive is $ α $
$v_2$ if the only positive is $ \beta $
$v_3$ if the only positive is $ \gamma $
and in case there are two positive coordinates and only one negative, this means that the nearest point lies in the corresponding edge:
$v_1, v_2$ if the only negative is $ \gamma $
$v_1, v_3$ if the only negative is $ \beta $
$v_2, v_3$ if the only negative is $ α $
In that case you have to find the point in the segment (edge) nearest to $p$
A: without going into all the gory detail, maybe i can outline one approach to the problem which may assist you in developing your own understanding of the situation. the method can also easily be applied to a hyperplane in a higher-dimensional space.
if your vectors are $v_1, v_2, v_3$ then a point in the plane determined by their endpoints must have the form
$$ \alpha v_1 + \beta v_2 + \gamma v_3$$
with the additional condition that
$$ \alpha + \beta +\gamma = 1$$
the square of the distance of a vector's endpoint from the origin is given by the scalar product of the vector with itself. we can attempt therefore to minimize this squared distance. using Lagrange's technique, we attempt to find a value of $\lambda$ so that the three derivatives of the expression:
$$ L(\lambda) = (\alpha v_1 + \beta v_2 + \gamma v_3)^2 - \lambda(\alpha + \beta +\gamma) $$
with respect to $\alpha, \beta, \gamma $ all vanish. the squaring here signifies a scalar product, so it is convenient to introduce the constants $k_{ij}$ where
$$ k_{ij} = v_i . v_j$$
now 
$$ \frac{\partial L}{\partial \alpha} = 2v_1 .  (\alpha v_1 + \beta v_2 + \gamma v_3) - \lambda \\
= 2(k_{11} \alpha + k_{12} \beta + k_{13} \gamma) -  \lambda
$$
setting this equal to zero, and likewise for the other two partial derivatives, then together with the condition $ \alpha + \beta +\gamma = 1$ this will give you a system of four linear equations in the four unknowns $\alpha, \beta, \gamma$ and $\lambda$, and solving this system will give you the coordinates of the point where the normal drawn through the origin meets the plane determined by the vertices of your triangle.
A: If you need to implement the code for JordiC's method and are only interested in the distance: since n-vector is normalized and if projection lies inside the triangle, |t| is the euclidian distance. No need to calculate ||P-P0||.
A: Jordi Cruzado's edit breaks down when dealing with obtuse triangles. This answer builds on Jordi Cruzado's answer to allow it to be applied to obtuse triangles. References to "the figure" refer to this figure.

The Problem
In Jordi Cruzado's approach, the region of the plane outside the triangle is broken into six sections. See the top panel of the figure. In these regions, $\alpha$, $\beta$, and $\gamma$ obey the following inequalities, which can be used to test which region $p_0$ is in:


*

*I: $\alpha \geq 0$, $\beta \geq 0$, $\gamma < 0$

*II: $\alpha < 0$, $\beta \geq 0$, $\gamma < 0$

*III: $\alpha < 0$, $\beta \geq 0$, $\gamma \geq 0$

*IV: $\alpha < 0$, $\beta < 0$, $\gamma \geq 0$

*V: $\alpha \geq 0$, $\beta < 0$, $\gamma \geq 0$

*VI: $\alpha \geq 0$, $\beta < 0$, $\gamma < 0$


According to Jordi Cruzado's edit, when $p_0$ is in regions I, III, and V, it is closest to the edge formed by $v_1$ and $v_2$, $v_2$ and $v_3$, and $v_1$ and $v_3$, respectively. When $p_0$ is in regions II, IV, and VI, it is closest to $v_2$, $v_3$, and $v_1$, respectively.
In the top panel of the figure, gray boxes represent the regions of the plane that are actually closest to the edges of the triangle. Two of each box's edges are rays perpendicular to the triangle's edges starting at the triangle's vertices. Using these boxes, you can see that portions of region II are actually closer to edges of the triangle than they are to $v_2$.

The Solution
To avoid this error, you can bisect regions II, IV, and VI. See the bottom panel of the figure. To determine which subregion $p_0$ is in, you can use the following inequalities, which also tell you which edge is closest to $p_0$:


*

*IIa: $\gamma \leq \alpha$ - Edge $v_1$, $v_2$

*IIb: $\gamma \geq \alpha$ - Edge $v_2$, $v_3$

*IVa: $\alpha \leq \beta$ - Edge $v_2$, $v_3$

*IVb: $\alpha \geq \beta$ - Edge $v_1$, $v_3$

*VIa: $\beta \leq \gamma$ - Edge $v_1$, $v_3$

*VIb: $\beta \geq \gamma$ - Edge $v_1$, $v_2$


Once you have determined which edge $p_0$ is closest to, you can determine which point within the triangle edge $p$ is closest to, and you can calculate the distance from this point to $p$, as in Jordi Cruzado's original answer. 
I'm sure there are more elegant solutions to this problem, but this approach will work.
