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. JQuery ajax方法及参数

    ©屋主原创,版权归 todayeeee 所有!如需转载,必须在页面明显位置给出原文链接!商业用途请 联系我!   $.ajax({ type: 'GET',    // 这是请求的方式 可以是GET方 ...

  2. List对象排序的通用方法

    转自 @author chenchuang import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Me ...

  3. eclispse快捷键

    Eclipse常用快捷键   1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示 ...

  4. PHP多表取数据的代码优化

    <?php header("Content-type: text/html; charset=utf-8"); //假设这里的$goods_arr  和 $shop_arr  ...

  5. 有关对字符串的处理,需要用到List时的简化写法

    这是项目中的需要根据ComputerName来获取IP的一个方法,如果出现多个ComputerName,需要将多个ComputerName的字符串以“:”分开,传进方法中,然后再处理不同的Name,然 ...

  6. protoful进行序列化

    Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台 ...

  7. android技术总结

    1.要做一个尽可能流畅的ListView,你平时在工作中如何进行优化的? ①Item布局,层级越少越好,使用hierarchyview工具查看优化. ②复用convertView ③使用ViewHol ...

  8. jqeury轮播

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  9. c# 服务端

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. LA 4998 Simple Encryption

    题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...