Find if Point P is in Triangle ABC - Algorithm & Details

In summary, to determine if a point P(x,y) is in a triangle with points A(x,y), B(x,y), and C(x,y), you can use the dot product method described in the links provided. This involves calculating the dot product of two vectors and using the sign to determine which side of the triangle the point is on. Another method involves using a perpendicular vector to determine the sign of the dot product and comparing it to the sign of the dot product between the perpendicular vector and a vector from a known point on the green side of the triangle. Further details and code examples can be found on the provided website.
  • #1
robert
23
0
I have a triangle in 2d space with points A(x,y), B(x,y), and C(x,y). I also have the point P(x,y). How do I find if the point P is in the triangle? If it involves anything like a cross product or dot product then please include details on how to do that. It's for a computer program and I have the variables ax,ay,bx,by,cx,cy,px,py so if someone could provide an algorithm that would be great. Thanks in advance.
 
Mathematics news on Phys.org
  • #3
Here is another method, maybe a bit more simple, where practical issues like inclusion of the border or numerical stability are perhaps easier to control.

The following procedure is repeated 3 times, once for each side of the triangle. In the attached picture, representing one of the three iterations using side AB, the idea is to determine if the given point is in the "green area", on the same side of the line as the triangle itself. (If this is true for the three sides, then the point is inside.) I use a vector perpendicular to the line, as drawn in the picture. If the vector from vertex A to vertex B is (p,q) = (Bx-Ax, By-Ay), then a perpendicular vector could be (-q,p). Now, a dot product between this perpendicular vector and any other vector (in particular, the vector from A to our given point P) will be positive on one side of the line, and negative on the other; we can use this sign to discern at which side any point lies. But there is a caveat: we don't really know towards which side (green or red) the perpendicular vector will point to (due to the simplistic way in which it was calculated; forcing it to the actual green side would require a good number of extra tests). In the picture it is on the green side, but it could have been just as perpendicular but on the red side. What I do is to use, as a reference, the sign of the dot product between the perpendicular vector and the vector from A to C (since C is always on the green side, on the side that contains the triangle).

The dot product of two vectors, Ux,Uy and Vx,Vy, is of course UxVx + UyVy.
 

Attachments

  • triang.png
    triang.png
    640 bytes · Views: 526
Last edited:
  • #4
Last edited by a moderator:

FAQ: Find if Point P is in Triangle ABC - Algorithm & Details

1. How does the algorithm determine if a point is inside a triangle?

The algorithm determines if a point is inside a triangle by using the concept of barycentric coordinates. It calculates the area of the triangle formed by the point and each of the triangle's vertices. If the sum of these areas is equal to the area of the original triangle, then the point is inside the triangle.

2. What are the inputs required for the algorithm?

The algorithm requires the coordinates of the three vertices of the triangle (A, B, C) and the coordinates of the point P that needs to be checked.

3. Can the algorithm be used for any type of triangle?

Yes, the algorithm can be used for any type of triangle - equilateral, isosceles, or scalene. It is not limited to any specific type of triangle.

4. What happens if the point is on the edge of the triangle?

If the point lies on one of the edges of the triangle, the algorithm will still consider it as inside the triangle. This is because the area of the triangle formed by the point and the two vertices of the edge will be zero, thus satisfying the condition of the algorithm.

5. Are there any limitations of this algorithm?

One limitation of this algorithm is that it assumes the triangle and the point lie on the same plane. If they are not on the same plane, the algorithm will not give accurate results. Additionally, it may not be suitable for very large or complex triangles, as it involves multiple calculations and may cause performance issues.

Similar threads

Replies
1
Views
938
Replies
3
Views
1K
Replies
1
Views
781
Replies
1
Views
785
Replies
5
Views
2K
Replies
2
Views
4K
Back
Top