POJ 1410 Intersection (判断线段与矩形是否相交)

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9536 Accepted: 2518

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

思路:判断线段与矩形相交--也就是判断线段与矩形的两条对角线是否有相交,只要有一条相交则说明相交。另外,此题说明了线段在矩形内部也算是相交,并且给出的矩形的两个点可能是左上,右下和或下,左上。线段相交需要满足的条件:(设两线段的端点分别为p1(x1,y1),p2(x2,y2) 和 p3(x3,y3),p4(x4,y4)
(1)快速排斥:min(x1,x2)<=max(x3,x4) && min(x3,x4)<=max(x1,x2) && min(y1,y2)<=max(y3,y4) && min(y3,y4)<=max(y1,y2);
(2)相互跨立:既满足(p1p3 x p1p2)*(p1p2 x p1p4)>=0 && (p3p2 x p3p4)*(p3p4 x p3p1)>=0 (点的顺序不唯一,可画个图,根据叉乘的右手法则看出,其实也就是判定p3,p4在p1p2的两端,和p2,p1在p3p4的两端)

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 struct node
 9 {
10     int x,y;
11 };
12 
13 node seg1,seg2,rec1,rec2,rec3,rec4;
14 
15 bool inRec(node s,int x1,int x2,int y1,int y2)
16 {
17     if(s.x > max(x1,x2))return false;
18     if(s.x < min(x1,x2))return false;
19     if(s.y > max(y1,y2))return false;
20     if(s.y < min(y1,y2))return false;
21     return true;
22 }
23 
24 int check(node p2,node p1,node p0)
25 {
26     return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
27 }
28 
29 bool isIntersect(node s1,node s2,node r1,node r2)
30 {
31     if(min(s1.x,s2.x)<=max(r1.x,r2.x) &&
32             min(r1.x,r2.x)<=max(s1.x,s2.x) &&
33             min(s1.y,s2.y)<=max(r1.y,r2.y) &&
34             min(r1.y,r2.y)<=max(s1.y,s2.y) &&
35             check(r1,s2,s1)*check(s2,r2,s1)>=0 &&
36             check(s1,r2,r1)*check(r2,s2,r1)>=0)
37         return true;
38     return false;
39 }
40 
41 int main()
42 {
43     int n;
44     scanf("%d",&n);
45     while(n--)
46     {
47         scanf("%d%d%d%d",&seg1.x,&seg1.y,&seg2.x,&seg2.y);
48         int x1,y1,x2,y2;
49         scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
50         if(inRec(seg1,x1,x2,y1,y2) || inRec(seg2,x1,x2,y1,y2)) //判断是否在矩形内
51         {
52             printf("T\n");
53             continue;
54         }
55         rec1.x = min(x1,x2); rec1.y = max(y1,y2);
56         rec2.x = max(x1,x2); rec2.y = min(y1,y2);
57         rec3.x = min(x1,x2); rec3.y = min(y1,y2);
58         rec4.x = max(x2,x2); rec4.y = max(y1,y2);
59         bool flag = false;
60         if(isIntersect(seg1,seg2,rec1,rec2))
61         {
62             flag = true;
63         }
64         if(isIntersect(seg1,seg2,rec3,rec4))
65         {
66             flag = true;
67         }
68         if(flag)
69             printf("T\n");
70         else
71             printf("F\n");
72     }
73     return 0;
74 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值