MATLAB: Intersection of two triangles

triangles

Hi If I have the vertexes of two triangles, how can I check if they intersect or not? I need a function that just gives me true if they intersect and false if not. Iif one of them is inside the other one, it shoud consider it an intersection. I found this coder, but it gives me empty result if one is inside the other one. % this define the vertexes (x,y) of first triangle trian1x =[ 0 0.5 1 0]; trian1y =[ 0 1 0 0]; % this define the vertexes (x,y) of second triangle trian2x =[ 0 0.25 0.5 0]; trian2y =[ 0 0.5 0 0]; % (xi,yi) are the intersection points [xi,yi] = polyxpoly(trian1x,trian1y,trian2x,trian2y,'unique'); figure, plot(trian1x,trian1y,'b') hold on plot(trian2x,trian2y,'r') hold on plot(xi,yi,'*g')
Thanks

Best Answer

Just combine the 'polyxpoly' result with two applications of 'inpoly' to test if either set of triangle vertices lies within the other triangle. If there are no intersection points from 'polyxpoly' and the results of 'inpoly' are all negative, then the triangles don't intersect.