poj1410 Intersection
2014-09-22 19:06
218 查看
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
Sample Output
题意:
有一个实心矩形,问某条线段与该矩形是否有交点。
注意:题目对于矩形点的输入并没有顺序,需要自己排序
思路:
•思路一:考虑相交的情况
•1.线段至少与一条边相交
•2.线段不与任意一条边相交,但在矩形的内部
核心:判断两条线段相交的方法
显然,只要p1,p2两点在p3,p4的两边,并且p3,p4两点在p1,p2两边就能满足条件
方法一 向量法:
方法二 公式法:
•由直线的两点式方程展开
•(x-x1)(y2-y1)=(y-y1)(x2-x1)
•令f(x,y)=(x-x1)(y2-y1)-(y-y1)(x2-x1)
•(x1,y1),(x2,y2)
•若f(x1,y1)*f(x2,y2)<0,则在两侧
•否则在一侧
需要判断在内部的情况:
显然,在内部时满足一种,最大值小于最小值,最小值大于最大值的关系
•思路二:考虑不相交的情况
•1.线段所在的直线与实心矩形不相交
•2.线段所在直线与矩形相交,但线段与其不相交
•依然是f(x,y)=(x-x1)(y2-y1)-(y-y1)(x2-x1)
•将矩形的四个点分别代入,若线段所在的直线与矩形不相交,则四个点必定在直线的同一侧,即同号
•若直线与矩形相交但线段不相交,则考虑极端情况
也是一种最大值小于最小值,最小值大于最大值的极端情况,排除掉之后就都是相交的情况了
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
题意:
有一个实心矩形,问某条线段与该矩形是否有交点。
注意:题目对于矩形点的输入并没有顺序,需要自己排序
思路:
•思路一:考虑相交的情况
•1.线段至少与一条边相交
•2.线段不与任意一条边相交,但在矩形的内部
核心:判断两条线段相交的方法
显然,只要p1,p2两点在p3,p4的两边,并且p3,p4两点在p1,p2两边就能满足条件
方法一 向量法:
方法二 公式法:
•由直线的两点式方程展开
•(x-x1)(y2-y1)=(y-y1)(x2-x1)
•令f(x,y)=(x-x1)(y2-y1)-(y-y1)(x2-x1)
•(x1,y1),(x2,y2)
•若f(x1,y1)*f(x2,y2)<0,则在两侧
•否则在一侧
需要判断在内部的情况:
显然,在内部时满足一种,最大值小于最小值,最小值大于最大值的关系
•思路二:考虑不相交的情况
•1.线段所在的直线与实心矩形不相交
•2.线段所在直线与矩形相交,但线段与其不相交
•依然是f(x,y)=(x-x1)(y2-y1)-(y-y1)(x2-x1)
•将矩形的四个点分别代入,若线段所在的直线与矩形不相交,则四个点必定在直线的同一侧,即同号
•若直线与矩形相交但线段不相交,则考虑极端情况
也是一种最大值小于最小值,最小值大于最大值的极端情况,排除掉之后就都是相交的情况了
相关文章推荐
- poj1410 Intersection
- Intersection--poj1410(判断线段与矩形的关系)
- POJ1410 Intersection
- poj1410 intersection
- Intersection
- Finding intersection and union of two sets.
- POJ 1410 Intersection
- UVa 10790 - How Many Points of Intersection?
- STL源码剖析之算法:set_union & set_intersection
- poj 3335 Rotating Scoreboard (Half Plane Intersection)
- POJ 1410 Intersection 判断矩形和线段相交
- POJ1410_Intersection(几何/线段是否相交/模板)
- HDU 4873 ZCC Loves Intersection(概率)
- STL algorithm算法set_difference,set_intersection(53)
- [leetcode]Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- hdu-5120-Intersection
- hdu 5120 Intersection
- leetcode[160] Intersection of Two Linked Lists
- 【leetcode】Intersection of Two Linked Lists