#### Archived

This topic is now archived and is closed to further replies.

# Point in a Triangle

This topic is 5280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering if anyone can tell me how to quickly and efficiently determine if a point is within a triangle. I have already tried using a system of equations for the three sides, and it gets messy. Any help is appreciated. Sagar Indurkhya

##### Share on other sites
The way i did it isn't really fast (i think) but it's one way to do it:

- get the normals from each vertex to the point
- if the magnitude of the sum of the normals(or it's square) is larger than one it's not on the triangle, if it's 1.0 it's on the edge.

This only works correctly if you know for sure that the point is on the triangle's plane.

Again, this might not be the fastest solution out there, it's one for sure.

EDIT: magnitude of the sum of the normals instead of just magnitude

[edited by - Tree Penguin on June 3, 2004 4:05:00 PM]

##### Share on other sites
The triangle has 3 points in CCW and we must check if a point B is inside it (we already checked if its in same plane)
if ((3-1)dot(2-1)<(B-1)dot(2-1) && (3-2)dot(1-2)>(B-2)dot(1-2))
then its inside
else its not inside

i hope it works... :/

[edited by - ygor_freire on June 3, 2004 5:24:37 PM]

##### Share on other sites
The best way to calculate if a point is in a triangle (or even a polygon) is to calculate the angle between your point and the points of the triangle (or polygon) and add them all up.

If the sum of the angles is larger or equal to 2*PI (or 360 degrees), then the point is inside the triangle (or polygon, won''t say it again ;P).

The way to calculate the angle between to vectors is to use the Dot product:

float dotProduct = Dot(Vector1, Vector2);float vectorsLength = Length(Vector1) * Length(Vector2);double angle = acos( dotProduct / vectorsMagnitude );

##### Share on other sites
thanx!!! that last one was easy to implement.But doesn''t that only work that don''t bend in? just curious.

##### Share on other sites
Yes, that algorithm only works for convex polygons.

##### Share on other sites
quote:

The best way to calculate if a point is in a triangle (or even a polygon) is to calculate the angle between your point and the points of the triangle (or polygon) and add them all up.

Sorry, but I think that is a terrible way. Do a search for barycentric coordinates. Can''t beat it, IMHO.

##### Share on other sites
Hmmm. I wonder if one might find such useful information about point in polygon tests in the Forum FAQ?

Graham Rhodes
Principal Scientist
Applied Research Associates, Inc.

Owned -_-

##### Share on other sites
quote:
Original post by raydog
Sorry, but I think that is a terrible way. Do a search for barycentric coordinates. Can''t beat it, IMHO.

Ok, your right, but it works for me for now...

##### Share on other sites

This topic is 5280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633369
• Total Posts
3011536
• ### Who's Online (See full list)

There are no registered users currently online

×