C program to check whether a point lies inside a triangle

In summary, the conversation discusses different methods for determining whether a fourth coordinate lies inside a triangle. One method involves finding the area of the triangle and comparing it to the areas of three smaller triangles formed by the fourth coordinate. However, this method may fail when using floating point integers. Another method involves using equations to determine if the fourth coordinate lies on the same side as the triangle. However, this method may also give a false positive when the fourth coordinate lies along an extended line of the triangle. A suggestion is made to combine both methods by comparing the areas of the original triangle and the triangle formed with the new point.
  • #1
Gagan A
20
0
Ok the question goes as:
Take four co-ordinates and check whether the fourth coordinate lies inside the triangle. I first did it with the area concept, that is, find the area of the whole triangle, then find the area of the three trangles formed by the fourth coordinate. if the sum of the other three areas comes equal to the total area then the point lies inside the triangle. But after successfull compilation i realized that my program will fail if there are floating point integers. So I did it another way.

#include<stdio.h>
int main()
{
float a,b,c,d,e,f,g,h,k,l,m,n,o,p; //(a,b),(c,d),(e,f),(g,h) are coordinates. Others are variables.
scanf("%f%f%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f,&g,&h); //Take input.

k = e*(b-d) + f*(c-a) + a*d - b*c;
l = g*(b-d) + h*(c-a) + a*d - b*c;

m = a*(f-d) + b*(c-e) + e*d - f*c;
n = g*(f-d) + h*(c-e) + e*d - f*c;

o = c*(b-f) + d*(e-a) + a*f - b*e;
p = g*(b-f) + h*(e-a) + a*f - b*e;

if (l==0 || n==0 || p==0) printf("YES\n"); // If the point lies on side of triangle. I think this needs some modification.
else if ((k/l>=0) && (m/n>=0) && (o/p>=0)) printf("YES\n");
else printf("NO\n");

return 0;
}


This is based on the concept that if the fourth coordinnate and the coordinates of the triangle lie on the same side the answer wil be yes. For that, find the equation of line passing through (a,b) and (c,d), Simplify the equation to form l = 0, and then put (e,f) and (g,h).
But the problem I am facing is that when the point lies in line of the triangle (you extend a line of the triangle and the point is on the extended portion) the answer still comes to be yes. I don't want that. Could someone please tell me how can I do that?
 
Last edited:
Technology news on Phys.org
  • #2
combine your original way witht the existing one - if you got a yes answer - compare the areas of the original triangle and the triangle formed with the new point, and if the new area is bigger, the point is somewhere along the extended lines...
 
  • #3
if your not to lazy see if your library has a book by Joseph O'rourke titled
Computational Geometry.
 

FAQ: C program to check whether a point lies inside a triangle

What is the purpose of a C program to check whether a point lies inside a triangle?

The purpose of this program is to determine if a given point lies inside a triangle, using the coordinates of the triangle's vertices and the given point. This can be useful in various applications, such as computer graphics, geometry calculations, and game development.

How does the C program determine if a point lies inside a triangle?

The C program uses the formula known as the "point-in-triangle" test, which involves calculating the areas of sub-triangles formed by the given point and each side of the triangle. If the sum of these areas is equal to the area of the original triangle, then the point lies inside the triangle.

What are the inputs required for the C program to check if a point lies inside a triangle?

The inputs required are the coordinates of the three vertices of the triangle and the coordinates of the given point. These can be provided by the user or can be predefined in the program.

Can the C program handle triangles with different orientations?

Yes, the C program can handle triangles with different orientations. The orientation of the triangle does not affect the calculation of the point-in-triangle test, as long as the coordinates of the vertices and the point are correctly provided.

What is the output of the C program if the point lies inside the triangle?

If the point lies inside the triangle, the C program will output a message stating that the point is inside the triangle. Otherwise, if the point lies outside the triangle or on one of its sides, the program will output a message stating that the point is not inside the triangle.

Similar threads

Replies
1
Views
851
Replies
12
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
8
Views
1K
Replies
19
Views
2K
Back
Top