C program to check whether a point lies inside a triangle

AI Thread Summary
The discussion revolves around determining whether a fourth coordinate lies inside a triangle defined by three other coordinates. The initial approach involved calculating the area of the triangle and the areas of three smaller triangles formed with the fourth point. However, this method failed with floating-point precision issues. The revised method uses the concept of line equations to check if the fourth point lies on the same side of the triangle's edges. The code snippet provided implements this logic but encounters a problem where points on the extended lines of the triangle are incorrectly classified as being inside. To resolve this, it is suggested to combine the area method with the line equation approach. If the point is classified as inside, the areas of the original triangle and the triangle formed with the new point should be compared to ensure the point is not merely on the extended lines. The discussion also references a computational geometry book for further insights.
Gagan A
Messages
19
Reaction score
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
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...
 
if your not to lazy see if your library has a book by Joseph O'rourke titled
Computational Geometry.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
12
Views
1K
Replies
2
Views
1K
Replies
8
Views
2K
Replies
19
Views
3K
Replies
5
Views
937
Back
Top