POJ 1410--Intersection(判断线段和矩形相交)

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16322 Accepted: 4213

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

Source

  • 题目的判断是否一条线段和矩形相交,可以想到直接判断给定线段是否和矩形的四条边相交即可,但是有一个问题,题目定义的矩形"The rectangle consists of four straight lines and the area in between",包括了其中的面积,就因为这个wa了几发Orz,我的等级还是不够啊。
  • 最后只要判断给定线段是否和矩形的四条边相交,以及线段是否在矩形内,线段是否在矩形内部可以用线段的端点是否在矩形内部来判断。
  •  1 #include<iostream>
     2 #include<algorithm>
     3 #include<cmath>
     4 using namespace std;
     5 int n;
     6 double xs, ys, xe, ye, xl, yl, xr, yr;
     7 const double eps = 1.0e8;
     8 typedef struct point {
     9     double x;
    10     double y;
    11     point(double a, double b) {
    12         x = a;
    13         y = b;
    14     }
    15     point() {
    16 
    17     }
    18 }point;
    19 typedef struct edge {
    20     point start;
    21     point end;
    22     edge(point a, point b) {
    23         start = a;
    24         end = b;
    25     }
    26     edge() {
    27 
    28     }
    29     edge(edge &t) {
    30         start = t.start;
    31         end = t.end;
    32     }
    33 }edge;
    34 point t[4];
    35 edge line;
    36 edge rec[4];
    37 
    38 inline double dabs(double a) { return a < 0 ? -a : a; }
    39 inline double max(double a, double b) { return a > b ? a : b; }
    40 inline double min(double a, double b) { return a < b ? a : b; }
    41 double multi(point p1, point p2, point p0) {
    42     return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y);
    43 }
    44 bool Across(edge v1, edge v2) {
    45     if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) &&
    46         max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) &&
    47         max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) &&
    48         max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) &&
    49         multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v2.start) >= 0 &&
    50         multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v1.start) >= 0
    51         )
    52         return true;
    53     return false;
    54 }
    55 int main(void) {
    56     while (cin >> n) {
    57         while (n-- > 0) {
    58             int flag = 0;
    59             cin >> xs >> ys >> xe >> ye >> xl >> yl >> xr >> yr;
    60             line = edge(point(xs, ys), point(xe, ye));
    61             t[0] = point(xl, yl), t[1] = point(xr, yl);
    62             t[2] = point(xr, yr), t[3] = point(xl, yr);
    63             for (int i = 0; i < 4; i++) {
    64                 rec[i] = edge(t[i], t[(i + 1)%4]);
    65             }
    66             for (int i = 0; i < 4; i++) {
    67                 if (Across(line, rec[i]))
    68                 {
    69                     flag = 1;
    70                     break;
    71                 }
    72             }
    73             if(line.start.x>=min(xl,xr)&&line.start.x<=max(xr,xl)&&line.start.y>=min(yl,yr)&&line.start.y<=max(yl,yr) ||
    74                 line.end.x >= min(xl, xr) && line.end.x <= max(xr, xl) && line.end.y >= min(yl, yr) && line.end.y <= max(yl, yr))
    75                     flag = 1;//判断是否点在矩形内部
    76             if (flag == 1)
    77                 cout << "T" << endl;
    78             else
    79                 cout << "F" << endl;
    80         }
    81     }
    82     return 0;
    83 }
    View Code

     

 

转载于:https://www.cnblogs.com/FlyerBird/p/9315319.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值