//叉积
//>0时,表示p1p3在p1p2的逆时针
//=0时,表示p1p2p3共线
//<0时,表示p1p3在p1p2的顺时针
private funciton CrossMutiply(p1:Point,p2:Point,p3:Point):int
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
//点积
private function PointMutiply(p1:Point,p2:Point,p3:Point):int
{
return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
}
//判断两线段是否相交
private function Segments_Intersect(line1:Line,line2:Line):Boolean
{
int d1=CrossMutiply(line1.p1,line1.p2,line2.p1);
int d2=CrossMutiply(line1.p1,line1.p2,line2.p2);
int d3=CrossMutiply(line2.p1,line2.p2,line1.p1);
int d4=CrossMutiply(line2.p1,line2.p2,line1.p2);
if((d1*d2<0)&&((d3*d4)<0))
return true;
else if(d1==0&&PointMutiply(line2.p1,line1.p1,line1.p2)<=0)
{
return truue;
}
else if(d2==0&&PointMutiply(line2.p2,line1.p1,line1.p2)<=0)
{
return true;
}
else if(d3==0&&PointMutiply(line1.p1,line2.p1,line2.p2)<=0)
{
return true;
}
else if(d4==0&&PointMutiply(line1.p2,line1.p1,line2.p2)<=0)
{
return true;
}
else
reutrn false;
}
更多参考:
C++版:
http://www.cnblogs.com/lujiacheng/archive/2011/07/28.html
CSDN贴:跨立:
http://topic.csdn.net/u/20090428/16/178d023d-9f65-45e0-99b6-0f38c908b80e.html