In 3D, given three points $P_1$, $P_2$, and $P_3$ spanning a non-degenerate triangle $T$. How to determine if the projection of a point $P$ onto the plane of $T$ lies within $T$?
[Math] Determine if projection of 3D point onto plane is within a triangle
3dgeometrytriangles
Related Solutions
A common technique in a computer program is to use barycentric coordinates.
Barycentric coordinates are a lot easier to find than any web resources indicate, so I'm not linking to them.
The easiest way to obtain barycentric coordinates of a point P, given a triangle with vertices described by the vectors A, B, C is likely this method:
$ AreaABC = \frac{ \left| \overline{AB} \times \overline{AC} \right| }{ 2 } $
$ \alpha = \frac{ \left| \overline{PB} \times \overline{PC} \right| }{ 2AreaABC } $
$ \beta = \frac{ \left| \overline{PC} \times \overline{PA} \right| }{ 2AreaABC } $
$ \gamma = 1 - \alpha - \beta $
Here $\alpha$ is the ratio of the area of a subtriangle PBC over the area of the whole triangle ABC, as shown in this image from Peter Shirley's book:
If ALL of the following 4 restrictions are met:
- $ 0 \le \alpha \le 1 $
- $ 0 \le \beta \le 1 $
- $ 0 \le \gamma \le 1 $
- $\alpha + \beta + \gamma = 1$
then the point P is inside the triangle.
Note if you compute $\gamma$ as I did above (using $\gamma = 1 - \alpha - \beta$) then you don't have to check $\alpha + \beta + \gamma = 1$, but you would if you found $\gamma$ using areas (as shown in the diagram).
If ANY of $\alpha$,$\beta$,$\gamma$ are outside those ranges, or if the sum of $ \alpha + \beta + \gamma \ne 1 $ then the point P is not inside the triangle.
Note also when one of $\alpha$,$\beta$,$\gamma$ is 0, and the other 2 coordinates are between 0 and 1, the point P is on an edge of the triangle.
When one of $\alpha$,$\beta$,$\gamma$ is 1 and the other two are 0, then the point P is exactly at a vertex of the triangle.
Of course, these computations assume P is already in the plane of the triangle. If P is not in the plane of the triangle, then you should project it there first, before computing the barycentric coordinates.
You know that $$ \begin{align} P = \alpha A + \beta B + \gamma C &= \beta(B-A) + \gamma(C-A) + A\\ &= \alpha (A-B) + \gamma(C-B) + B \\ &= \alpha (A-C) + \beta(B-C) + C. \end{align} $$ We can classify the outside of the triangle in six regions:
1) $0\leq\beta$, $0\leq\gamma$, and $1\leq\beta+\gamma$, in which case the point is closest to $BC$. (In which case would it be closest to the $B$ or $C$?)
2) $0\leq\alpha$, $0\leq\gamma$, and $1\leq\alpha+\gamma$, in which case the point is closest to $AC$.
3) $0\leq\alpha$, $0\leq\beta$, and $1\leq\alpha+\beta$, in which case the point is closest to $AB$.
I'll let you work the other three cases which involve mixed signs, for example $1<\gamma$, $\beta<0$, for which the vertex $C$ is the closest point.
Best Answer
The question is a slight extension of the question given here: Check whether a point is within a 3D Triangle
There is an elegant solution to this given by W. Heidrich, Journal of Graphics, GPU, and Game Tools,Volume 10, Issue 3, 2005.
Let $\vec{u}=P_2-P_1$, $\vec{v}=P_3-P_1$, $\vec{n}=\vec{u}\times\vec{v}$, $\vec{w}=P-P_1$. We then have directly the barycentric coordinates of the projection $P'$ of $P$ onto $T$ as
The coordinates of the projected point is
The point $P'$ lies inside $T$ if