【第21期】观点:人工智能到底用 GPU?还是用 FPGA?

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(判断线段与矩形是否相交) http://poj.org/problem?id=1410 题意:        给你一个矩形的对角坐标(x1,y1)与(...

PKU Online Judge POJ流传最广的分类,希望以后会切之(转)

转自 [url]http://bbs.pediy.com/showthread.php?t=123253[/url] 多版本的POJ分类 ­ 流传最广的一种分类: ­ 初期: ­ 一.基本算法: ­ (1)枚举. (poj1753,poj2965) ­ (2)贪心(poj1328,poj2109,poj2586) ­ (3)递归和分治法. ­ (4)递推. ­ (5)构造法.(poj3295) ­ (6)模拟法.(poj1068,poj2632,poj1573,poj29

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

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

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

POJ 1410 Intersection(判断线段与矩形是否相交) http://poj.org/problem?id=1410 <span sty

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

Intersection Description You are to write a program that has to decide whether a given line segment...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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