Intersection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12822   Accepted: 3347

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

解题思路:

题意很清楚,就是判断一个线段是否和矩形相交。而所谓“相交”,但是这个相交的定义是线段在矩形内或者线段与矩形的边相交。

判断方法:

判断线段的两端点是否在矩形内,若是,则线段在矩形内。

判断线段是否与矩形相交,即是否和矩形的四条边中的任意一条边相交(规范相交和不规范相交都算)。

其实这是一个很好的模板题,注意处理下就可以了!

ACcode:

#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define PI 3.1415926
#define MOD 10000000007
#define N 1000005
#define INF 0x7fffffff
using namespace std;
typedef long long LL;
const double eps=1e-;
//点
struct point
{
double x,y;
};
//线段
struct line
{
point p1,p2;
} l;
//面
struct poly
{
int n;//几个面
double area;
point plist[];
} rec;
//点乘
double dotdel(double x1,double y1,double x2,double y2)
{
return x1*x2+y1*y2;
}
//叉乘
double crossmul(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
//判断是否为0,达到一定精度即认为成立
int cmpzero(double d)
{
return (fabs(d)<eps)?:(d>?:-);
}
//右手螺旋定则,1:a在cd右侧,-1:a在cd左侧,0:三点共线
int cross(point a,point c,point d)
{
return cmpzero(crossmul(a.x-c.x,a.y-c.y,d.x-c.x,d.y-c.y));
}
//在cross(a,c,d)==0的基础上,可判断点a是否在cd内部
int between(point a,point c,point d)
{
return cmpzero(dotdel(c.x-a.x,c.y-a.y,d.x-a.x,d.y-a.y))!=;
}
//两线段相交情况:0:不相交,1:规范相交,2:不规范相交(交于端点或重合)
int seg_intersect(point a,point b,point c,point d)
{
int a_cd=cross(a,c,d);
if(a_cd== && between(a,c,d))
return ;
int b_cd=cross(b,c,d);
if(a_cd== && between(a,c,d))
return ;
int c_ab = cross(c, a, b);
if (c_ab == && between(c, a, b))
return ;
int d_ab=cross(d,a,b);
if(d_ab== && between(d,a,b))
return ;
if((a_cd^b_cd)==- && (c_ab^d_ab)==-)
return ;
return ;
}
//使用有向面积法判断点是否在多边形内
bool point_in_poly(point p)
{
double s=0.0;
for(int i=; i<rec.n; i++)
s+=fabs(crossmul(rec.plist[i].x-p.x,rec.plist[i].y-p.y,rec.plist[(i+)%rec.n].x-p.x,
rec.plist[(i+)%rec.n].y-p.y));
if(cmpzero(s-rec.area)==) return true;
else return false;
}
//判断线段是否与多边形相交
bool rec_seg_intersect()
{
if(point_in_poly(l.p1) && point_in_poly(l.p2))
return ;
else if(seg_intersect(l.p1,l.p2,rec.plist[],rec.plist[])
|| seg_intersect(l.p1,l.p2,rec.plist[],rec.plist[])
|| seg_intersect(l.p1,l.p2,rec.plist[],rec.plist[])
|| seg_intersect(l.p1,l.p2,rec.plist[],rec.plist[]))
return ;
return ;
}
//计算多边形面积
void getarea()
{
double s=rec.plist[].y*(rec.plist[rec.n-].x-rec.plist[].x);
for(int i=; i<rec.n; i++)
s+=rec.plist[i].y*(rec.plist[i-].x-rec.plist[(i+)%rec.n].x);
rec.area=s;
}
int main()
{
int T;
double x1,y1,x2,y2,t;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf",&l.p1.x,&l.p1.y,&l.p2.x,&l.p2.y);
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1>x2)
{
t=x1;
x1=x2;
x2=t;
}
if(y2>y1)
{
t=y1;
y1=y2;
y2=t;
}
rec.n=;
rec.plist[].x=x1;
rec.plist[].y=y1;
rec.plist[].x=x1;
rec.plist[].y=y2;
rec.plist[].x=x2;
rec.plist[].y=y2;
rec.plist[].x=x2;
rec.plist[].y=y1;
getarea();
puts(rec_seg_intersect()?"T":"F");
}
return ;
}

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

  1. poj 1410 Intersection 线段相交

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

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

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

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

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

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

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

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

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

  6. POJ 1410 Intersection (计算几何)

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

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

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

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

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

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

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

随机推荐

  1. 201521123078 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 1.互斥访问与同步访问 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么 ...

  2. 201521123020 《Java程序设计》第9周学习总结

    1.本周学习总结 2. 书面作业 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 答:数组越界:不需要 ...

  3. 如何实现Sublime Text3中vue文件高亮显示的最有效的方法

    今天第一次使用Sublime Text3软件,在实现vue文件高亮显示的过程中一直报错,经过了半天时间的不停尝试终于找到了最有效的一种解决方法!错误提示如下: 刚开始尝试了很多方法都不行,只要打开in ...

  4. OSGi-简介(01)

    OSGi是什么? OSGi联盟现在将OSGi定义为一种技术: OSGi技术是指一系列用于定义Java动态化组件系统的标准.这些标准通过为大型分布式系统以及嵌入式系统提供一种模块化架构减少了软件的复杂度 ...

  5. PolarDB · 新品介绍 · 深入了解阿里云新一代产品 PolarDB

    背景意义 云计算为如今的互联网时代提供了更多的计算能力,乃至创造能力,关系型数据库作为所有应用不可或缺的重要部件,开箱即用,高性价加比特性的云数据库深受开发者的喜爱.作为一线的开发和运维人员,在阿里云 ...

  6. MySQL的一点浅显知识

    本人最近看了一本有关于MySQL的书籍<MySQL必知必会>,书中只写了一些基本知识,但是也基本涵盖了所有的MySQL的知识点.其余的比较高级的也只是在基础上进行扩展或者是优化,看完这本书 ...

  7. 【京东详情页】——原生js学习之匿名函数

    一.引言 在js模块中,要给每一个功能封装一个匿名函数.为了更好的理解什么是匿名函数,为什么要用匿名函数,我做了一些查阅和学习. 二.匿名函数 什么是:在创建时,不被任何变量引用的函数. 为什么:节约 ...

  8. CA认证和颁发吊销证书

    摘要:涉及到网络安全这一块,想必大家都听过CA吧.像百度.淘宝.京东等这些知名网站,每年都要花费一笔money来买CA证书.但其实简单的企业内的CA认证,我们自己就可以实现,今天小编我就讲解一下怎么在 ...

  9. django之快速分页

    本文介绍djanog两种分页,第一是普通分页,第二是使用haystack全文检索的分页. 1.django自带分页功能,这个功能非常好用.基本知识点:Django提供了数据分页的类,这些类被定义在dj ...

  10. 51nod 1393 0和1相等串 思路 : map存前缀和

    题目: 思路:把'0'当成数字-1,'1'当成数字1,求前缀和,用map更新当前前缀和最早出现的位置.(用map而不用数组是因为可能会出现负数) 当前缀和的值之前出现过,比如i = 10时,sum = ...