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

 
‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’
---------------------------------------------------------------------------------------------------------------
题目有点坑,没有说明输入的不一定是左上,以及右下,可能会是左下,以及右上
并且,包含在矩形里也算相交,这题看题解不是好想法啊,自己想出来比较好
#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)==&&(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< && d3*d4<)
return true;
if(d1== && onSegment(p3,p4,p1))
return true;
if(d2== && onSegment(p3,p4,p2))
return true;
if(d3== && onSegment(p1,p2,p3))
return true;
if(d4== && onSegment(p1,p2,p4))
return true;
return false;
} point p[];
line li[];
int num[]; 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[].st.x=xleft;
li[].st.y=ytop;
li[].ed.x=xleft;
li[].ed.y=ybottom; li[].st.x=xleft;
li[].st.y=ybottom;
li[].ed.x=xright;
li[].ed.y=ybottom; li[].st.x=xright;
li[].st.y=ybottom;
li[].ed.x=xright;
li[].ed.y=ytop; li[].st.x=xright;
li[].st.y=ytop;
li[].ed.x=xleft;
li[].ed.y=ytop; bool flag=true;
for(i=;i<;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 ;
}

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

  1. POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1243   Accepted: 524 D ...

  2. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

  3. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  4. POJ 3304 Segments (直线和线段相交判断)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7739   Accepted: 2316 Descript ...

  5. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  6. poj 1269 线段相交/平行

    模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...

  7. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

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

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Mem ...

  9. poj 1066 线段相交

    链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

随机推荐

  1. android 学习随笔十七(服务 )

    1.Service 四大组件之一 运行于后台,没有前台界面的组件,用于运行需要在后台运行的代码 可以理解为没有前台的Activity 定义方式:创建java类继承Service,清单文件中注册该类 p ...

  2. resultMap / resultType

    ===================resultMap:实体类的属性和通过resultMap映射后的property属性一致 <resultMap id="workerSelect& ...

  3. stdout.read()与stdout.readlines()方法同时使用后果

    stdout.read()与stdout.readlines()方法同时使用将无法导致最后使用的stdout.readlines()读取的内容为空,原因是首先调用的stdout.read()已将数据读 ...

  4. Python 的命令行参数处理 optparse->argparse

    optaprse自2.7版开始弃用:弃用optparse模块,不会进一步开发,将继续开发argparse模块作为替代. 但是用习惯了optparse,还是很好用的撒. optparse使用起来,相比旧 ...

  5. Maven创建多个子项目

    一.下载jdk并安装:下载apache-maven包,解压到指定目录.(例:D:\Java\apache-maven-3.3.9) 二.配置环境. 1.配置jdk环境 系统变量 (1)JAVA_HOM ...

  6. NEON在Android中的使用举例【转】

    转自:http://blog.csdn.net/fengbingchun/article/details/37766607 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.  打开Eclip ...

  7. c++ 程序在内存中的分布

    从低地址到高地址: 1.代码区[包含常量的]:存放函数体的二进制代码 2.全局变量区[已初始化 + 未初始化]: 全局变量和静态变量的存储是放一块的,初始化的全局变量和静态变量在一块区域, 未初始化的 ...

  8. Java获取字符串编码方式

    直接下载吧: http://files.cnblogs.com/files/xiluhua/BytesEncodingDetectTool.rar

  9. linux中利用awk对数组进行排序

    数组 在排序前需要对数组有所了解,数组是用于存储一系列值得变量,这些值之间通常是由联系的,可通过索引来访问数组的值,索引需要用括号括起来,基本格式如下: array[index]=value awk数 ...

  10. Oracle绑定变量

    select * from table where id = ? 类似于上面这样的sql,如果不用绑定变量,每次执行时Oracle会认为是不同的sql,会在每次执行时生成一遍执行计划,而执行计划的生成 ...