poj 1410 线段相交判断

http://poj.org/problem?id=1410

Intersection

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11329   Accepted: 2978

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

Southwestern European Regional Contest 1995

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’

---------------------------------------------------------------------------------------------------------------

题目有点坑,没有说明输入的不一定是左上,以及右下,可能会是左下,以及右上

并且,包含在矩形里也算相交,这题看题解不是好想法啊,自己想出来比较好

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;
typedef struct point
{
    int x,y;
}point;

typedef struct line
{
    point st,ed;
}line;

int crossProduct(point a,point b,point c)
{
    return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}

bool onSegment(point a,point b,point c)
{
    int maxx=max(a.x,b.x);
    int maxy=max(a.y,b.y);
    int minx=min(a.x,b.x);
    int miny=min(a.y,b.y);
    if(crossProduct(a,b,c)==0&&(c.x<=maxx)&&(c.x>=minx)&&(c.y<=maxy)&&(c.y>=miny))
    {
        return true;
    }
    return false;
}

bool segIntersect(point p1,point p2,point p3,point p4)
{
    int d1=crossProduct(p3,p4,p1);
    int d2=crossProduct(p3,p4,p2);
    int d3=crossProduct(p1,p2,p3);
    int d4=crossProduct(p1,p2,p4);
    if(d1*d2<0 && d3*d4<0)
        return true;
    if(d1==0 && onSegment(p3,p4,p1))
        return true;
    if(d2==0 && onSegment(p3,p4,p2))
        return true;
    if(d3==0 && onSegment(p1,p2,p3))
        return true;
    if(d4==0 && onSegment(p1,p2,p4))
        return true;
    return false;
}

point p[5];
line li[5];
int num[1005];

int main()
{
    int n,m,i,j;
    line tmp;
    int xleft,ytop,xright,ybottom;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d%d%d%d%d%d",&tmp.st.x,&tmp.st.y,&tmp.ed.x,&tmp.ed.y
              ,&xleft,&ytop,&xright,&ybottom);
        li[0].st.x=xleft;
        li[0].st.y=ytop;
        li[0].ed.x=xleft;
        li[0].ed.y=ybottom;

        li[1].st.x=xleft;
        li[1].st.y=ybottom;
        li[1].ed.x=xright;
        li[1].ed.y=ybottom;

        li[2].st.x=xright;
        li[2].st.y=ybottom;
        li[2].ed.x=xright;
        li[2].ed.y=ytop;

        li[3].st.x=xright;
        li[3].st.y=ytop;
        li[3].ed.x=xleft;
        li[3].ed.y=ytop;

        bool flag=true;
        for(i=0;i<4;i++)
        {
            if(segIntersect(tmp.st,tmp.ed,li[i].st,li[i].ed))
            {
                flag=false;
                break;
            }
        }

        int maxx=max(xleft,xright);
        int maxy=max(ytop,ybottom);
        int minx=min(xleft,xright);
        int miny=min(ytop,ybottom);
        if(tmp.st.x<=maxx&&tmp.st.x>=minx&&tmp.st.y>=miny&&tmp.st.y<=maxy
           ||tmp.ed.x<=maxx&&tmp.ed.x>=minx&&tmp.ed.y>=miny&&tmp.ed.y<=maxy)
            flag=false;

        if(flag)
            printf("F\n");
        else printf("T\n");
    }
    return 0;
}

poj 1410 线段相交判断,布布扣,bubuko.com

时间: 2024-07-31 14:31:22

poj 1410 线段相交判断的相关文章

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

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

poj 2653 (线段相交判断)

http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9531   Accepted: 3517 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishi

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

ACM1558两线段相交判断和并查集

Segment set Problem Description A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set. Input In the first line ther

poj 1066 线段相交

链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5431   Accepted: 2246 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid

poj 1269 线段相交/平行

模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = 1 #include <cmath> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 7 #define eps 1e-8 8 #define PI acos(-1.0)//3.14159265358979323846 9 //判断一个数是否为0,是则返回true,否

poj 2653 线段相交

题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说了没被压住的线段不超过1000个,所以不会爆 1 #include<math.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 bool ans[100100]; 6 int n; 7 double X1,X2,Y1,Y2;

POJ 1066 Treasure Hunt 线段相交判断

判断以宝藏的坐标和中点的坐标为线段的点是否与墙相交,求最少相交的墙的数量 中点算出来,枚举中点和墙 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define eps 1e-8 #define INF 1e9 using namespace std; const int maxn=100; typede

poj 2653 Pick-up sticks(几何线段相交判断)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10949   Accepted: 4085 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin