C program to check whether a point lies inside a triangle

Click For Summary
SUMMARY

The discussion focuses on a C program designed to determine whether a given point lies inside a triangle defined by three coordinates. The initial approach utilized area calculations, but the author encountered issues with floating-point precision. The revised method checks the position of the point relative to the triangle's sides using line equations. However, the program incorrectly identifies points on the extended lines of the triangle as being inside, prompting a request for a solution to refine this logic.

PREREQUISITES
  • Understanding of C programming and syntax
  • Knowledge of geometric concepts related to triangles
  • Familiarity with floating-point arithmetic and its implications
  • Basic understanding of line equations in a 2D coordinate system
NEXT STEPS
  • Implement a method to accurately check if a point lies strictly inside a triangle using barycentric coordinates
  • Explore the use of computational geometry algorithms for point-in-polygon tests
  • Learn about floating-point precision issues and how to mitigate them in C programming
  • Review the book "Computational Geometry" by Joseph O'Rourke for advanced techniques
USEFUL FOR

Software developers, particularly those working with geometric algorithms, students studying computational geometry, and anyone interested in improving their C programming skills related to geometric calculations.

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.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K