


determine if point (x0,y0) is in triangle defined by nodes (xt(3),yt(3)) function res = isintriangle(xt,yt,x0,y0) determine if point (x0,y0) is in triangle defined by nodes (xt(3),yt(3)) | using algorithm used for scene rendering in computer graphics | algorithm works well unless particle happens to lie in a line parallel | to the edge of a triangle. | This can cause problems if you use a regular grid, say for idealized | modelling and you happen to seed particles right on edges or parallel to | edges. | ==============================================================================|


0001 function res = isintriangle(xt,yt,x0,y0) 0002 % determine if point (x0,y0) is in triangle defined by nodes (xt(3),yt(3)) 0003 % 0004 % function res = isintriangle(xt,yt,x0,y0) 0005 % 0006 % determine if point (x0,y0) is in triangle defined by nodes (xt(3),yt(3)) | 0007 % using algorithm used for scene rendering in computer graphics | 0008 % algorithm works well unless particle happens to lie in a line parallel | 0009 % to the edge of a triangle. | 0010 % This can cause problems if you use a regular grid, say for idealized | 0011 % modelling and you happen to seed particles right on edges or parallel to | 0012 % edges. | 0013 %==============================================================================| 0014 0015 res = 0; 0016 f1 = (y0-yt(1))*(xt(2)-xt(1)) - (x0-xt(1))*(yt(2)-yt(1)); 0017 f2 = (y0-yt(3))*(xt(1)-xt(3)) - (x0-xt(3))*(yt(1)-yt(3)); 0018 f3 = (y0-yt(2))*(xt(3)-xt(2)) - (x0-xt(2))*(yt(3)-yt(2)); 0019 if(f1*f3 >= 0.0 & f3*f2 >= 0.0) 0020 res = 1; 0021 end; 0022 0023 return 0024