POJ1410Intersection【判断线段与矩形相交+点在矩形内的简单判定】
2015-09-04 09:58
351 查看
Language:Default Intersection
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 14 9 11 2 1 5 7 1 Sample Output F |
想到了直线可能完全在矩形内部但却没有完全理解题意The terms top left and bottom right do not imply any ordering of coordinates;注意它们的位置关系不确定
#include<cstdio> #include<cstdlib> #include<cstring> #define eps 1e-8 using namespace std; struct point{ double x,y; }; struct line{ point a,b; }A[100010]; double MAX(double a,double b){ return a>b?a:b; } double MIN(double a,double b){ return a<b?a:b; } bool judge(int a,int b){ if(MIN(A[a].a.x,A[a].b.x)>MAX(A[b].a.x,A[b].b.x)||MIN(A[a].a.y,A[a].b.y)>MAX(A[b].a.y,A[b].b.y)||MIN(A[b].a.x,A[b].b.x)>MAX(A[a].a.x,A[a].b.x)||MIN(A[b].a.y,A[b].b.y)>MAX(A[a].a.y,A[a].b.y)) return false; double h,i,j,k; h=(A[a].b.x-A[a].a.x)*(A[b].a.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].a.x-A[a].a.x); i=(A[a].b.x-A[a].a.x)*(A[b].b.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].b.x-A[a].a.x); j=(A[b].b.x-A[b].a.x)*(A[a].a.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].a.x-A[b].a.x); k=(A[b].b.x-A[b].a.x)*(A[a].b.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].b.x-A[b].a.x); return h*i<=eps&&j*k<=eps; } int main() { int t,i,j,k; double xleft,ytop,xright,ybottom; double x1,y1,x2,y2; scanf("%d",&t); while(t--){ scanf("%lf%lf%lf%lf",&A[0].a.x,&A[0].a.y,&A[0].b.x,&A[0].b.y); scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); xleft=MIN(x1,x2);xright=MAX(x1,x2); ybottom=MIN(y1,y2);ytop=MAX(y1,y2); A[1].a.x=xleft;A[1].a.y=ybottom;A[1].b.x=xleft;A[1].b.y=ytop; A[2].a.x=xleft;A[2].a.y=ytop;A[2].b.x=xright;A[2].b.y=ytop; A[3].a.x=xright;A[3].a.y=ytop;A[3].b.x=xright;A[3].b.y=ybottom; A[4].a.x=xright;A[4].a.y=ybottom;A[4].b.x=xleft;A[4].b.y=ybottom; for(i=1;i<=4;++i){ if(judge(0,i))break; } bool flag=false; if(A[0].a.x<=xright&&A[0].a.x>=xleft&&A[0].a.y>=ybottom&&A[0].a.y<=ytop)flag=true; if(A[0].b.x<=xright&&A[0].b.x>=xleft&&A[0].b.y>=ybottom&&A[0].b.y<=ytop)flag=true; if(i>4&&flag==0) printf("F\n"); else printf("T\n"); } return 0; }
相关文章推荐
- CSDN-markdown编辑器的样例保存
- 原来UIWindow可以自定义frame
- [人月神话]读书笔记8--软件产品需要的文档&&软件开发没有银弹(捷径)
- 嵌入式内核初学习
- java中extends和implements的区别
- C++中成员变量的初始化方法
- 分布式缓存的一起问题 – 后端技术 by Tim Yang
- 2.一个随机生成多种颜色并获取颜色RGB值以及对应十六进制的安卓小应用
- 程序猿需要知道的相关知识
- IOS 百度地图开发中大头针(标注)以及折线的基本使用
- 最大公约和最小公倍数
- stl string
- webService(php)的soap与nusoap服务端与客户端的通信
- python 错误集
- 被动DNS
- 新兵训练营系列课程——Feed架构介绍 - 长微博
- CentOS7图形界面切换及版本查看
- 半边数据结构及其使用
- uva455 - Periodic Strings
- C++类中的常量