Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8611 | Accepted: 2254 |
Description
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
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
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
Source
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef double mType; /**表示点或向量*/ struct Tpoint { mType x,y; Tpoint(){} Tpoint(mType _x,mType _y):x(_x),y(_y){} }; /**有起点和终点的向量或线段*/ struct Tsegment { Tpoint start,end; Tsegment(){} Tsegment(Tpoint _start,Tpoint _end):start(_start),end(_end){} Tsegment(mType sx,mType sy,mType tx,mType ty):start(sx,sy),end(tx,ty){} }; struct Trectangle { mType l,r,b,t; Trectangle(){} Trectangle(mType _l,mType _r,mType _b,mType _t):l(_l),r(_r),b(_b),t(_t){} }; /**生成一个点P到点Q的向量*/ Tpoint MakeVector(Tpoint P,Tpoint Q) { return Tpoint(Q.x-P.x,Q.y-P.y); } /**向量P与Q的叉积PQ*/ mType CrossProduct(Tpoint P,Tpoint Q) { return P.x*Q.y-P.y*Q.x; } /**向量QP与向量QR的叉积,用来判断向量的拐向 * 返回值: >0 向右拐, <0 向右拐,等于零同向或反向 */ mType MultiCross(Tpoint P,Tpoint Q,Tpoint R) { return CrossProduct(MakeVector(Q,P),MakeVector(Q,R)); } /**判断线段P和线段Q是否相交*/ bool IsIntersect(Tsegment P,Tsegment Q) { if(max(P.start.x,P.end.x)<min(Q.start.x,Q.end.x)||max(Q.start.x,Q.end.x)<min(P.start.x,P.end.x)|| max(P.start.y,P.end.y)<min(Q.start.y,Q.end.y)||max(Q.start.y,Q.end.y)<min(P.start.y,P.end.y))return 0; return (MultiCross(P.end,P.start,Q.start)*MultiCross(P.end,P.start,Q.end)<=0&& MultiCross(Q.end,Q.start,P.start)*MultiCross(Q.end,Q.start,P.end)<=0); } mType sx,sy,ex,ey,l,r,t,b; Tsegment P; Trectangle Q; int n; bool IsSegRectInter(Tsegment P,Trectangle Q) { if(Q.l<=P.start.x&&P.start.x<=Q.r&&Q.b<=P.start.y&&P.start.y<=Q.t)return 1; if(Q.l<=P.end.x&&P.end.x<=Q.r&&Q.b<=P.end.y&&P.end.y<=Q.t)return 1; if(IsIntersect(P,Tsegment(Q.l,Q.t,Q.r,Q.b)))return 1; if(IsIntersect(P,Tsegment(Q.l,Q.b,Q.r,Q.t)))return 1; return 0; } int main() { while(~scanf("%d",&n)) while(n--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&sx,&sy,&ex,&ey,&l,&t,&r,&b); if(t<b)swap(t,b); if(r<l)swap(r,l); P=Tsegment(sx,sy,ex,ey); Q=Trectangle(l,r,b,t); puts(IsSegRectInter(P,Q)?"T":"F"); } return 0; }