1410 Intersection
来源:互联网 发布:以下属于云计算的特征 编辑:程序博客网 时间:2024/05/16 19:18
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
Southwestern European Regional Contest 1995
除了线段完全在矩形内和线段所在直线斜率不存在两种特殊情况外,相交的情况只有以下四种及其组合:
线段与x=xl、x=xr、y=yb、y=yt分别相交,讨论即可。
/*The rectangle consists of four straight lines and the area in between.注意矩形是实心的*/#include<iostream>using namespace std;int xs, ys, xe, ye, xl, yt, xr, yb;void swap(int &a, int &b){ if (a > b) { int t = a; a = b; b = t; }}bool f1(int x){ return x >= xl&&x <= xr;}bool f2(int y){ return y >= yb&&y <= yt;}bool judge(int x){ return (x >= xs&&x <= xe) || (x >= xe&&x <= xs);}bool judge1(int y){ return (y >= ys&&y <= ye) || (y >= ye&&y <= ys);}bool judge_x(double x){ return x >= (double)xl&&x <= (double)xr;}bool judge_y(double y){ return y >= (double)yb&&y <= (double)yt;}int main(){ int n; cin >> n; while(n--) { cin >> xs >> ys >> xe >> ye >> xl >> yt >> xr >> yb; swap(xl, xr); //The terms top left and bottom right do not imply any ordering of coordinates.注意矩形端点大小,并非按照左上、右下的顺序给出 swap(yb, yt); int b1 = ys, b2 = ye; swap(b1, b2); if (f1(xs) && f1(xe) && f2(ys) && f2(ye)) //线段在矩形内 { cout << 'T' << endl; continue; } if (xs == xe) //斜率不存在 { if (f1(xs)) if (f2(ys) || f2(ye) || (b1 <= yb&&b2 >= yt)) { cout << 'T' << endl; continue; } } double slope = 1.0*(ys - ye) / (xs - xe); double b = ys - slope*xs; if (judge(xl)) { double y = slope*xl + b; if (judge_y(y)) { cout << 'T' << endl; continue; } } if (judge(xr)) { double y = slope*xr + b; if (judge_y(y)) { cout << 'T' << endl; continue; } } if (judge1(yb)) { double x = (yb - b) / slope; if (judge_x(x)) { cout << 'T' << endl; continue; } } if (judge1(yt)) { double x = (yt - b) / slope; if (judge_x(x)) { cout << 'T' << endl; continue; } } cout << 'F' << endl; } return 0;}
- 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- Poj-1410-Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection 笔记
- 线段树模板(最简便)
- jenkins简介
- 彻底弄懂dalvik字节码【一】
- Python学习(3)
- python做科学计算scipy
- 1410 Intersection
- binwalk在ubuntu中的安装与使用问题
- css3 transition
- MySQL5.6 PERFORMANCE_SCHEMA 说明
- 3300点悬于一线 “金融股"能否带动行情上攻?
- 算法基础之----直接选择排序
- NYOJ 6 喷水装置(一)
- 多线程启动线程数计算公式
- wordpress的搭建