题目链接:pku1410 注意之点: 1、文中给出的左上顶点和右下顶点不保证x1<x2,y1>y2;即需要自己判断2、文中似乎没说,但必须这么认为:线段完全在矩形内部要返回T. 代码: //where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) //the top left and (xright, ybottom) the bottom right corner of the rectangle #include <cmath> #include <algorithm> using namespace std; struct point { double x,y; }; struct line1 { point p1, p2; }; const double eps=1e-10; double min(double a, double b) { return a < b ? a : b; } double max(double a, double b) { return a > b ? a : b; } bool is_cross(point a, point b, point c, point d){ if ( min(a.x, b.x) > max(c.x, d.x) || min(a.y, b.y) > max(c.y, d.y) || min(c.x, d.x) > max(a.x, b.x) || min(c.y, d.y) > max(a.y, b.y) ) return 0; double h, i, j, k; h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x); j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x); k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x); return h * i <= eps && j * k <= eps; } #include<iostream> using namespace std; int main() { int n; point s1,e1,s2,e2,s3,e3,s4,e4,s5,e5; int xl,yt,xr,yb; cin>>n; while(n--) { cin>>s1.x>>s1.y>>e1.x>>e1.y>>xl>>yt>>xr>>yb; s2.x=xl;s2.y=yt;e2.x=xr;e2.y=yt; s3.x=xr;s3.y=yb;e3.x=xr;e3.y=yt; s4.x=xl;s4.y=yb;e4.x=xr;e4.y=yb; s5.x=xl;s5.y=yb;e5.x=xl;e5.y=yt; if(is_cross(s1,e1,s2,e2)||is_cross(s1,e1,s3,e3)||is_cross(s1,e1,s4,e4) ||is_cross(s1,e1,s5,e5))cout<<"T"<<endl; else { if( s1.x<=max(xl,xr)&&s1.x>=min(xl,xr)&&s1.y<=max(yt,yb)&&s1.y>=min(yt,yb) && e1.x<=max(xl,xr)&&e1.x>=min(xl,xr)&&e1.y<=max(yt,yb)&&e1.y>=min(yt,yb) ) cout<<"T"<<endl; else cout<<"F"<<endl; } } system("pause"); return 1; } 代码二: //where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) //the top left and (xright, ybottom) the bottom right corner of the rectangle #include <cmath> #include <algorithm> const double eps=1e-8; const double pi=acos(-1.0); using namespace std; struct point { int x,y; }; struct line1 { point p1, p2; }; double dist(point p1,point p2) { double x1=p1.x-p2.x,y1=p1.y-p2.y; return sqrt(x1*x1+y1*y1); } double multi(point p0,point p1,point p2) { return (p1.x-p0.x)*(p2.y-p0.y) -(p2.x-p0.x)*(p1.y-p0.y); } bool is_cross(point s1,point e1,point s2,point e2) { return max(s1.x,e1.x)>=min(s2.x,e2.x)&& max(s2.x,e2.x)>=min(s1.x,e1.x)&& max(s1.y,e1.y)>=min(s2.y,e2.y)&& max(s2.y,e2.y)>=min(s1.y,e1.y)&& multi(s2,e1,s1)*multi(e1,e2,s1)>=0&& multi(s1,e2,s2)*multi(e2,e1,s2)>=0; } #include<iostream> using namespace std; int main() { int n; point s1,e1,s2,e2,s3,e3,s4,e4,s5,e5; int xl,yt,xr,yb; cin>>n; while(n--) { cin>>s1.x>>s1.y>>e1.x>>e1.y>>xl>>yt>>xr>>yb; s2.x=xl;s2.y=yt;e2.x=xr;e2.y=yt; s3.x=xr;s3.y=yb;e3.x=xr;e3.y=yt; s4.x=xl;s4.y=yb;e4.x=xr;e4.y=yb; s5.x=xl;s5.y=yb;e5.x=xl;e5.y=yt; if(is_cross(s1,e1,s2,e2)||is_cross(s1,e1,s3,e3)||is_cross(s1,e1,s4,e4) ||is_cross(s1,e1,s5,e5))cout<<"T"<<endl; else { if(s1.x<=max(xl,xr)&&s1.x>=min(xl,xr)&&s1.y<=max(yt,yb)&&s1.y>=min(yt,yb) && e1.x<=max(xl,xr)&&e1.x>=min(xl,xr)&&e1.y<=max(yt,yb)&&e1.y>=min(yt,yb) ) cout<<"T"<<endl; else cout<<"F"<<endl; } } system("pause"); return 1; }