# poj1410判断线段与矩形是否相交(判断线段相交)

Intersection

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

(线段在矩形里面属于相交)

（1）判断线段与线段之间是否相交

1、两线段不在同一直线上

2、两线段在同一直线上（相交和不相交两种情况）

#include<stdio.h>
#include<algorithm>
using namespace std;
struct point{
double x,y;
}L1,L2;
struct line{
point A,B;
}L[5];
double cal(point A,point B,point C)
{
return (C.x-A.x)*(B.y-A.y)-(B.x-A.x)*(C.y-A.y);
}
int cross(point C,point D)
{
double d1,d2,d3,d4;
d1=cal(L1,L2,C);
d2=cal(L1,L2,D);
d3=cal(C,D,L1);
d4=cal(C,D,L2);
if(d1==0 && d2==0 && d3==0 && d4==0)    //两线段在同一直线上
{
if(max(L1.x,L2.x)<min(C.x,D.x)||max(C.x,D.x)<min(L1.x,L2.x))
return 0;
else if(max(L1.y,L2.y)<min(C.y,D.y)||max(C.y,D.y)<min(L1.y,L2.y))
return 0;
else return 1;
}
if(d1*d2<0 && d3*d4<0)
{
return 1;
}
return 0;
}
int fun1(double x1,double y1,double x2,double y2)
{
double xmin=x1<x2?x1:x2;
double xmax=x1>x2?x1:x2;
double ymin=y1<y2?y1:y2;
double ymax=y1>y2?y1:y2;
double Lxmin=L1.x<L2.x?L1.x:L2.x;
double Lxmax=L1.x>L2.x?L1.x:L2.x;
double Lymin=L1.y<L2.y?L1.y:L2.y;
double Lymax=L1.y>L2.y?L1.y:L2.y;
if(Lxmin>=xmin&&Lxmax<=xmax&&Lymin>=ymin&&Lymax<=ymax)
return 1;
else return 0;
}
void fun(double x1,double y1,double x2,double y2)
{
for(int i=0; i < 4; i++)
{
if(cross(L[i].A,L[i].B)==1)
{
printf("T\n");
return ;
}
}
if(fun1(x1,y1,x2,y2)==1)
{
printf("T\n");
return ;
}
printf("F\n");
}

int main()
{
int n;
double x1,y1,x2,y2;
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf%lf%lf",&L1.x,&L1.y,&L2.x,&L2.y);
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
L[0].A.x=x1; L[0].A.y=y1;
L[0].B.x=x2; L[0].B.y=y1;
L[1].A.x=x2; L[1].A.y=y1;
L[1].B.x=x2; L[1].B.y=y2;
L[2].A.x=x1; L[2].A.y=y2;
L[2].B.x=x2; L[2].B.y=y2;
L[3].A.x=x1; L[3].A.y=y1;
L[3].B.x=x1; L[3].B.y=y2;
fun(x1,y1,x2,y2);
}
return 0;
}

• 本文已收录于以下专栏：

## POJ1410Intersection【判断线段与矩形相交+点在矩形内的简单判定】

Language:Default Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi...
• R1986799047
• 2015年09月04日 09:58
• 776

## C#判断线段是否相交

• xxdddail
• 2017年04月20日 17:14
• 1087

## C语言-判断线段是否与矩形范围有交集

• zhaopenghhhhhh
• 2014年12月12日 14:29
• 1569

## 判断线段相交 opencv-ext

• zhanghefu
• 2010年12月14日 22:45
• 1731

## 计算几何基础——矢量和叉积 && 叉积、线段相交判断、凸包（转载）

• itismelzp
• 2015年10月27日 17:11
• 1443

## 判断线段与圆是否相交

ACM模版判断线段与圆是否相交typedef long long ll; typedef struct // 点结构 { ll x, y; } Point;Point A, B, C, O...
• f_zyj
• 2016年07月29日 17:11
• 2346

## 计算几何----判断线段相交（一）

• liangzhaoyang1
• 2016年04月07日 22:54
• 1144

## POJ - 1127 Jack Straws 判断线段相交 叉积

• HowardEmily
• 2017年07月19日 21:28
• 673

## 判断任意多边形与矩形的相交(线段与矩形相交或线段与线段相交)

• kfarvid
• 2011年09月08日 11:45
• 2738

## 判断两个矩形是否相交（C++)

• woaitingting1985
• 2016年12月05日 16:02
• 1032

举报原因： 您举报文章：poj1410判断线段与矩形是否相交(判断线段相交) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)