原题传送门:http://poj.org/problem?id=1410
博主的中文题面(放心提交):
https://www.luogu.org/problemnew/show/T22919

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 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

题目大意

判断线段和矩形是否相交

题解

枚举四条边计算交点,特判共线和在内部的情况

线段在矩形内部也算相交,先输入线段再输入矩形,矩形的端点不是按顺序给的,只能保证倒数1、3个是横坐标,2、4个是纵坐标,共线的时候叉乘出来会WA,需要写个特判(即under函数)。

代码
#include<cstdio>
#include<algorithm>
#define db double
using namespace std;
const db eps=1e-8;
struct pt{db x,y;};
pt s,e,lu,ld,ru,rd;
int sig(db x){return (x>eps)-(x<-eps);}
db cross(pt a,pt b,pt c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
void in()
{scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y,&lu.x,&lu.y,&rd.x,&rd.y);if(lu.x>rd.x) swap(lu.x,rd.x);if(lu.y<rd.y) swap(lu.y,rd.y);ld.x=lu.x;ld.y=rd.y;ru.x=rd.x;ru.y=lu.y;
}
bool check(pt x)
{int d1,d2,d3,d4;d1=sig(cross(x,lu,ru));d2=sig(cross(x,ld,rd));d3=sig(cross(x,lu,ld));d4=sig(cross(x,ru,rd));if(d1*d2<0&&d3*d4<0) return 1;return 0;
}
bool under(pt a,pt b,pt c)
{return (min(b.x,c.x)<=a.x&&a.x<=max(b.x,c.x)&&min(b.y,c.y)<=a.y&&a.y<=max(b.y,c.y));
}
bool test(pt a,pt b)
{int d1,d2,d3,d4;d1=sig(cross(s,a,b));d2=sig(cross(e,a,b));d3=sig(cross(a,s,e));d4=sig(cross(b,s,e));if(d1*d2<0&&d3*d4<0) return 1;if(!d1&&under(s,a,b)) return 1;if(!d2&&under(e,a,b)) return 1;if(!d3&&under(a,s,e)) return 1;if(!d4&&under(b,s,e)) return 1;return 0;
}
bool check2()
{int d1,d2,d3,d4;d1=test(lu,ru);d2=test(ru,rd);d3=test(rd,ld);d4=test(ld,lu);if(d1||d2||d3||d4) return 1;return 0;
}
void ac()
{if(check(s)&&check(e)) printf("T\n");else if(check2()) printf("T\n");else printf("F\n");
}
int main()
{
//  freopen("data.out","w",stdout);int T;scanf("%d",&T);for(int i=1;i<=T;++i){in();ac();}return 0;
}

POJ1410 Intersection相关推荐

  1. [poj1410]Intersection

    题目大意:求线段与实心矩形是否相交. 解题关键:转化为线段与线段相交的判断. #include<cstdio> #include<cstring> #include<al ...

  2. CF 1093 E. Intersection of Permutations

    E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...

  3. 【leetcode75】Intersection of Two Arrays(数组的交集)

    题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: ...

  4. 一个Apache CollectionUtils.intersection 方法的简单问题

    2019独角兽企业重金招聘Python工程师标准>>> 今天在使用CollectionUtils.intersection()  的时候,发现个问题,明明两个集合中有几个完全相同的类 ...

  5. Python计算两个numpy数组的交集(Intersection)实战:两个输入数组的交集并排序、获取交集元素及其索引、如果输入数组不是一维的,它们将被展平(flatten),然后计算交集

    Python计算两个numpy数组的交集(Intersection)实战:两个输入数组的交集并排序.获取交集元素及其索引.如果输入数组不是一维的,它们将被展平(flatten),然后计算交集 目录

  6. python 集合set 的三大方法intersection union difference来处理文氏图

    TODO - 练习:A或B,但不能同时包含 编写一个函数,将两个集合(set_a和set_b)作为输入,并返回一个新的集合,其中包含set_a或set_b中的元素,但不包含两者兼有的元素. 在上面的文 ...

  7. Leetcode: Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...

  8. Intersection of Two Linked Lists——经典问题

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  10. Intersection of Two Linked Lists 解答

    Question Write a program to find the node at which the intersection of two singly linked lists begin ...

最新文章

  1. linux停止客户端,linux – 从客户端打印motd停止ssh登录?
  2. 【HDU/算法】最短路问题 杭电OJ 2544 (Dijkstra,Dijkstra+priority_queue,Floyd,Bellman_ford,SPFA)
  3. 音视频技术开发周刊 | 233
  4. 不是有效的win32应用程序_杀毒软件有坑!三分之二的安卓杀毒软件的“主业”并不是杀毒...
  5. P2260 [清华集训2012]模积和,P2834 能力测验(二维除法分块)
  6. 组个最小数C语言pta,PTA|C语言:组个最小数
  7. 多线程数据下载(akshare)
  8. java redis pipeline,巧用 Redis pipeline 命令,解决真实的生产问题
  9. 应对大数据分析的几个方法
  10. android桌面工具,不一样的Android桌面小工具
  11. win10写java工具_推荐三款录屏工具:也许是电脑录屏最实用的软件
  12. Blender建模:如何改变窗口布局?
  13. 自己不优秀,认识谁都没用
  14. jena 查询 java_使用SPARQL和Jena查询DBpedia
  15. Firefox个性化教程
  16. FFT专题:IFFT后信号如何重建
  17. 旅游类APP-Android模块分析
  18. 目标检测、追踪梳理:帧差法、光流法、背景减除法
  19. vue项目,h5图片放大后,支持手指缩放功能
  20. com.android.ide.common.signing.KeytoolException: Failed to read key AndroidDebugKey from store

热门文章

  1. C++/QT控制通过VISA控制硬件设备,超级容易学会的控制硬件方法
  2. android手机和荣耀哪个版本好,【求测评】荣耀v40轻奢版与荣耀X10哪款更好?图文爆料分析...
  3. 浮窗 动画特效 android,悬浮窗能实现自定Animation动画效果吗?
  4. SpringBoot2.0高级案例(02) :整合 RocketMQ ,实现请求异步处理
  5. 第 7 章 Neutron - 071 - 详解 ML2 Core Plugin(I)
  6. Unity基础-图形渲染
  7. DELPHI 对象的本质 VMT
  8. MvvmCross框架在XamarinForms中的使用入门
  9. C# ToString
  10. 一些控制鼠标的例子!