Check whether a given point lies inside a triangle or not | GeeksforGeeks

preview_player
Показать описание

This video is contributed by Harshit Verma

Please Like, Comment and Share the Video among your friends.

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

But how did you get the formula for area? I get that area of triangle is 1/2(base*ht) but how does the formula come by in case of any triangle

sbera
Автор

Since we are calculation area as float, the sum might not always be exactly equal to the bigger area and we may get wrong answer. We need to round off the areas.

rahulkhandelwal
Автор

If the area of the main triangle (points 1, 2, 3) is zero (and really a line segment) then this logic falls apart. The point tested could be on the line far away from the "triangle", but still on the line, and the areas would all add up to zero. If the main triangle has points that are co-located (and really a simple point), then the point tested can be anywhere at all and the function returns true. So watch out if "A" is zero...

gemhut
Автор

+ if A1 or A2 or A3 == 0 then the point is on the edge, any edge some like to consider as the point not inside the tringle if its on the edge

M.R.J-mmxv
Автор

Is there a way to do this with a quadrilateral (without splitting the quad into two tri's)

ned
Автор

Is there a possibility where the areas of the three triangles would be again equal to the area of the big one but the point would be outside ?

randomaccount
Автор

Clear and simple explanations (this is the definition of elegant ?). How about the general case of a triangle in 3D ?

YMO
Автор

it was glitchy so i had to do this


float D = abs(A - A1 - A2 - A3);

return D >= 0. && D < 0.001;


I have to do this in order for the triangle to look solid and not glitchy..

Kino-Imsureq
Автор

Excellent video, thank you for making it!

darrenwalsh
Автор

Just look to see if point P is on the same side of line AB as point C. Do the same for the other lines AC, BC, comparing with the opposite point. If they are all on the same side of the respective lines, then the point is in the triangle. It's very simple, very precise. If you deal with a vertical line, just compare the x coordinates. Your function that determines the side of a line that a point is on, should return either one side or the other, or on the line. You can return something like -1, 0 and 1 for this test. Don't make me post code.

gemhut
Автор

This is not very good way to do it because of precision issues

WowPlusWow