题意:判断一条线段是否在矩形里或与矩形相交。
思路:先判断线段的两个点是否在矩形里,再判断线段是否与矩形的四条边相交即可。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; ll chaj(ll x1,ll y1,ll x2,ll y2) { ll ans=x1*y2-x2*y1; if(ans==0) return 0; if(ans>0) return 1; return -1; } int main() { int t; cin>>t; while(t--) { ll x1,y1,x2,y2,x3,y3,x4,y4; cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; if(x1>=min(x3,x4)&&x1<=max(x3,x4)&&y1>=min(y3,y4)&&y1<=min(y3,y4)) { cout<<"T"<<endl; continue; } if(x2>=min(x3,x4)&&x2<=max(x3,x4)&&y2>=min(y3,y4)&&y2<=min(y3,y4)) { cout<<"T"<<endl; continue; } int s=0; if( chaj(x1-x3,y1-y3,0,y4-y3)*chaj(x2-x3,y2-y3,0,y4-y3)<=0&&chaj(x3-x1,y3-y1,x2-x1,y2-y1)*chaj(x3-x1,y4-y1,x2-x1,y2-y1)<=0 ) s=1; if( chaj(x1-x3,y1-y3,x4-x3,0)*chaj(x2-x3,y2-y3,x4-x3,0)<=0&&chaj(x3-x1,y3-y1,x2-x1,y2-y1)*chaj(x4-x1,y3-y1,x2-x1,y2-y1)<=0 ) s=1; if( chaj(x1-x4,y1-y4,x3-x4,0)*chaj(x2-x4,y2-y4,x3-x4,0)<=0&&chaj(x4-x1,y4-y1,x2-x1,y2-y1)*chaj(x3-x1,y4-y1,x2-x1,y2-y1)<=0 ) s=1; if( chaj(x1-x4,y1-y4,0,y3-y4)*chaj(x2-x4,y2-y4,0,y3-y4)<=0&&chaj(x4-x1,y4-y1,x2-x1,y2-y1)*chaj(x4-x1,y3-y1,x2-x1,y2-y1)<=0 ) s=1; if(s) cout<<"T"<<endl; else cout<<"F"<<endl; } }
原文:https://www.cnblogs.com/zcb123456789/p/13669688.html