描述

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.

输入

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.

输出

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.

样例输入

1
4 9 11 2 1 5 7 1

样例输出

F

题目来源

http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1879

注意点:

1.线段在矩形内算作与矩形相交

2.给出的矩形坐标不一定为先左上后右下,这需要自己改下(x1<x2,y1>y2)

思路:

1.判断线段所在直线是否经过矩形,这个可以判断矩形的各个点是否在线段所在直线的同一边,如果在同一边那么不相交.

2.判断线段的端点是否经过矩形的边

3.答案出来了

#include <iostream>
using namespace std;
struct Node
{double x,y;
};
double f(Node o,Node a ,Node b)
{return (a.x-o.x)*(o.y-b.y)-(o.x-b.x)*(a.y-o.y);
}
bool tong(Node *d , Node *x)
{int ans = 0;for(int i = 0 ; i < 4 ; i++){if(f(d[i],x[0],x[1])>-0.000001 &&f(d[i],x[0],x[1])<0.00001)return false;else if(f(d[i],x[0],x[1])>0)ans++;}if(ans==4 || ans==0)return true;return false;
}
int main()
{int n;Node d[4],x[2];cin>>n;while(n--){double ax,ay,bx,by;cin>>x[0].x>>x[0].y>>x[1].x>>x[1].y>>ax>>ay>>bx>>by;d[0].x = min(ax,bx); d[0].y = max(ay,by);d[1].x = max(ax,bx); d[1].y = min(ay,by);d[2].x = d[0].x ; d[2].y = d[1].y;d[3].x = d[1].x ; d[3].y = d[0].y;if(tong(d,x)){cout<<"F"<<endl;}else if(max(x[0].x,x[1].x)<min(d[0].x,d[1].x) ||min(x[0].x,x[1].x)>max(d[0].x,d[1].x) ||max(x[0].y,x[1].y)<min(d[0].y,d[1].y) ||min(x[0].y,x[1].y)>max(d[0].y,d[1].y)){cout<<"F"<<endl;}else{cout<<"T"<<endl;}}return 0;
}

附上一些测试数据(来源于网络):

68
4 9 11 2 1 1 7 5
11 2 4 9 1 1 7 5
12 12 24 24 19 5 25 17
4 6 15 9 1 1 11 11
19 5 25 17 12 12 24 24
0 18 8 12 1 1 11 11
2 4 4 2 1 1 11 11
-4 9 -11 2 -1 1 -7 5
-11 2 -4 9 -1 1 -7 5
-12 12 -24 24 -19 5 -25 17
-4 6 -15 9 -1 1 -11 11
-19 5 -25 17 -12 12 -24 24
0 18 -8 12 -1 1 -11 11
-2 4 -4 2 -1 1 -11 11
4 -9 11 -2 1 -1 7 -5
11 -2 4 -9 1 -1 7 -5
12 -12 24 -24 19 -5 25 -17
4 -6 15 -9 1 -1 11 -11
19 -5 25 -17 12 -12 24 -24
0 -18 8 -12 1 -1 11 -11
2 -4 4 -2 1 -1 11 -11
-4 -9 -11 -2 -1 -1 -7 -5
-11 -2 -4 -9 -1 -1 -7 -5
-12 -12 -24 -24 -19 -5 -25 -17
-4 -6 -15 -9 -1 -1 -11 -11
-19 -5 -25 -17 -12 -12 -24 -24
0 -18 -8 -12 -1 -1 -11 -11
-2 -4 -4 -2 -1 -1 -11 -11
9 1 9 2 4 3 9 6
9 2 9 1 4 3 9 6
10 3 13 3 4 3 9 6
13 3 10 3 4 3 9 6
10 6 14 6 4 3 9 6
14 6 10 6 4 3 9 6
9 7 9 10 4 3 9 6
9 10 9 7 4 3 9 6
4 7 4 10 4 3 9 6
4 10 4 7 4 3 9 6
0 6 3 6 4 3 9 6
3 6 0 6 4 3 9 6
1 3 3 3 4 3 9 6
3 3 1 3 4 3 9 6
4 0 4 2 4 3 9 6
4 2 4 0 4 3 9 6
5 3 8 5 4 3 9 6
8 5 5 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
6 4 6 5 4 3 9 6
6 5 6 4 4 3 9 6
4 3 9 6 4 3 9 6
9 6 4 3 4 3 9 6
4 3 5 4 4 3 9 6
5 4 4 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
5 3 9 3 4 3 9 6
9 3 5 3 4 3 9 6
4 4 4 5 4 3 9 6
4 5 4 4 4 3 9 6
4 3 4 5 4 3 9 6
4 5 4 3 4 3 9 6
4 3 4 6 4 3 9 6
4 6 4 3 4 3 9 6
9 2 9 5 4 3 9 6
9 5 9 2 4 3 9 6
9 2 9 7 4 3 9 6
9 7 9 2 4 3 9 6
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T

1879: Intersection相关推荐

  1. CF 1093 E. Intersection of Permutations

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

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

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

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

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

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

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

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

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

  6. Leetcode: Intersection of Two Arrays

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

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

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

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

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

  9. Intersection of Two Linked Lists 解答

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

最新文章

  1. oracle无法删除app文件,DG-主库删除日志备库无法应用-求解决办法
  2. Android应用程序窗口(Activity)的窗口对象(Window)的创建过程分析
  3. python 获取行列号两个数组相等_python – 寻找在numpy中找到两个相等长度数组之间精确重叠的最快方法...
  4. magento邮件使用php,Magento订单成功无邮件发送
  5. 浅谈数学中的化归原则
  6. 培训笔记——2019年性能测试
  7. Redis 6.0 源码阅读笔记(6) -- Set 数据类型源码分析
  8. 正则表达式,时间戳和日期互相转换
  9. 传输预编码matlab,无线通信-预编码-MATLAB代码合集-毕设专用.zip
  10. 学生信息管理系统优化问题汇总
  11. 使用python实现arp欺骗
  12. 飞思卡尔16位单片机(三)——GPIO输出功能测试
  13. ACDSee将捆绑雅虎助手,广大ACDSee用户有难了
  14. Java-面试-逻辑题
  15. MTK 修改ro.hardware 获取cpu 和固件版本号方法
  16. C语言文件指针偏移的使用(点阵字库txt文件取字)
  17. 迅雷下载原理和P2p技术
  18. ESP32 通过NVS存储WiFi账号和密码至Flash
  19. pcf8563c语言程序,PCF8563T标准驱动源程序
  20. 第10章第6节:使用iSlide对幻灯片中的多张图片进行环形布局 [PowerPoint精美幻灯片实战教程]

热门文章

  1. android短信发不了图片,手机发不出短信怎么办?-安卓手机发不出短信的解决方法 - 河东软件园...
  2. 5G/NR 终于明白5G原来是这样
  3. Anomaly Detection 入门概述
  4. win11自带杀毒软件怎么关闭 windows11关闭自带杀毒软件的步骤
  5. python 存储图片_使用python存储网页上的图片实例
  6. 天勤python_天勤量化策略库:R-Breaker策略(难度:初级)
  7. C语言大型连续剧(第一集)——Hello!C先生
  8. VSCode(Visual Studio Code)整合Git
  9. 下载和安装Tcl/Tk:
  10. 循环日程表(递归、分治)