判断线段与矩形是否相交

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 10:23:12

判断线段与矩形是否相交

Sample Input1 4 9 11 2 1 5 7 1Sample OutputFJudge Tips注意看题目说明,top的值可能会小于bottom.线段在矩形里面也算相交.///// WA了三次,存到博客上以供查询.思路,相看线段所在直线是否与矩形相交,如果不相交则必为 “F”,如果相交,则看线段的两个点是否在矩形的同一边(即两点的 x(y) 坐标都比矩形的小 x(y) 坐标小,或者大),若在同一边则为“F”,否则就是相交的情况.//// my code#include
using namespace std;int main(){
int n,xs,ys,xe,ye,xleft,ytop,xr,yb;cin>>n;for(int i=0; i>xs>>ys>>xe>>ye>>xleft>>ytop>>xr>>yb;
int a=ys-ye,b=xe-xs,c=xs*ye-xe*ys;
if( (a*xleft+b*ytop+c>=0 && a*xr+b*yb+c=0 && a*xr+b*ytop+c xr)
swap(xleft,xr);
if(ytop < yb)
swap(ytop,yb);
if( (xsxr) ||
(ys>ytop && ye>ytop) ||