% 判断xoy平面内两线段是否相交
function r=line_cross(x1,y1,x2,y2,x3,y3,x4,y4) %x1,y1,x2,y2为L1线段端点坐标
LD1x=min(x1,x2);LD1y=min(y1,y2); %x3,y3,x4,y4为L2线段端点坐标
LD2x=min(x3,x4);LD2y=min(y3,y4); %LD为leftdown,RU为rightup
RU1x=max(x2,x1);RU1y=max(y2,y1);
RU2x=max(x3,x4);RU2y=max(y3,y4);
X=(RU1x>LD2x)&(RU2x>LD1x);
Y=(RU1y>LD2y)&(RU2y>LD1y);
if X&Y
M1=[x3-x1,y3-y1;x2-x1,y2-y1];
M2=[x4-x1,y4-y1;x2-x1,y2-y1];
M3=[x1-x3,y1-y3;x3-x4,y3-y4];
M4=[x2-x3,y2-y3;x3-x4,y3-y4];
P1=det(M1); P2=det(M2); P3=det(M3); P4=det(M4);
Q1=P1*P2;Q2=P3*P4;
if Q1 r=2;%相交
elseif Q1==0||Q2==0
r=1;%端点在线段上
else
r=0;%相离
end
else
r=0;
end