题目链接

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。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = 1e2 + ;
const double eps = 1e-;
using namespace std; struct node
{
double x, y;
}l1, l2, a, b, c, d; double mult(node a, node b, node c) //叉积
{
return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
}
bool solve() //判断点在矩形里
{
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))
return true;
return false;
}
bool solve2(node a, node b, node c, node d) //判断线段ab与线段cd是否相交,相交返回true,包含线段重合的情况,已测试。
{
if(max(a.x, b.x)<min(c.x, d.x)) return false;
if(max(a.y, b.y)<min(c.y, d.y)) return false;
if(max(c.x, d.x)<min(a.x, b.x)) return false;
if(max(c.y, d.y)<min(a.y, b.y)) return false;
if(mult(c, d, a)*mult(c, d, b)>)
return false;
if(mult(a, b, c)*mult(a, b, d)>)
return false;
return true;
}
int main()
{
int n;
scanf("%d", &n);
int xx = ;
while(n--)
{
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);
if(a.x > c.x) {
b.x = a.x;
d.x = c.x;
}
else {
b.x = c.x;
d.x = a.x;
}
if(a.y > c.y) {
b.y = a.y;
d.y = c.y;
}
else {
b.y = c.y;
d.y = a.y;
}
a.x = d.x; a.y = b.y;
c.x = b.x; c.y = d.y;
if(solve())
cout<<"T"<<endl;
else if(solve2(l1, l2, a, b)||solve2(l1, l2, a, d)||solve2(l1, l2, c, b)||solve2(l1, l2, c, d))
cout<<"T"<<endl;
else cout<<"F"<<endl;
}
return ;
}

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

  1. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

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

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

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

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

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

    题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...

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

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  6. poj 1410 Intersection 线段相交

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

  7. POJ 1410 Intersection (计算几何)

    题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...

  8. POJ 1410 Intersection(计算几何)

    题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...

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

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

随机推荐

  1. <linux硬件及硬盘分区>关于硬盘的规划和使用细节

    ps:期末考试 终于结束了,这下我也终于有时间开始继续经营我的博客.这个学期上的一些课真的非常有用,感觉很多课程细地讲都可以写成非常精致的技术博文,比如流水线技术,数据库的一些技术,大学里的考试考的内 ...

  2. Idea 使用过程中遇到的问题记录

    1.在Idea启动Tomcat,有时候提示项目启动 failed,请查看日志,此时的日志在Tomcat主目录的日志文件中是没有的,此时的日志记录在: C:\Users\wanhua.lu\.Intel ...

  3. linux 文件存取 软硬联接的区别

    一.linux文件存取过程 在linux系统中根目录是自引用的,比如要找 /etc/sysconfig/networkscripts/ifcfg-0文件 先根据根目录/ 的inode号,在inode ...

  4. java:类集操作总结

    java:类集操作总结 1.List接口允许有重复的元素,Set接口中不允许有重复的元素 2.ArrayList,和Vector的区别 3.set依靠equals和hashCode区分 4.TreeS ...

  5. php 数据处理--合并,拆分,追加,去重

    1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加. 示例代码: <?php $arr = ...

  6. Python编程-一个小爬虫工具的实现过程

    需求描述: 1,打开网站: 2,获取网站的文件内容: 3,返回保存到文件中: 这里的就用到了多线程的方法 import requests,threading,time def write_html(u ...

  7. jQuery插件--图片文字向上向左循环滚动

    需要引用jquery 调用非常简单: 一. 向上滚动 $(".scroll_two").jScroll({vertical: true}); <div class=" ...

  8. php中socket的使用

    php中使用socket在服务器端主要使用这么几个函数: 1/$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)创建一个socket 2/sock ...

  9. 本机不装Oracle,使用plsql连接远程Oracle的方法

    由于Oracle的庞大,有时候我们需要在只安装Oracle客户端如plsql.toad等的情况下去连接远程数据库,可是没有安装Oracle就没有一切的配置文件去支持.最后终于发现一个很有效的方法,Or ...

  10. BZOJ1727:[Usaco2006 Open]The Milk Queue挤奶队列

    我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.lydsy.com/JudgeOnl ...