#### Archived

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

# Point inside the triangle?

## 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.

##### Share on other sites
The sum of the degrees should be close to pi.
Write something like:
if( sum < 3.07 && sum > 3.21 )

Just change the values to a boundary which fits your needs.

__________________________ ____ ___ __ _
Enselic's Corner - CodeSampler.com

[edited by - Enselic on April 29, 2003 2:49:35 PM]

[edited by - Enselic on April 29, 2003 2:50:16 PM]

##### Share on other sites
But I have cos of the angle not the angle!

##### Share on other sites
You want to know what you will end up with if you add the three cos values, don't you? Well I've thought of it for a while and come up with this:

Since v is not propotional to cos v the sum of three cos v with v1 + v2 + v3 = 360° will not have a constant value. I've tried to prove that but I'm not smart enough... Well, consider you have cos v1 + cos v2 + cos v3 and then change the angles but still keep the sum of the angles to equal 360° and the result is not the same (cos (v1 + 30) + cos (v2 - 30) + cos v3)...

Example:
cos 120° + cos 120° + cos 120° = -1.5
cos 150° + cos 90° + cos 120° = -1.366

Anyone disagree or have a better prove?

____________________ ____ ___ __ _
Enselic's Corner - CodeSampler.com

[edited by - Enselic on May 2, 2003 9:35:34 AM]

[edited by - Enselic on May 2, 2003 9:36:04 AM]

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982072

• 21
• 9
• 9
• 13
• 11