#### Archived

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

# Point inside the triangle?

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

## Recommended Posts

Let's say I have the triangle ABC and the point P (all in 2D). I want to check if the point P is inside the triangle ABC. So I need to calculate: cosangle1 = Dot(P - A, P - B) / Magnitude(P - A) * Magnitude(P - B); cosangle2 = Dot(P - B, P - C) / Magnitude(P - B) * Magnitude(P - C); cosangle3 = Dot(P - C, P - A) / Magnitude(P - C) * Magnitude(P - A); sum = cosangle1 + cosangle2 + cosangle3; And now I have a question: what will be the sign of 'sum' if the point is inside the triangle and what if it is outside? [edited by - TheVoid on April 28, 2003 6:04:11 AM]

##### Share on other sites
if ( sum >2*3.14159236 ) return true
else return false

assuming true for inside

##### Share on other sites
Not the sum of the cos values, but the sum of the degrees in radians.

cos v != v

So if v1 + v2 + v3 is close to 2*pi then the point is inside the triangle. And btw, this is true for any convex polygon.

______________________________
Enselic''s Corner Code Sampler

##### Share on other sites
Actually, you don''t even need those equations. If you are working in 2d (and even 3d, as long as you project all the coordinates to an abitrary 2d space). All you need to do is cast a ray in any direction from the point and count the number of lines that are crossed. Since it doesnt matter which direction, you can translate the triangle so that the original point becomes the origin of the system, then find how many lines in the triangle cross the positive X-Axis. This is an extremely quick operation, and you can make a great deal of optimizations based on trivial exclusion. If you want some code, I can post some here for you.

##### Share on other sites
Check out the link near the bottom in the forum FAQ:

Forum FAQ

Its a discussion of point-in-polygon strategies.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
But what if I have cos of angle not angle in radians? I need to know it!

I know I can get the angle in radians using acos() function, but I want to know it when I have cos of angle.

[edited by - TheVoid on April 29, 2003 9:23:12 AM]

##### Share on other sites
You have an angle measurement in degrees and want to convert to radians? 2*PI radians = 360 deg, so just multiply your degrees by PI/180.

##### Share on other sites
Aight... so you have the cos of the angle, and you want to find the angle itself? Whats the problem with acos? Please clarify.

##### Share on other sites
I need to know the sign of the ''sum'' if the point is inside the triangle and outside. I need to know whether the ''sum'' will be greater, greater or equal, less, less or equal or equal to 0.

• 12
• 40
• 15
• 10
• 23