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. 软工实践 - 第二十八次作业 Beta 冲刺(6/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10146478.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...

  2. EasyUI 显示表单数据 小记

    界面图:

  3. WIN8 打开图片内置管理员无法激活此应用

    1.运行 gpedit.msc 2.计算机配置.windows设置.安全设置.本地策略.安全选项.“用户账户控制用于内置管理员账户的管理员批准模式”  改为已启用 3.重启电脑

  4. [bzoj4860] [BeiJing2017]树的难题

    Description 给你一棵 n 个点的无根树.树上的每条边具有颜色. 一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci.对于一条树上的简单路径,路径上经过的所有边按顺序组成一 ...

  5. [Leetcode] The minimum depth of binary tree二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. 理解JavaScript的function

    JavaScript中最有特色而又让你困惑的function算一个了,下面看一下常用操作: function doit(){ ..... } doit(); JavaScript中的函数我们可以把它当 ...

  8. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) C

    C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. HDU3038:How Many Answers Are Wrong(带权并查集)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  10. HDU2255:奔小康赚大钱(KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...