poj 1410 Intersection (判断线段与矩形相交 判线段相交)

题目链接

Intersection

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12040   Accepted: 3125

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

题意:

有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交

分析:有很多坑点,

①给出的矩形顶点的坐标需要自己重新排下序再使用,看discuss说所谓的top等并不是严格指上方,只是一个相对的参考,所以要重新排下序再用。

②因为题目里面说了矩形的内部也算矩形的一部分,所以线段在矩形内部是认为和矩形相交的。

③在判断线段与矩形四个边是否有交点时,要注意对非规范相交的判定,当线段和边共线且不相交时叉积也为0。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 #define LL __int64
 8 const int maxn = 1e2 + 10;
 9 const double eps = 1e-8;
10 using namespace std;
11
12 struct node
13 {
14     double x, y;
15 }l1, l2, a, b, c, d;
16
17 double mult(node a, node b, node c) //叉积
18 {
19     return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
20 }
21 bool solve() //判断点在矩形里
22 {
23     if((l1.x>=a.x && l1.x<=b.x && l1.y>=d.y && l1.y<=a.y) || (l2.x>=a.x && l2.x<=b.x && l2.y>=d.y && l2.y<=a.y))
24     return true;
25     return false;
26 }
27 bool solve2(node a, node b, node c, node d) //判断线段ab与线段cd是否相交,相交返回true,包含线段重合的情况,已测试。
28 {
29     if(max(a.x, b.x)<min(c.x, d.x)) return false;
30     if(max(a.y, b.y)<min(c.y, d.y)) return false;
31     if(max(c.x, d.x)<min(a.x, b.x)) return false;
32     if(max(c.y, d.y)<min(a.y, b.y)) return false;
33     if(mult(c, d, a)*mult(c, d, b)>0)
34         return false;
35     if(mult(a, b, c)*mult(a, b, d)>0)
36        return false;
37     return true;
38 }
39 int main()
40 {
41     int n;
42     scanf("%d", &n);
43     int xx = 1;
44     while(n--)
45     {
46         scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &l1.x, &l1.y, &l2.x, &l2.y, &a.x, &a.y, &c.x, &c.y);
47         if(a.x > c.x) {
48             b.x = a.x;
49             d.x = c.x;
50         }
51         else {
52             b.x = c.x;
53             d.x = a.x;
54         }
55         if(a.y > c.y) {
56             b.y = a.y;
57             d.y = c.y;
58         }
59         else {
60             b.y = c.y;
61             d.y = a.y;
62         }
63         a.x = d.x; a.y = b.y;
64         c.x = b.x; c.y = d.y;
65         if(solve())
66             cout<<"T"<<endl;
67         else if(solve2(l1, l2, a, b)||solve2(l1, l2, a, d)||solve2(l1, l2, c, b)||solve2(l1, l2, c, d))
68         cout<<"T"<<endl;
69         else cout<<"F"<<endl;
70     }
71     return 0;
72 }
时间: 2024-08-07 04:14:17

poj 1410 Intersection (判断线段与矩形相交 判线段相交)的相关文章

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

POJ 1410 Intersection --几何,线段相交

题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单纯做了两次跨立实验,在下图这种情况是错误的: 这样的话线段与右边界的两次跨立实验(叉积<=0)都会通过,但是并不相交. 所以要加快速排斥. 还有就是这题题目说给出的不一定是左上角,右下角依次的顺序.所以干脆重新自己定义左上角,右下角. 代码: #include <iostream> #inc

简单几何(线段相交) POJ 1410 Intersection

题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /************************************************ * Author :Running_Time * Created Time :2015/10/27 星期二 13:17:49 * File Name :POJ_1410.cpp ******************************************

POJ 1410 Intersection(计算几何)

题目链接:id=1410">Intersection 推断线段与矩形的关系.与矩形相交打印T,否则打印F. 坑题,精度. . .. 思路就是,先推断 线段是否在矩形里面,再推断线段和两条对角线的关系,利用叉积模板就可以 測试数据有个坑,就是 左上角的坐标并不一定比右下角的小. ..这根本不符合题意嘛 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring&

POJ 1410 Intersection

这题我就是用最原始的思考方法,其中有许多细节要注意.主体思想就是四条边分别和线段比较. 线段在矩形内要考虑. 我的代码有点乱有点长,其中有的部分可以写成函数. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int x_s,y_s,x_e,y_e,x_l,y_t,x_r,y_b; bool f1(int x) { retu

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

题目: 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 doe

POJ 1410 Intersection 数据错误

题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) 和 (xright,ybottom)不是按顺序给出的,需要自己判断下顺序. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algori

poj1410(判断线段和矩形是否相交)

题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两端点是否在矩形内(因为是矩形,即凸多边形,直接用叉积判断即可,如果是一般的多边形,需要用射线法判断.) AC code: #include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib>