TGC Codebase Backup



Determine whether a point is inside a 2D triangle by IanM

23rd Sep 2003 14:05
Summary

Determine whether a point is inside a 2D triangle. This is a very fast routine as it doesn't use any trig functions - just subtractions and multiplies



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function PointInTriangle(  x as float, y as float, x1 as float, y1 as float, x2 as float, y2 as float, x3 as float, y3 as float)
   local AB as float
   local BC as float
   local CA as float

   AB = ((y-y1)*(x2-x1)) - ((x-x1)*(y2-y1))
   BC = ((y-y2)*(x3-x2)) - ((x-x2)*(y3-y2))
   if AB * BC <= 0 then exitfunction 0

   CA = (y-y3)*(x1-x3) - (x-x3)*(y1-y3)
   if BC * CA <= 0 then exitfunction 0

endfunction 1