Adobe Interview Question
Software Engineer / DevelopersOne more way...
Find the area of triangle and three triangle's joining three point's taking two point from the triangle and one given point... if the area of original triangle is equal to sum of all three triangle then it is inside the triangle, if any one smaller triangle area is zero the on the triangle else outside..
If the point is inside the triangle, then the line connecting one vertex with the point should intersect the line connecting the other two vertexes. Check this property for all three vertexes. If all hold, then the point lies inside.
one more point to be noted that both the points(vertex and the given point) will lie on the same side of the intersecting edge of the triangle.
make one point as a origin such as both point lies at positive side of x axis... then for the new co-ordinate... put the point in the equation of line joining the three point's..
for origin to first and second point it should be positive and negative... or vice versa..
less then zero for line joining the non zero two points..
then its inside the triangle...
otherwise no
<pre lang="" line="1" title="CodeMonkey51728" class="run-this">#include<iostream.h>
#include<conio.h>
#include<math.h>
#define cosi(x1,y1,x2,y2) acos((x1*x2+y1*y2)/sqrt((pow(x1,2)+pow(y1,2))*(pow(x2,2)+pow(y2,2))))
void main()
{
clrscr();
int i,f;
char ch='c';
double q1,q2,q3,p[3][2],p1[2],a1,a2;
cout<<"enter the coordinates of triangle\n";
for(i=0;i<3;i++)
cout<<"\rx=",cin>>p[i][0],cout<<" y=",cin>>p[i][1];
do
{
clrscr();
cout<<"\nenter any point";
cout<<"x=",cin>>p1[0],cout<<" y=",cin>>p1[1];
i=0;
f=0;
while(i<3)
{
if(p1[0]==p[i][0]&&p1[1]==p[i][1])
{
f=1;
break;
}
i++;
}
if(f==0)
{
for(i=0;i<3;i++)
{
f=1;
a1=1;
a2=-1;
if(i==1)
a2=2;
else
if(i==3)
a1=-2;
q1=cosi((p[i][0]-p[i+a1][0]),(p[i][1]-p[i+a1][1]),(p[i][0]-p[i+a2][0]),(p[i][1]-p[i+a2][1]));
q2=cosi((p[i][0]-p1[0]),(p[i][1]-p1[1]),(p[i][0]-p[i+a1][0]),(p[i][1]-p[i+a1][1]));
q3=cosi((p[i][0]-p1[0]),(p[i][1]-p1[1]),(p[i][0]-p[i+a2][0]),(p[i][1]-p[i+a2][1]));
if(abs(q1)<(abs(q2)+abs(q3)))
{
f=0;
break;
}
}
}
if(f==1)
cout<<"\ninside the triangle";
else
cout<<"\noutside the triangle";
cout<<"\n\n\n press q to quit";
ch=getch();
}while(ch!='q');
getch();
}
</pre><pre title="CodeMonkey51728" input="yes">
</pre>
Algo:
1. take first line (by vertices 1 and 2) and solving it with given point should give same sign as with vertex 3.
2. take second line (by vertices 2 and 3) and solving it with given point should give same sign as with vertex 1.
3. take third line (by vertices 3 and 1) and solving it with given point should give same sign as with vertex 2.
if all of the above is satisfied line is in the triangle otherwise its not.
Take one vertex of a triangle and one of the lines connecting to it. Draw a line from vertex to point you want to scrutinize. Check the angle between two lines and compare to angle of the triangle for that vertix. Repeat for other vertices. If the angle with the point is smaller than angle of the triangle for all three vertices, point is inside the triangle.
Suppose ABC is triangle and P is given point.
- mag February 19, 2012Step 1. Find Area(PAB), Area(PBC), Area(PCA)
Step 2. If all three areas are of same sign (irrespective of positive or negative), then P lies inside ABC, otherwise outside
Note: Better than Sanjay's approach because it doesn't calculate area of whole triangle
To see how it works:
1. When you find area of PAB, and if it is positive, it means you are standing at P and A, B are clockwise from P.
2. Similar argument holds for PBC and PCA.
3. If all three areas are positive then you are standing at P and looking at A, B and C in clockwise direction, if all are negative then anti-clockwise.
4. If there is a sign change, you (P) are outside triangle ABC.
5. If any of the area is zero, then P is one of the edges AB, BC or CA