Point inside the triangle?

Started by
11 comments, last by TheVoid 20 years, 11 months ago
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]
Advertisement
if ( sum >2*3.14159236 ) return true
else return false

assuming true for inside

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
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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.
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
But what if I have cos of angle not angle in radians? I need to know it!

Edited/Added:
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]
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.
No, no. Please, read it carefully.
Aight... so you have the cos of the angle, and you want to find the angle itself? Whats the problem with acos? Please clarify.
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.

This topic is closed to new replies.

Advertisement