Skip to main content
GameDev.net gamedev.net
🔒 Locked

triangle question

Started by hello_there Feb 2, 2004 at 9:53 PM 1 replies 450+ views
Original Post
hello_there
hello_there
how would i check if a point is inside a triangle in 2d. Is there any easy way or am i going to have to do it with Trig?
____________How could hell be worse?
hello_there
hello_there
oh yeah i should mention it''s just a axis alinged cube cut down the diagonal.
____________How could hell be worse?
Zahlman
Zahlman
Found via google, knowing what to search for ("barycentric coordinates"):

http://graphics.cs.ucdavis.edu/GraphicsNotes/Barycentric-Coordinates/Barycentric-Coordinates.html

"if a1, a2, a3 are scalars such that a1 + a2 + a3 = 1, then the point P defined by P = a1P1 + a2P2 + a3P3 is a point on the plane of the triangle formed by P1, P2, P3. The point is within the triangle P1P2P3 if 0<=a1,a2,a3<=1. If any of the a''s is less than zero or greater than one, the point P is outside the triangle. If any of the a''s is zero, ... P is on one of the lines joining the vertices of the triangle." (and for completeness, if one of the a''s is one and the others are zero, then P is trivially at a vertex of the triangle.)

So now we have equations

a1 * p1x + a2 * p2x + a3 * p3x = Px
a1 * p1y + a2 * p2y + a3 * p3y = Py
a1 + a2 + a3 = 1


Solve 3 equations in 3 unknowns, and check the a values against (0,1). Done.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.