2
$\begingroup$

Given the points of the vertices of a triangles as tuples (x,y) and a point P=(x,y).

How can I determine if this point P is contained in the triangle (assume that it's not on the border of the triangle)?

$\endgroup$
3

2 Answers 2

1
$\begingroup$

Given three non-collinear points in $\mathbb{R}^2$ (the vertices of a triangle) $A,B,C$ and a point $P$, there is a unique way to represent $P$ as $$ P=\lambda_A A + \lambda_B B + \lambda_C C $$ with $\lambda_A,\lambda_B,\lambda_C$ being real coefficients fulfilling $\lambda_A+\lambda_B+\lambda_C=1$. This kind of representation is also known as exact barycentric coordinates. The coefficients $\lambda_A,\lambda_B,\lambda_C$ are straightforward to find through linear algebra and the point $P$ strictly lies on the interior of $ABC$ iff $$\lambda_A>0,\quad\lambda_B>0,\quad\lambda_C>0,$$ that is equivalent to $$ \begin{pmatrix}1 & 1 & 1 \\ x_A & x_B & x_C \\ y_A & y_B & y_C\end{pmatrix}^{-1} \begin{pmatrix}1 \\ x_P \\ y_P\end{pmatrix}>0. $$

An equivalent alternative, assuming that $A,B,C$ are counter-clockwise ordered, is to compute the (oriented) areas of $ABP,BCP,CAP$ through the shoelace formula and check that all these (oriented) areas are positive.

$\endgroup$
0
$\begingroup$

When the point is inside the triangle, the algebraic areas of the three triangles it forms with the three edges have the same sign. These areas are obtained as simple determinants https://en.wikipedia.org/wiki/Triangle#Using_coordinates (without the absolute values).

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .