# 计算几何判断两个线段是否有相交 |模板()-其他

## 计算几何判断两个线段是否有相交 |模板()

``````typedef long long ll;
struct Point{
double x,y;
Point(double X=0,double Y=0){
x=X,y=Y;
}
void in(){
scanf("%lf%lf",&x,&y);
}
Point operator-(Point A)const{
return Point(x-A.x,y-A.y);
}
ll operator*(Point A)const{
return 1ll*x*A.y-1ll*y*A.x;
}
bool operator==(Point A)const{
return x==A.x&&y==A.y;
}
}st,en,p[20][2];
double mult(Point a, Point b, Point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool intersect(Point aa, Point bb, Point cc, Point dd)//相交返回true，不相交返回false
{
if ( max(aa.x, bb.x)<min(cc.x, dd.x) ) return false;
if ( max(aa.y, bb.y)<min(cc.y, dd.y) ) return false;
if ( max(cc.x, dd.x)<min(aa.x, bb.x) ) return false;
if ( max(cc.y, dd.y)<min(aa.y, bb.y) ) return false;
if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 ) return false;
if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 ) return false;
return true;
}``````
————————
``````typedef long long ll;
struct Point{
double x,y;
Point(double X=0,double Y=0){
x=X,y=Y;
}
void in(){
scanf("%lf%lf",&x,&y);
}
Point operator-(Point A)const{
return Point(x-A.x,y-A.y);
}
ll operator*(Point A)const{
return 1ll*x*A.y-1ll*y*A.x;
}
bool operator==(Point A)const{
return x==A.x&&y==A.y;
}
}st,en,p[20][2];
double mult(Point a, Point b, Point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool intersect(Point aa, Point bb, Point cc, Point dd)//相交返回true，不相交返回false
{
if ( max(aa.x, bb.x)<min(cc.x, dd.x) ) return false;
if ( max(aa.y, bb.y)<min(cc.y, dd.y) ) return false;
if ( max(cc.x, dd.x)<min(aa.x, bb.x) ) return false;
if ( max(cc.y, dd.y)<min(aa.y, bb.y) ) return false;
if ( mult(cc, bb, aa)*mult(bb, dd, aa)<0 ) return false;
if ( mult(aa, dd, cc)*mult(dd, bb, cc)<0 ) return false;
return true;
}``````