Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12817   Accepted: 3343

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

Source

题意:给一条线段,然后是一个矩形,问线段是否与矩形相交
    kuangbin模版
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#define eps 1e-8
#define maxn 100
using namespace std;
int sgn(double x)
{
if(abs(x) < eps) return ;
if(x<) return -;
else return ;
}
struct Point
{
double x;
double y;
Point(){}
Point(double _x,double _y)
{
x = _x;
y = _y;
}
Point operator -(const Point &a) const
{
return Point(x-a.x,y-a.y);
}
Point operator + (const Point &a) const
{
return Point(x+a.x,y+a.y);
}
double operator *(const Point &a) const
{
return x*a.x+y*a.y;
}
double operator ^(const Point &a) const
{
return x*a.y-y*a.x;
}
};
struct Line
{
Point s;
Point 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) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <=;
}
///判断直线和线段是否相交
bool seg_inter_line(Line l1,Line l2)
{
return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s,l1.e)) <=;
}
bool Onseg(Point p,Line L)
{
return
sgn((L.s-p)^(L.e-p)) == &&
sgn((p.x-L.s.x)*(p.x-L.e.x)) <= &&
sgn((p.y-L.s.y)*(p.y-L.e.y)) <= ;
}
int inConvexpoly(Point a,Point p[],int n)
{
for(int i=;i<n;i++)
{
if(sgn((p[i]-a)^(p[(i+)%n]-a)) < ) return -;
else if(Onseg(a,Line(p[i],p[(i+)%n]))) return ;
}
return ;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
Point s;
Point e;
double x1,y1,x2,y2;
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&s.x,&s.y,&e.x,&e.y,&x1,&y1,&x2,&y2);
if(x1 > x2) swap(x1,x2);
if(y1 > y2) swap(y1,y2);
Point p[];
Line L = Line(s,e);
p[] = Point(x1,y1);
p[] = Point(x2,y1);
p[] = Point(x2,y2);
p[] = Point(x1,y2);
if(inter(L,Line(p[],p[])))
{
printf("T\n");
continue;
}
else if(inter(L,Line(p[],p[])))
{
printf("T\n");
continue;
}
else if(inter(L,Line(p[],p[])))
{
printf("T\n");
continue;
}
else if(inter(L,Line(p[],p[])))
{
printf("T\n");
continue;
}
else if(inConvexpoly(L.s,p,)>= || inConvexpoly(L.e,p,)>=)
{
printf("T\n");
continue;
}
else
printf("F\n");
}
return ;
}

最新文章

  1. HTML5 学习总结(二)——HTML5新增属性与表单元素
  2. jQuery Validate验证框架详解
  3. 在eclipse上开发nodejs
  4. 交换技术(swaping) 视频11
  5. 由ArrayList构造函数源码引出的问题
  6. php-抽象
  7. No.006 ZigZag Conversion
  8. Java基础知识强化之IO流笔记80:NIO之 ServerSocketChannel
  9. Java之循环语句练习1
  10. web交互方式
  11. 【转】FTS抓包看蓝牙的SDP整个过程
  12. windows服务启动 1053错误
  13. [zz]android的logcat详细用法
  14. WARNING OGG-01223 TCP/IP error 111 (Connection refused)
  15. 【27前端】base标签带有href属性会让chrome里的svg元素url失效
  16. hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)
  17. git使用之如何将github库下载到本地与如何将代码上传github
  18. ES3:ElasticSearch 索引
  19. [bzoj1369] [Baltic2003]Gem
  20. WPF笔记1 用VS2015创建WPF程序

热门文章

  1. BZOJ3172 [Tjoi2013]单词 【AC自动机】
  2. [codeforces/edu30]总结(E)
  3. bzoj 1189 [HNOI2007]紧急疏散evacuate 二分+网络流
  4. bzoj 4724 [POI2017]Podzielno 二分+模拟
  5. 基于Bootstrap的遮罩层,带有加载提示
  6. 【Linux】NAT模式下关于主机ping不通虚拟机的问题
  7. 多条select语句的合并
  8. (转) jsp学习笔记
  9. 51Nod 1001数组中和等于K的数对
  10. easyui datagrid 的数据加载Json数据