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

原创 2015年07月07日 09:58:38
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;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1410 Intersection (判断线段是否与矩形相交)

题目大意:给定一条线段的起始和终止点的坐标,和一个矩形的左上和右下的坐标,判断线段是否与矩形相交。注意:矩形是实心       的,而且矩形的两点坐标不一定是按照左上、右下的顺序。 解题思路:首...

poj-1410 判断矩形和线段是否相交

Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9464   A...

POJ 1410 Intersection(判断线段与矩形是否相交)

POJ 1410 Intersection(判断线段与矩形是否相交) http://poj.org/problem?id=1410 题意:        给你一个矩形的对角坐标(x1,y1)与(...

poj 1410 矩形与线段相交判断

#include #include #include #include using namespace std; struct point { int x,y; }; int ...

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

Language:Default Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi...

POJ 1410 Intersection(线段与矩形相交)

判断矩形和线段是否相交,没什么说的,正确理解题意中相交的定义具体代码:#include #include #include using namespace std; typedef struct { ...

poj 1410 线段与矩形相交

题意:判断是否线段有和给定的矩形右交点 解法:几何模板题 坑点: 不一定就是左上点和右下点 线段如果在矩形内也算是相交 /**************...

poj 1410 Intersection(判矩形和线段相交。。细节多)

//以下为原blog搬迁过来的内容 【题目大意】:给出一条线段和一个矩形,判断线段和矩形是否相交。   【解题思路】:判给出的线段与矩形的每条线段是否相交(包括相交和非规范相交)...

poj1410 Intersection 线段与矩形相交

//poj1410 //题目意思:判断线段是否与矩形相交(注:线段在矩形内也是相交) //解题思路:图形学中的线段裁剪算法,有Cohen-Sutherland,梁友栋-Barsky等, //这里...

poj 1410 Intersection (判矩形和线段相交。。细节多)

【题目大意】:给出一条线段和一个矩形,判断线段和矩形是否相交。   【解题思路】:判给出的线段与矩形的每条线段是否相交(包括相交和非规范相交)。 1、内含也算相交(WA一次)       2、看...
  • wlyqh
  • wlyqh
  • 2011-10-29 23:16
  • 348
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)