Math Help?

Malte Pfaff-Brill revolution at derbrill.de
Fri Apr 30 17:07:22 EDT 2010


Hi Scott,

this has been described as the most cost efficient (clock cycle wise) algo in game programming gems (if I recall correctly). I translated this to rev a while back:

Theory is, if the point is inside the triangle, then the sum of all partial areas equals the area of the whole triangle. 
To avoid rounding errors it does not check for equality, but a instead looks if the value is smaller than possible rounding error (I still hope for sub pixel position in the future, thus I left it in).

Hope that helps,

Malte

function withinTriangle x1,y1,x2,y2,x3,y3,px,py
   -- x1 / y1 to x3 / y3 define the triangle, pX and pY are the point to check
   local area1,area2
   put area(x1,y1,x2,y2,px,py) + area(x2,y2,x3,y3,px,py) + area(x3,y3,x1,y1,px,py) into area1
   put area(x1,y1,x2,y2,x3,y3) into area2
   return abs(area1-area2)<0.000000000001
end withinTriangle

function area x1,y1,x2,y2,x3,y3
   return abs(x1*y2+x2*y3+x3*y1-x1*y3-x3*y2-x2*y1)/2
end area


More information about the use-livecode mailing list