Intersection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13528   Accepted: 3521

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

今天又懂了一个~~非规范相交.
规范相交模板(两条线段只有一个交点):
///叉积
double mult(Point a, Point b, Point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
} ///a, b为一条线段两端点c, d为另一条线段的两端点 相交返回true, 不相交返回false
bool isCross(Point a, Point b, Point c, Point d)
{
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, b, a)*mult(b, d, a)<)return false;
if (mult(a, d, c)*mult(d, b, c)<)return false;
return true;
}

非规范相交模板(可以理解为重合)

const double eps = 1e-;
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dblcmp(double m) {
if (fabs(m) < eps) return ;
return m > ? : -;
}
bool isCross(Point a,Point b,Point c,Point d){
if (dblcmp(max(a.x, b.x)-min(c.x, d.x)) >= && dblcmp(max(c.x, d.x)-min(a.x, b.x)) >=
&& dblcmp(max(a.y, b.y)-min(c.y, d.y)) >= && dblcmp(max(c.y, d.y)-min(a.y, b.y)) >=
&& dblcmp(cross(a, d, c)*cross(b, d, c)) <= && dblcmp(cross(c, b, a)*cross(d, b, a)) <= )
return true;
return false;
}

题目很坑,有可能输入的不是左上角和右下角...
判断很简单,就是四条边都拿过去判断一下..然后判断一下线段是不是在矩形内。

///判断线段与矩形是否相交
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps = 1e-;
struct Point{
double x,y;
};
struct Line{
Point a,b;
}line[];
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dblcmp(double m) {
if (fabs(m) < eps) return ;
return m > ? : -;
}
bool isCross(Point a,Point b,Point c,Point d){
if (dblcmp(max(a.x, b.x)-min(c.x, d.x)) >= && dblcmp(max(c.x, d.x)-min(a.x, b.x)) >=
&& dblcmp(max(a.y, b.y)-min(c.y, d.y)) >= && dblcmp(max(c.y, d.y)-min(a.y, b.y)) >=
&& dblcmp(cross(a, d, c)*cross(b, d, c)) <= && dblcmp(cross(c, b, a)*cross(d, b, a)) <= )
return true;
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
Line l;
double lx,ly,rx,ry;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l.a.x,&l.a.y,&l.b.x,&l.b.y,&lx,&ly,&rx,&ry);
if(lx>rx) swap(lx,rx);
if(ly<ry) swap(ly,ry);
line[].a.x = lx,line[].a.y =ly,line[].b.x = rx,line[].b.y=ly;
line[].a.x = lx,line[].a.y =ly,line[].b.x = lx,line[].b.y=ry;
line[].a.x = lx,line[].a.y =ry,line[].b.x = rx,line[].b.y=ry;
line[].a.x = rx,line[].a.y =ly,line[].b.x = rx,line[].b.y=ry;
int flag = false;
for(int i=;i<=;i++){
if(isCross(l.a,l.b,line[i].a,line[i].b)){
flag = true;
break;
}
}
if(max(l.a.x,l.b.x)<rx&&min(l.a.x,l.b.x)>lx&&max(l.a.y,l.b.y)<ly&&min(l.a.y,l.b.y)>ry) flag = true;
if(flag) printf("T\n");
else printf("F\n");
}
return ;
}

hdu 1410(直线与矩形相交)的更多相关文章

  1. hdu 3304(直线与线段相交)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12042   Accepted: 3808 Descrip ...

  2. 线段和矩形相交 POJ 1410

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

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

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

  4. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  5. C:矩形相交、相包含、相离关系判断

    矩形相交 包含 问题.参考 假定矩形是用一对点表达的(minx, miny) (maxx, maxy),那么两个矩形    rect1{(minx1, miny1)(maxx1, maxy1)}    ...

  6. 判断直线与线段相交 POJ 3304 Segments

    题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...

  7. poj 3304(直线与线段相交)

    传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...

  8. POJ 3304 Segments[直线与线段相交]

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  9. Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)

    C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...

随机推荐

  1. 剑指offer:正则表达式匹配

    目录 题目 解题思路 具体代码 题目 题目链接 剑指offer:正则表达式匹配 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符.表示任意一个字符,而*表示它前面的字符可以 ...

  2. 配置apache反向代理进行跨域

    配置apache反向代理 打开配置文件httpd.conf 开启 proxy_http_module 和 proxy_module 模块,将#号删除 #LoadModule proxy_module ...

  3. gdb调试命令的使用及总结

    gdb调试命令的使用及总结 gdb是一个在UNIX环境下的命令行调试工具.如果需要使用gdb调试程序,请在gcc时加上-g选项.下面的命令部分是简化版,比如使用l代替list等等. 1.基本命令 命令 ...

  4. [NOIP2016]愤怒的小鸟 DP

    ---题面--- 题解: 首先观察数据范围,n <= 18,很明显是状压DP.所以设f[i]表示状态为i时的最小代价.然后考虑转移. 注意到出发点(0, 0)已经被固定,因此只需要2点就可以确定 ...

  5. 理解First Chance和Second Chance避免单步调试

    原文链接地址:http://blog.csdn.net/Donjuan/article/details/3859160 在现在C++.Java..Net代码大行其道的时候,很多代码错误(Bug)都是通 ...

  6. 【BZOJ 1082】[SCOI2005]栅栏 二分+dfs

    对于最优解我们发现所有的最优解都可以是前多少多少个,那么我们就二分这个前多少多少个,然后用dfs去判解,我们发现在dfs的过程中如果不剪枝几乎必T,所以我们就需要一些有效的剪枝 I. 我们在枚举过程中 ...

  7. 几个JQuery解析XML的程序例子

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...

  8. HDU4289:Control(最小割)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. 怎么让Intellj Idea 把数据库的表映射成hibernate的domain对象

    步骤如下: 第一步:连接数据源: 点击:idea右边的database.如下图所示: 或者你依次点击:view-->Tool windows--->database 然后你将看在如下点击下 ...

  10. git上传本地项目

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...