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,我的等级还是不够啊。
  • 最后只要判断给定线段是否和矩形的四条边相交,以及线段是否在矩形内,线段是否在矩形内部可以用线段的端点是否在矩形内部来判断。
  •  #include<iostream>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int n;
    double xs, ys, xe, ye, xl, yl, xr, yr;
    const double eps = 1.0e8;
    typedef struct point {
    double x;
    double y;
    point(double a, double b) {
    x = a;
    y = b;
    }
    point() { }
    }point;
    typedef struct edge {
    point start;
    point end;
    edge(point a, point b) {
    start = a;
    end = b;
    }
    edge() { }
    edge(edge &t) {
    start = t.start;
    end = t.end;
    }
    }edge;
    point t[];
    edge line;
    edge rec[]; inline double dabs(double a) { return a < ? -a : a; }
    inline double max(double a, double b) { return a > b ? a : b; }
    inline double min(double a, double b) { return a < b ? a : b; }
    double multi(point p1, point p2, point p0) {
    return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y);
    }
    bool Across(edge v1, edge v2) {
    if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) &&
    max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) &&
    max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) &&
    max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) &&
    multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v2.start) >= &&
    multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v1.start) >=
    )
    return true;
    return false;
    }
    int main(void) {
    while (cin >> n) {
    while (n-- > ) {
    int flag = ;
    cin >> xs >> ys >> xe >> ye >> xl >> yl >> xr >> yr;
    line = edge(point(xs, ys), point(xe, ye));
    t[] = point(xl, yl), t[] = point(xr, yl);
    t[] = point(xr, yr), t[] = point(xl, yr);
    for (int i = ; i < ; i++) {
    rec[i] = edge(t[i], t[(i + )%]);
    }
    for (int i = ; i < ; i++) {
    if (Across(line, rec[i]))
    {
    flag = ;
    break;
    }
    }
    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) ||
    line.end.x >= min(xl, xr) && line.end.x <= max(xr, xl) && line.end.y >= min(yl, yr) && line.end.y <= max(yl, yr))
    flag = ;//判断是否点在矩形内部
    if (flag == )
    cout << "T" << endl;
    else
    cout << "F" << endl;
    }
    }
    return ;
    }
 

POJ 1410--Intersection(判断线段和矩形相交)的更多相关文章

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

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

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

    题目: Description You are to write a program that has to decide whether a given line segment intersect ...

  3. [POJ 1410] Intersection(线段与矩形交)

    题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

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

    Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...

  5. 线段和矩形相交 POJ 1410

    // 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...

  6. poj 1410 Intersection 线段相交

    题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...

  7. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

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

    题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两 ...

  9. Intersection--poj1410(判断线段与矩形的关系)

    http://poj.org/problem?id=1410 题目大意:给你一个线段和矩形的对角两点  如果相交就输出'T'  不想交就是'F' 注意: 1,给的矩形有可能不是左上 右下  所以要先判 ...

随机推荐

  1. 【学习笔记】HTML基础:使用html制作网页

    一.初识HTML 1.什么是HTML? Hyper Text Markup Language(超文本标记语言) 扩展XML:Extendsible  Markup Language(可扩展性标记语言) ...

  2. 对SNMP4J的一些封装

    SNMP4J是一个开源的,用Java实现的snmp协议.其中提供了一下API,在这些API上面封装了一些方法,比如SNMP的get-request请求,get-next-request请求等 如果不了 ...

  3. Git学习1:Git起步

    本系列文章部分原理和命令相关内容是从 https://git-scm.com/book/zh/v2 摘录,软件实际使用是总结自己的实践经验成文. 1. 关于版本控制 版本控制是一种记录一个或若干文件内 ...

  4. UIRecorder安装与使用

    继vue单元测试,将进行vue的e2e测试学习. 学习点: 安装uirecorder 用工具(UI Recorder)录制测试脚本 测试脚本的回放 本文意在安装UI Recorder,并且利用该工具进 ...

  5. HCNA修改OSPF基准带宽

    1.拓扑图 2.R1配置ip开启OSPF The device is running! <Huawei>sysEnter system view, return user view wit ...

  6. FFT算法实现——基于GPU的基2快速傅里叶变换

    最近做一个东西,要用到快速傅里叶变换,抱着蛋疼的心态,自己尝试写了一下,遇到一些问题. 首先看一下什么叫做快速傅里叶变换(FFT)(来自Wiki): 快速傅里叶变换(英语:Fast Fourier T ...

  7. CSS基础语法(三) CSS的6种特性

    样式表常用写法及特性(组合.继承.关联性.权值性.层叠性.重要性) 1.样式的组合:把具有相同声明定义的选择符组合在一起,并用逗号隔开.-例如:段落元素p.单元格元素td和类c1可以使用相同样式: p ...

  8. Locust性能测试2 分布式运行

    locust分布式可以是本机多进程,也可以是本机作为master,其他机器作slave. 试一下本机的多进程运行: 1  控制台输入 locust -f 脚本路径 --master 2  打开另一个控 ...

  9. position中需要注意的地方

    relative是相对元素本身位置进行移位,但不会改变本身位置的大小 本身的位置 移位后,可以看到,p5的位置还是在那,并不会自动往上走,也就是p2的位置原来所占据的位置不变的.不会因为偏移而改变布局 ...

  10. [USACO15OPEN]haybales Trappe…

    嘟嘟嘟 刚开始我以为如果这头牛撞开一个干草堆的话,获得的冲刺距离只有新增的部分,但实际上是加上原来的部分的. 暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度O(n2). 优化想起来也不难 ...