Given the coordinates of the three vertices of a triangleĀ (x1, y1),Ā (x2, y2), andĀ (x3, y3), along with a pointĀ P(x, y).Ā Ā FindĀ if the point P lies inside the triangle or on its boundary.
Examples:
Input: x1 = 0, y1 = 0, x2 = 20, y2 = 0, x3 = 10, y3 = 30, x = 10, y = 15. Output: true Explanation: The point (x, y) lies within the Triangle.
Input: x1 = 0, y1 = 0, x2 = 20, y2 = 20, x3 = 20, y3 = 0, x = 30, y = 0. Output: false Explanation: The point (x, y) doesn't lie within the Triangle.
[Naive Approach] Using Area of Triangle - O(1) Time and O(1) Space
The idea is to calculate the area of the given triangle and compare it with the sum of the areas of the three triangles formed using the given point.
If both areas are equal, the point lies inside or on the boundary of the triangle.
Working of Approach:
Compute the area of triangle ABC.
Compute the areas of triangles PAB, PBC, and PCA.
Add the three smaller triangle areas.
If the sum equals the area of ABC, return true.
Otherwise, return false.
C++
#include<bits/stdc++.h>usingnamespacestd;// Function to calculate twice the area of a triangle.intarea(intx1,inty1,intx2,inty2,intx3,inty3){returnabs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));}boolisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Calculate area of the original triangle.intA=area(x1,y1,x2,y2,x3,y3);// Calculate areas of the three sub-triangles.intA1=area(x,y,x2,y2,x3,y3);intA2=area(x1,y1,x,y,x3,y3);intA3=area(x1,y1,x2,y2,x,y);// Point lies inside or on the boundary if areas match.return(A==A1+A2+A3);}intmain(){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;cout<<(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");return0;}
Java
importjava.util.Arrays;// Function to calculate twice the area of a triangle.publicclassGFG{publicstaticintarea(intx1,inty1,intx2,inty2,intx3,inty3){returnMath.abs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));}publicstaticbooleanisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Calculate area of the original triangle.intA=area(x1,y1,x2,y2,x3,y3);// Calculate areas of the three sub-triangles.intA1=area(x,y,x2,y2,x3,y3);intA2=area(x1,y1,x,y,x3,y3);intA3=area(x1,y1,x2,y2,x,y);// Point lies inside or on the boundary if areas match.return(A==A1+A2+A3);}publicstaticvoidmain(String[]args){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;System.out.println(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");}}
Python
frommathimportfabs# Function to calculate twice the area of a triangle.defarea(x1,y1,x2,y2,x3,y3):returnfabs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))defisInsideTri(x1,y1,x2,y2,x3,y3,x,y):# Calculate area of the original triangle.A=area(x1,y1,x2,y2,x3,y3)# Calculate areas of the three sub-triangles.A1=area(x,y,x2,y2,x3,y3)A2=area(x1,y1,x,y,x3,y3)A3=area(x1,y1,x2,y2,x,y)# Point lies inside or on the boundary if areas match.return(A==A1+A2+A3)if__name__=='__main__':x1=0y1=0x2=20y2=0x3=10y3=30x=10y=15print('true'ifisInsideTri(x1,y1,x2,y2,x3,y3,x,y)else'false')
C#
usingSystem;// Function to calculate twice the area of a triangle.publicclassGFG{publicstaticintarea(intx1,inty1,intx2,inty2,intx3,inty3){returnMath.Abs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));}publicstaticboolisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Calculate area of the original triangle.intA=area(x1,y1,x2,y2,x3,y3);// Calculate areas of the three sub-triangles.intA1=area(x,y,x2,y2,x3,y3);intA2=area(x1,y1,x,y,x3,y3);intA3=area(x1,y1,x2,y2,x,y);// Point lies inside or on the boundary if areas match.return(A==A1+A2+A3);}publicstaticvoidMain(){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;Console.WriteLine(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");}}
JavaScript
// Function to calculate twice the area of a triangle.functionarea(x1,y1,x2,y2,x3,y3){returnMath.abs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));}functionisInsideTri(x1,y1,x2,y2,x3,y3,x,y){// Calculate area of the original triangle.letA=area(x1,y1,x2,y2,x3,y3);// Calculate areas of the three sub-triangles.letA1=area(x,y,x2,y2,x3,y3);letA2=area(x1,y1,x,y,x3,y3);letA3=area(x1,y1,x2,y2,x,y);// Point lies inside or on the boundary if areas match.return(A==A1+A2+A3);}// Driver Codeletx1=0,y1=0;letx2=20,y2=0;letx3=10,y3=30;letx=10,y=15;console.log(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");
Output
true
[Expected Approach] Using Cross Product - O(1) Time and O(1) Space
The idea is to use the cross product to determine the side of each triangle edge on which the given point lies.
If the point lies on the same side of all three edges (or on an edge), it is inside or on the boundary of the triangle.
Working of Approach:
Compute the cross product for the point with each triangle edge.
Check the sign of all three cross products.
If all are positive or all are negative (zeros are allowed), the point is inside.
If both positive and negative values are present, the point is outside.
Return the corresponding result.
Let us understand with an example: Input: x1 = 0, y1 = 0, x2 = 20, y2 = 0, x3 = 10, y3 = 30, x = 10, y = 15.
Compute the cross product of point P(10, 15) with each triangle edge: AB, BC, and CA.
The obtained cross products are 300, 150, and 150, respectively.
Since all three cross products are positive, the point lies on the same side of every edge.
Therefore, the point lies inside the triangle.
Hence, the output is true.
C++
#include<bits/stdc++.h>usingnamespacestd;// Cross product of vectors AB and AP to determine// which side of edge AB the point P lies on.intcrossVal(intax,intay,intbx,intby,intpx,intpy){return(bx-ax)*(py-ay)-(by-ay)*(px-ax);}boolisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Compute signed area (cross product) for P w.r.t each edgeintc1=crossVal(x1,y1,x2,y2,x,y);intc2=crossVal(x2,y2,x3,y3,x,y);intc3=crossVal(x3,y3,x1,y1,x,y);// If P is on the same side of all edges (all +ve, all -ve,// or zero), it lies inside or on the boundary of the triangleboolhasPos=(c1>0)||(c2>0)||(c3>0);boolhasNeg=(c1<0)||(c2<0)||(c3<0);return!(hasPos&&hasNeg);}intmain(){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;cout<<(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");return0;}
Java
publicclassGFG{// Cross product of vectors AB and AP to determine// which side of edge AB the point P lies on.publicstaticintcrossVal(intax,intay,intbx,intby,intpx,intpy){return(bx-ax)*(py-ay)-(by-ay)*(px-ax);}publicstaticbooleanisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Compute signed area (cross product) for P w.r.t each edgeintc1=crossVal(x1,y1,x2,y2,x,y);intc2=crossVal(x2,y2,x3,y3,x,y);intc3=crossVal(x3,y3,x1,y1,x,y);// If P is on the same side of all edges (all +ve, all -ve,// or zero), it lies inside or on the boundary of the trianglebooleanhasPos=(c1>0)||(c2>0)||(c3>0);booleanhasNeg=(c1<0)||(c2<0)||(c3<0);return!(hasPos&&hasNeg);}publicstaticvoidmain(String[]args){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;System.out.println(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");}}
Python
defcrossVal(ax,ay,bx,by,px,py):# Cross product of vectors AB and AP to determine# which side of edge AB the point P lies on.return(bx-ax)*(py-ay)-(by-ay)*(px-ax)defisInsideTri(x1,y1,x2,y2,x3,y3,x,y):# Compute signed area (cross product) for P w.r.t each edgec1=crossVal(x1,y1,x2,y2,x,y)c2=crossVal(x2,y2,x3,y3,x,y)c3=crossVal(x3,y3,x1,y1,x,y)# If P is on the same side of all edges (all +ve, all -ve,# or zero), it lies inside or on the boundary of the trianglehasPos=(c1>0)or(c2>0)or(c3>0)hasNeg=(c1<0)or(c2<0)or(c3<0)returnnot(hasPosandhasNeg)if__name__=="__main__":x1,y1=0,0x2,y2=20,0x3,y3=10,30x,y=10,15print("true"ifisInsideTri(x1,y1,x2,y2,x3,y3,x,y)else"false")
C#
usingSystem;publicclassGFG{// Cross product of vectors AB and AP to determine// which side of edge AB the point P lies on.publicstaticintcrossVal(intax,intay,intbx,intby,intpx,intpy){return(bx-ax)*(py-ay)-(by-ay)*(px-ax);}publicstaticboolisInsideTri(intx1,inty1,intx2,inty2,intx3,inty3,intx,inty){// Compute signed area (cross product) for P w.r.t each edgeintc1=crossVal(x1,y1,x2,y2,x,y);intc2=crossVal(x2,y2,x3,y3,x,y);intc3=crossVal(x3,y3,x1,y1,x,y);// If P is on the same side of all edges (all +ve, all -ve,// or zero), it lies inside or on the boundary of the triangleboolhasPos=(c1>0)||(c2>0)||(c3>0);boolhasNeg=(c1<0)||(c2<0)||(c3<0);return!(hasPos&&hasNeg);}publicstaticvoidMain(){intx1=0,y1=0;intx2=20,y2=0;intx3=10,y3=30;intx=10,y=15;Console.WriteLine(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");}}
JavaScript
functioncrossVal(ax,ay,bx,by,px,py){// Cross product of vectors AB and AP to determine// which side of edge AB the point P lies on.return(bx-ax)*(py-ay)-(by-ay)*(px-ax);}functionisInsideTri(x1,y1,x2,y2,x3,y3,x,y){// Compute signed area (cross product) for P w.r.t each// edgeletc1=crossVal(x1,y1,x2,y2,x,y);letc2=crossVal(x2,y2,x3,y3,x,y);letc3=crossVal(x3,y3,x1,y1,x,y);// If P is on the same side of all edges (all +ve, all// -ve, or zero), it lies inside or on the boundary of// the trianglelethasPos=(c1>0)||(c2>0)||(c3>0);lethasNeg=(c1<0)||(c2<0)||(c3<0);return!(hasPos&&hasNeg);}// Driver Codeletx1=0,y1=0;letx2=20,y2=0;letx3=10,y3=30;letx=10,y=15;console.log(isInsideTri(x1,y1,x2,y2,x3,y3,x,y)?"true":"false");