概述
Intersection
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13106 | Accepted: 3412 |
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.
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.
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 大意:给出一条线段的起点和终点,然后给出一个矩形的斜对角线的起点和终点,判断这条线段和这个矩形是否相交 思路:将直线方程求出,为Ax+By+C=0,然后判断,如果与矩形相交,那么会有两个点分别在线段两侧,那么代入值 会是一个正一个负。。但这不是唯一的判断,这条线段会存在于这个矩形中,也就是说这个矩形会包含这条线段,此时 两个值也是一个正一个负,那么就要判断这条线段的两个端点和矩形四个顶点的关系就行了。 ac代码:#include<stdio.h> #include<math.h> #include<string.h> #include<iostream> #include<algorithm> #define MAXN 100010 #define LL long long using namespace std; struct s { int x; int y; }begin,end,sqb,sqe; int main() { int t,a,b,c; int num1,num2; scanf("%d",&t); while(t--) { scanf("%d%d%d%d%d%d%d%d",&begin.x,&begin.y,&end.x,&end.y,&sqb.x,&sqb.y,&sqe.x,&sqe.y); a=begin.y-end.y; b=end.x-begin.x; c=end.y*begin.x-end.x*begin.y; if(sqb.x>sqe.x)//为了严谨,将顶点顺序保持前者为上 { int x=sqb.x; sqb.x=sqe.x; sqe.x=x; } if(sqb.y<sqe.y) { int y=sqb.y; sqb.y=sqe.y; sqe.y=y; } num1=(a*sqb.x+b*sqb.y+c)*(a*sqe.x+b*sqe.y+c); num2=(a*sqb.x+b*sqe.y+c)*(a*sqe.x+b*sqb.y+c); if(num1>0&&num2>0) { printf("Fn"); continue; } if((begin.x<sqb.x&&end.x<sqb.x)||(begin.x>sqe.x&&end.x>sqe.x)||(begin.y>sqb.y&&end.y>sqb.y)||(begin.y<sqe.y&&end.y<sqe.y))//检查是否包含 printf("Fn"); else printf("Tn"); } return 0; }
最后
以上就是玩命身影为你收集整理的POJ 1410 Intersection(判断线段与矩形是否相交)的全部内容,希望文章能够帮你解决POJ 1410 Intersection(判断线段与矩形是否相交)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复