# Is The Point lies Inside The Triangle [duplicate]

Tell if the point A(x,y) lies inside the triangle formed by the points P(a,b) , Q(c,d) , R(e,f) .

I can solve this by finding the sum of the areas of the triangles - APQ , AQR , ARP and comparing this with the area of the triangle PQR. But it takes a good amount of time (which is not good for M.C.Q based exams). Is there any cleaver way to solve this?

## marked as duplicate by Jack D'Aurizio, Shailesh, Joey Zou, Leucippus, Cyclohexanol.Aug 22 '16 at 1:15

• You can find the equations of PQ,QR,RS then determine the position of the given point with respect to these. – gambler101 Aug 21 '16 at 12:34
• I refer you to Stack Overflow! – Parcly Taxel Aug 21 '16 at 12:36
• The method you suggest is actually quite quick if you use the determinant method for finding the area. Find the area of the quadrilateral APQR and compare it to the area of the triangle. – David Quinn Aug 21 '16 at 12:40

The area of the triangle $PQR$ is $$\frac 12\left|\begin{matrix}a&c&e&a\\b&d&f&b\end{matrix}\right|$$ $$=\frac 12[(ad-bc)+(cf-ed)+(eb-af)]$$

Meanwhile the area of the quadrilateral $APQR$ is$$\frac 12\left|\begin{matrix}a&x&c&e&a\\b&y&d&f&b\end{matrix}\right|$$ $$=\frac 12[(ay-bx)+(xd-yc)+(cf-ed)+(eb-af)]$$

So if the area of the quadrilateral is greater than the area of the triangle, the point $A$ lies outside the triangle.

Note that it is the absolute value of these two expressions which must be compared.

• Actually I was unaware about the formula for quadrilateral. Thanks. – Sujan Dutta Aug 21 '16 at 13:32

Set up some coordinate system such that, in this coordinate system, the points are given by $(x_1, y_1)$, $(x_2, y_2)$, and $(x_3, y_3)$. Let the given point be "(x*, y*)". Now determine whether the given point is on the same side of each line as the third vertex:

1) Find a, b such that both $(x_1, y_1)$ and $(x_2, y_2)$ lie on line y= ax+ b (in other words, find the equation of the line determined by those two points). Determine if $y_3- ax_3- b$ and $y^*- ax^*- b$ have the same sign. If not we are done and the answer is "no, the point (x*, y*) does not lie inside this triangle. If "yes" then

2) Find a, b such that both $(x_1, y_1)$ and $(x_3, y_3)$ lie on line y= ax+ b. Determine if $y_2- ax_2- b$ and $y^*- ax^*- b$ have the same sign. If not we are done and the answer is "no", the point (x*, y*) does not lie inside this triangle. If "yes" then

3) Find a, b such that both $(x_2, y_2)$ and $(x_3, y_3)$ lie on line y= ax+ b. Determine if $y_1- ax_1- b$ and $y^*- a^x*- b$ have the same sign. If not we are done and the answer is "no", the point (x*, y*) does not lie inside this triangle. If "yes" then point (x*, y*) does lie inside the triangle.

Note: if, for i= 1, 2, or 3, $y_i- ax_i- b$ is 0 then the three points do not define a triangle. If, for any i= 1, 2, or 3, $y^*- ax^*- b$ is 0, the points lies on the triangle, not inside it.

If time is of the essence, sketch the given triangle and point first to see if you even need to investigate more closely.

If so, then one possibility is to compute the barycentric coordinates of the point. This can be done by solving the linear system $$\begin{bmatrix}a&c&e\\b&d&f\\1&1&1\end{bmatrix}\begin{bmatrix}\lambda_1\\\lambda_2\\\lambda_3\end{bmatrix}=\begin{bmatrix}x\\y\\1\end{bmatrix}.$$ Row-reduction by hand shouldn’t take very long for this small a matrix. If any of the resulting coordinates $\lambda_k$ is negative, then the point is outside of the triangle. If all are in the open interval $(0,1)$, then the point is in the interior; if it’s on a vertex, one of the coordinates will be $1$ and the other two zero; and if it’s on an edge, but not a vertex, one coordinate will be zero and the other two will lie in $(0,1)$.