线段判交

来源:互联网 发布:mac桌面图标怎么删除 编辑:程序博客网 时间:2024/05/01 00:04

线段判交模版

 

 

#include<iostream>using namespace std;inline int max(int a,int b){return a>b? a:b;}inline int min(int a,int b){return a<b? a:b;}struct point{int x;int y;};struct line{point s;point e;}l1,l2;inline int crossProduct(line a, line b)  //前一条叉乘后一条{return (a.e.x-a.s.x) * (b.e.y-b.s.y) - (a.e.y-a.s.y) * (b.e.x-b.s.x);}bool judge(line P,line Q);bool judgeRec(line P,line Q);int main(){while(true){cin>>l1.s.x>>l1.s.y>>l1.e.x>>l1.e.y;cin>>l2.s.x>>l2.s.y>>l2.e.x>>l2.e.y;if( judge(l1,l2) )cout<<"相交"<<endl;elsecout<<"不相交"<<endl;}return 0;}bool judge(line P,line Q){if(!judgeRec(P,Q))return false;line L1,L2,L3;L1.s=Q.s;L1.e=P.s;L2=Q;L3.s=Q.s;L3.e=P.e;if( crossProduct(L1,L2)*crossProduct(L2,L3) <0)return false;L1.s=P.s;L1.e=Q.e;L2=P;L3.s=P.s;L3.e=Q.s;if( crossProduct(L1,L2)*crossProduct(L2,L3)<0)return false;return true;}bool judgeRec(line a,line b)         //判断以两条线段为对角线的矩形是否相交(快速排除){if( max(a.s.x,a.e.x)>min(b.s.x,b.e.x) && max(b.s.x,b.e.x)>min(a.s.x,a.e.x)&& max(a.s.y,a.e.y)>min(b.s.y,b.e.y) && max(b.s.y,b.e.y)>min(a.s.y,a.e.y) )return true;return false;}


 

原创粉丝点击