Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 712 | 174 | Standard |
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
Figure 1: Line segment does not intersect rectangle
The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format: xstart ystart xend yend xleft ytop xright ybottom 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. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.
For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.
1 4 9 11 2 1 5 7 1
F
This problem is used for contest: 117 184
这是我做的第一个计算几何的问题在这里纪念一下,详细情况请看我博客里面转载的判断线段的相交。。。
#include<iostream>
using namespace std;
struct POINT
{
double x;
double y;
};
struct LINE
{
POINT head;
POINT tail;
};
double crossmul(POINT point1,POINT point2,POINT point3)//计算两个向量的叉乘积
{
double ax=point1.x-point2.x;
double ay=point1.y-point2.y;
double bx=point1.x-point3.x;
double by=point1.y-point3.y;
return (ax*by-ay*bx);
}
bool linecross(LINE line1,LINE line2)//判断两个线段是否相交
{
if((crossmul(line1.head,line1.tail,line2.head)*crossmul(line1.head,line1.tail,line2.tail)<=0)//注意等号
&&(crossmul(line2.head,line2.tail,line1.head)*crossmul(line2.head,line2.tail,line1.tail)<=0))
{
return true;
}
else return false ;
}
int main()
{
int n;
cin>>n;
for(int t=1;t<=n;t++)
{
double xstart,ystart,xend,yend;
double xleft,ytop,xright,ybottom;
cin>>xstart>>ystart>>xend>>yend;
cin>>xleft>>ytop>>xright>>ybottom;
if((xstart>=xleft&&xstart<=xright&&ystart>=ybottom&&ystart<=ytop)||
(xend>=xleft&&xend<=xright&¥d>=ybottom&¥d<=ytop))//判断线段的端点是否有一个在矩形内部
{
cout<<"T"<<endl;
}
else
{
LINE a;
LINE cross1,cross2;
a.head.x=xstart;a.head.y=ystart;
a.tail.x=xend;a.tail.y=yend;
cross1.head.x=xleft;cross1.head.y=ybottom;
cross1.tail.x=xright;cross1.tail.y=ytop;
cross2.head.x=xleft;cross2.head.y=ytop;
cross2.tail.x=xright;cross2.tail.y=ybottom;
if(linecross(cross1,a)||linecross(cross2,a))//判断线段与矩形的两个对角线相交
{
cout<<"T"<<endl;
}
else
{
cout<<"F"<<endl;
}
}
}
return 0;
}