题目:

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

题意:给出一条线段和一个矩形 判断两者是否相交
思路:就直接暴力判断 但是要考虑一些边界情况 曾经在判断线段是否在矩形内的时候莫名其妙wa

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const double eps=1e-;
int n;
double x,y,xx,yy,tx,ty,txx,tyy; int dcmp(double x){
if(fabs(x)<eps) return ;
if(x<) return -;
else return ;
} struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x=_x,y=_y;
}
Point operator + (const Point &b) const{
return Point(x+b.x,y+b.y);
}
Point operator - (const Point &b) const{
return Point(x-b.x,y-b.y);
}
double operator * (const Point &b) const{
return x*b.x+y*b.y;
}
double operator ^ (const Point &b) const{
return x*b.y-y*b.x;
}
}; struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s=_s,e=_e;
}
}; bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) &&
dcmp((l2.s-l1.e)^(l1.s-l1.e))*dcmp((l2.e-l1.e)^(l1.s-l1.e))<= &&
dcmp((l1.s-l2.e)^(l2.s-l2.e))*dcmp((l1.e-l2.e)^(l2.s-l2.e))<=;
} int main(){
scanf("%d",&n);
while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x,&y,&xx,&yy,&tx,&ty,&txx,&tyy);
double xl=min(tx,txx);
double xr=max(tx,txx);
double ydown=min(ty,tyy);
double yup=max(ty,tyy);
Line line=Line(Point(x,y),Point(xx,yy));
Line line1=Line(Point(tx,ty),Point(tx,tyy));
Line line2=Line(Point(tx,ty),Point(txx,ty));
Line line3=Line(Point(txx,ty),Point(txx,tyy));
Line line4=Line(Point(txx,tyy),Point(tx,tyy));
if(inter(line,line1) || inter(line,line2) || inter(line,line3) || inter(line,line4) || (max(x,xx)<xr && max(y,yy)<yup && min(x,xx)>xl && min(y,yy)>ydown)) printf("T\n");
else printf("F\n");
}
return ;
}

 

POJ 1410 Intersection (线段和矩形相交)的更多相关文章

  1. POJ 1410--Intersection(判断线段和矩形相交)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16322   Accepted: 4213 Des ...

  2. poj 1410 Intersection 线段相交

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

  3. POJ 1410 判断线段与矩形交点或在矩形内

    这个题目要注意的是:给出的矩形坐标不一定是按照左上,右下这个顺序的 #include <iostream> #include <cstdio> #include <cst ...

  4. 线段和矩形相交 POJ 1410

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

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

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

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

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

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

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

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

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

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

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

  10. POJ 1410 Intersection (计算几何)

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

随机推荐

  1. eclipse上配置svn

    eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的“Install New Software”,通过 ...

  2. iOS开发基础篇-transform属性

    一. transform 属性 在OC中,通过 transform 属性可以修改对象的平移.缩放比例和旋转角度. 1)创建“基于控件初始位置”的形变  CGAffineTransformMakeRot ...

  3. .Net Core应用框架Util介绍(一)

    距离上次发文,已经过去了三年半,这几年技术更新节奏异常迅猛,.Net进入了跨平台时代,前端也被革命性的颠覆. 回顾 2015年,正当我还沉迷于JQuery + EasyUi的封装时,突然意识到技术已经 ...

  4. 爬虫基础(四)-----MongoDB的使用

    ------------------------------------------------------------------------摆脱穷人思维 <四> :减少无意义的频繁决策 ...

  5. 创建SVN源库钩子

    在源库的hooks目录下面添加post-commit.bat文件,每次代码该文件会自动执行以保证同步到备份服务器 set SVN_HOME="D:\Program Files\VisualS ...

  6. HDU 3901 Wildcard

    题目:Wildcard 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3901 题意:给一个原串(只含小写字母)和一个模式串(含小写字母.?.* ,*号可替 ...

  7. centos7之添加开机启动服务/脚本

    一.添加开机启动脚本 #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to ...

  8. ABP项目概述

    在系统性介绍整个ABP框架之前,我们首先需要对ABP框架有一个大概的了解,ABP框架的全称是:Asp.Net Boilerplate Project(即Asp.Net 的样板项目)顾名思义就是能够通过 ...

  9. Python——OS模块

    OS模块 OS模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = ...

  10. <Android基础>(三) UI开发 Part 2 ListView

    ListView 1)ListView的简单用法 2)定制ListView界面 3)提升ListView的运行效率 4)ListView的点击事件 3.5 ListView 3.5.1 ListVie ...