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

  • Thread starter Thread starter robert
  • Start date Start date
  • Tags Tags
    Point Triangle
Click For Summary

Discussion Overview

The discussion revolves around determining whether a point P is inside a triangle defined by vertices A, B, and C in a 2D space. Participants explore various algorithms and mathematical concepts, including dot products and cross products, to solve this problem, with a focus on practical implementation for computer programming.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant requests an algorithm to check if point P is within triangle ABC, specifying the need for details on cross products or dot products.
  • Another participant provides links to external resources for understanding dot products and triangle interiors, suggesting that these concepts are relevant to the problem.
  • A different approach is proposed, where the point's position relative to each triangle side is assessed using perpendicular vectors and dot products. This method includes a caveat regarding the determination of the correct side of the line.
  • Additional resources are shared, highlighting a website that offers clear explanations and working code related to computer geometry.

Areas of Agreement / Disagreement

Participants present multiple methods and approaches without reaching a consensus on a single algorithm or solution. The discussion includes various perspectives on how to implement the point-in-triangle test, indicating that no definitive agreement exists.

Contextual Notes

The proposed methods involve assumptions about vector orientations and numerical stability, which may not be fully resolved in the discussion. There are also references to external resources that may contain additional information or alternative approaches.

robert
Messages
23
Reaction score
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.
 
Physics news on Phys.org
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: 585
Last edited:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
3K
Replies
19
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K