Jump to content
  • Advertisement

Archived

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

Sagar_Indurkhya

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!