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. Jlink 软件断点和硬件断点

    调试2440 RAM拷贝至SDRAM遇到的问题 汇编代码主要是初始化一些寄存器,关狗,初始化时钟,初始化存储管理器以便访问内存,然后将SoC上4k RAM数据拷贝至SDRAM,然后在SRAM里面运行, ...

  2. IO流分类详细介绍和各种字节流类介绍与使用 过滤流 字节流

    Java基础笔记 – IO流分类详细介绍和各种字节流类介绍与使用 过滤流 字节流本文由 arthinking 发表于627 天前 ⁄ Java基础 ⁄ 评论数 1 ⁄ 被围观 2,036 views+ ...

  3. mac --snip 滚动截屏

    1.snip 下载配置:https://jingyan.baidu.com/article/fec4bce2458d03f2618d8b8e.html 2.mac的火狐浏览器好像不支持,必须在sofa ...

  4. [洛谷P3693]琪露诺的冰雪小屋

    题目大意:琪露诺的冰雪小屋,我做的第一道大模拟题. 题解:模拟... 卡点:无数小错误,要是没有写一点调一点,那大概是永远调不出来了... C++ Code: #include <cstdio& ...

  5. [国家集训队]Crash的数字表格 / JZPTAB 莫比乌斯反演

    ---题面--- 题解: $$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}{\frac{ij}{gcd(i, j)}}$$ 改成枚举d(设n < m) $$ans ...

  6. CF840C On the Bench 解题报告

    CF840C On the Bench 题意翻译 给定\(n\) \((1≤n≤300)\) 个数,求问有多少种排列方案使得任意两个相邻的数之积都不是完全平方数.由于方案数可能很大,输出方案数 \(m ...

  7. Why is the ibdata1 file continuously growing in MySQL?

    We receive this question about the ibdata1 file in MySQL very often in Percona Support. The panic st ...

  8. bzoj 5092 [Lydsy1711月赛]分割序列 贪心高维前缀和

    [Lydsy1711月赛]分割序列 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 213  Solved: 97[Submit][Status][Dis ...

  9. 利用WebStorm来管理你的Github

    什么是Github Github是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目,以最简单的方式来说,其实就是一个代码库,上面有全世界无数优秀的码农上传自己的作品和大家共享(当然 ...

  10. BZOJ 1598 牛跑步

    牛跑步 [问题描述] BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M ...