bool PointInTriangle (i, p0, p1, p2) 
    returns ({TRUE, FALSE},u,v);
 1: e0=i-p0 
 2: e1=p1-p0 
 3: e2=p2-p0 
 4: if (e1x=0) 
 5:   if (e2x=0) return (FALSE,0,0); 
 6:   u=e0x/e2x
 7:   if (u<0 or u>1) return FALSE,0,0); 
 8:   if (e1y=0) return FALSE,0,0) 
 9:   v=( e0y-e2yu)/e1y
10:   if (v<0) return (FALSE,0,0); 
11: else 
12:   d = e2ye1x-e2xe1y 
13:   if (d=0) return (FALSE,0,0); 
14:   u = (e0ye1x-e0xe1y)/d 
15:   if (u<0 or u>1) return (FALSE,0,0);
16:   v = (e0x-e2xu) / e1x
17:   if (v<0) return (FALSE,0,0);
18: if (u+v>1) return (FALSE,0,0); 
19: return (TRUE,u,v);

Example 1: Pseudocode for testing some point i for inclusion in a triangle.

Back to Article