Check whether a given point lies inside a triangle or not | GeeksforGeeks
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.
1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. Area A = [ x1(y2 - y3) + x2(y3 - y1) + x3(y1-y2)]/2
2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.
Math proof
Area of the Triangle Formed by Three co-ordinate Points | Area of the Triangle
http://www.mathopenref.com/coordtrianglearea.html
Read full article from Check whether a given point lies inside a triangle or not | GeeksforGeeks
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.
1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. Area A = [ x1(y2 - y3) + x2(y3 - y1) + x3(y1-y2)]/2
2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1.
3) Calculate area of the triangle PBC. Let this area be A2.
4) Calculate area of the triangle PAC. Let this area be A3.
5) If P lies inside the triangle, then A1 + A2 + A3 must be equal to A.
float
area(
int
x1,
int
y1,
int
x2,
int
y2,
int
x3,
int
y3)
{
return
abs
((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
/* A function to check whether point P(x, y) lies inside the triangle formed
by A(x1, y1), B(x2, y2) and C(x3, y3) */
bool
isInside(
int
x1,
int
y1,
int
x2,
int
y2,
int
x3,
int
y3,
int
x,
int
y)
{
/* Calculate area of triangle ABC */
float
A = area (x1, y1, x2, y2, x3, y3);
/* Calculate area of triangle PBC */
float
A1 = area (x, y, x2, y2, x3, y3);
/* Calculate area of triangle PAC */
float
A2 = area (x1, y1, x, y, x3, y3);
/* Calculate area of triangle PAB */
float
A3 = area (x1, y1, x2, y2, x, y);
/* Check if sum of A1, A2 and A3 is same as A */
return
(A == A1 + A2 + A3);
}
Math proof
Area of the Triangle Formed by Three co-ordinate Points | Area of the Triangle
http://www.mathopenref.com/coordtrianglearea.html
Since the area of a trapezium = 1/2 × the sum of the parallel sides × the perpendicular distance between them,
Hence, the area of the triangle ABC = ∆ABC
= area of the trapezium ALNC + area of the trapezium CNMB - area of the trapezium ALMB
= 1/2 ∙ (AL + NC) . LN + 1/2 ∙ (CN + BM) ∙ NM - 1/2 ∙ (AL + BM).LM
= 1/2 ∙ (y1 + y3) (x3 - x1) + 1/2 ∙ (y3 + y2) (x2 - x3) - 1/2 ∙ (y1 + y2) (x2 – x1) // easier to understand
= 1/2 ∙ [x1 y2 – y1 x2 + x2 y3 - y2 x3 + x3 y1 – y3 x1]
= 1/2 [x1 (y2 - y3) + x2 (y3 – y1) + x3 (y1 – y2)] sq. units.
http://www.crazyforcode.com/check-point-lies-triangle/Hence, the area of the triangle ABC = ∆ABC
= area of the trapezium ALNC + area of the trapezium CNMB - area of the trapezium ALMB
= 1/2 ∙ (AL + NC) . LN + 1/2 ∙ (CN + BM) ∙ NM - 1/2 ∙ (AL + BM).LM
= 1/2 ∙ (y1 + y3) (x3 - x1) + 1/2 ∙ (y3 + y2) (x2 - x3) - 1/2 ∙ (y1 + y2) (x2 – x1) // easier to understand
= 1/2 ∙ [x1 y2 – y1 x2 + x2 y3 - y2 x3 + x3 y1 – y3 x1]
= 1/2 [x1 (y2 - y3) + x2 (y3 – y1) + x3 (y1 – y2)] sq. units.
Read full article from Check whether a given point lies inside a triangle or not | GeeksforGeeks