problem:
Intersection
Time Limit:1000MS
Memory Limit:10000KB
64bit IO Format:%I64d & %I64u
Submit
Status
Description
You are to write a program that has to decide whether a given line segment intersects a given rectangle.
An example:
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
line is said to intersect the rectangle if the line and the rectangle
have at least one point in common. The rectangle consists of four
straight lines and the area in between. Although all input values are
integer numbers, valid intersection points do not have to lay on the
integer grid.
Input
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.
Output
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.
Sample Input
1
4 9 11 2 1 5 7 1
Sample Output
F
实在是太坑。。
判断线段和矩形的四条边相交是不够的。。
因为矩形是实心的,要判断线段是否在矩形内部。。。
注意审题。。
我的代码实在太sb。。
code:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
#define eps 1e-6
#define N 6
struct line
{
double x1,x2,y1,y2;
}a,b;
inline int dcmp(double x)
{
if(fabs(x)<eps)
return 0;
return x>0?1:-1;
}
double x,y;
void cal()
{
double a1=a.y1-a.y2,b1=a.x2-a.x1,c1=a.x1*a.y2-a.x2*a.y1;
double a2=b.y1-b.y2,b2=b.x2-b.x1,c2=b.x1*b.y2-b.x2*b.y1;
x=(c1*b2-c2*b1)/(a2*b1-a1*b2);
y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
}
bool judgep(line c,line d)
{
if((c.x1-c.x2)*(d.y1-d.y2)==(c.y1-c.y2)*(d.x1-d.x2)) return true;
else return false;
}
bool judget(line c,line d)
{
if((c.x2-c.x1)*(d.y1-c.y1)==(c.y2-c.y1)*(d.x1-c.x1)) return true;
else return false;
}
int ans;
int main()
{
int t;
cin>>t;
while(t--)
{
ans=0;
cin>>a.x1>>a.y1>>a.x2>>a.y2>>b.x1>>b.y1>>b.x2>>b.y2;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if((a.x1-b.x1)*(a.x1-b.x2)<=0&&(a.y1-b.y1)*(a.y1-b.y2)<=0) ans=1;
if((a.x2-b.x1)*(a.x2-b.x2)<=0&&(a.y2-b.y1)*(a.y2-b.y2)<=0) ans=1;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
double t;
t=b.x2;
b.x2=b.x1;
b.x1=t;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
double w1=b.x1,w2=b.x2,w3=b.y1,w4=b.y2;
b.x2=b.x1;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
b.x2=w2;
b.y1=b.y2;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
b.y1=w3;
b.x1=b.x2;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
b.x1=w1;
b.y2=b.y1;
//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;
if(judgep(a,b));
else
{
cal();
if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;
}
if(ans==0) cout<<"F"<<endl;
else cout<<"T"<<endl;
}
return 0;
}