【hdu】 Intersection 线段相交 cross+quick
来源:互联网 发布:手感好的鼠标 知乎 编辑:程序博客网 时间:2024/04/30 08:18
Intersection
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 6
Problem 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
14 9 11 2 1 5 7 1
Sample Output
F
Source
PKU
这么漂亮的代码搞了半天终于ac
1.point点一定不用int,需用double类型
2.线段判交:快速排斥(以P1P2为对角线的矩形S1是否和以P3P4为对角线的矩形S2相交),跨立实验(点P1,P2必然在线段P3P4的不同侧,点P3,P4必然在线段P1P2的不同侧,两者关系&&)
#include<cstdio>using namespace std;#define eps 1e-8double x1,y1,x2,y2,x3,y3,x4,y4;//x1,x2 denotes the line,x3,x4 denotes the rec.y etc...struct Point{double x,y; Point(double a=0.0,double b=0.0){x=a,y=b;}};struct Line{Point p,q;Line(Point u,Point v):p(u),q(v){}Line(){}}s[5];inline double max(double a,double b) {return a>b?a:b;}inline double min(double a,double b) {return a<b?a:b;}void swap(double &a,double &b){double t=a;a=b;b=t;}//(p1-p0)*(p2-p0)double cross(Point p1,Point p2,Point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}bool quick(Line a,Line b){if(min(a.p.x,a.q.x)<=max(b.p.x,b.q.x)&&min(b.p.x,b.q.x)<=max(a.p.x,a.q.x)&&min(a.p.y,a.q.y)<=max(b.p.y,b.q.y)&&min(b.p.y,b.q.y)<=max(a.p.y,a.q.y)&&cross(a.p,a.q,b.p)*cross(a.p,a.q,b.q)<=0&&cross(b.p,b.q,a.p)*cross(b.p,b.q,a.q)<=0) //由于算线段的端点和另一线段相交的情况。。。return true;return false;}bool check(Line k){if(k.p.x>=x3&&k.p.x<=x4&&k.p.y>=y4&&k.p.y<=y3||k.q.x>=x3&&k.q.x<=x4&&k.q.y>=y4&&k.q.y<=y3) return true;return false;}int main(){int T;scanf("%d",&T);while(T--){scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);if(x3>x4) swap(x3,x4);if(y3<y4) swap(y3,y4);s[0]=Line(Point(x1,y1),Point(x2,y2));s[1]=Line(Point(x3,y3),Point(x4,y3));s[2]=Line(Point(x4,y3),Point(x4,y4));s[3]=Line(Point(x3,y4),Point(x4,y4));s[4]=Line(Point(x3,y3),Point(x3,y4));if(quick(s[0],s[1])||quick(s[0],s[2])||quick(s[0],s[3])||quick(s[0],s[4])||check(s[0])) printf("T\n");else printf("F\n");}return 0;}附上poj测试数据:
684 9 11 2 1 1 7 511 2 4 9 1 1 7 512 12 24 24 19 5 25 174 6 15 9 1 1 11 1119 5 25 17 12 12 24 240 18 8 12 1 1 11 112 4 4 2 1 1 11 11-4 9 -11 2 -1 1 -7 5-11 2 -4 9 -1 1 -7 5-12 12 -24 24 -19 5 -25 17-4 6 -15 9 -1 1 -11 11-19 5 -25 17 -12 12 -24 240 18 -8 12 -1 1 -11 11-2 4 -4 2 -1 1 -11 114 -9 11 -2 1 -1 7 -511 -2 4 -9 1 -1 7 -512 -12 24 -24 19 -5 25 -174 -6 15 -9 1 -1 11 -1119 -5 25 -17 12 -12 24 -240 -18 8 -12 1 -1 11 -112 -4 4 -2 1 -1 11 -11-4 -9 -11 -2 -1 -1 -7 -5-11 -2 -4 -9 -1 -1 -7 -5-12 -12 -24 -24 -19 -5 -25 -17-4 -6 -15 -9 -1 -1 -11 -11-19 -5 -25 -17 -12 -12 -24 -240 -18 -8 -12 -1 -1 -11 -11-2 -4 -4 -2 -1 -1 -11 -119 1 9 2 4 3 9 69 2 9 1 4 3 9 610 3 13 3 4 3 9 613 3 10 3 4 3 9 610 6 14 6 4 3 9 614 6 10 6 4 3 9 69 7 9 10 4 3 9 69 10 9 7 4 3 9 64 7 4 10 4 3 9 64 10 4 7 4 3 9 60 6 3 6 4 3 9 63 6 0 6 4 3 9 61 3 3 3 4 3 9 63 3 1 3 4 3 9 64 0 4 2 4 3 9 64 2 4 0 4 3 9 65 3 8 5 4 3 9 68 5 5 3 4 3 9 65 3 8 3 4 3 9 68 3 5 3 4 3 9 66 4 6 5 4 3 9 66 5 6 4 4 3 9 64 3 9 6 4 3 9 69 6 4 3 4 3 9 64 3 5 4 4 3 9 65 4 4 3 4 3 9 65 3 8 3 4 3 9 68 3 5 3 4 3 9 65 3 9 3 4 3 9 69 3 5 3 4 3 9 64 4 4 5 4 3 9 64 5 4 4 4 3 9 64 3 4 5 4 3 9 64 5 4 3 4 3 9 64 3 4 6 4 3 9 64 6 4 3 4 3 9 69 2 9 5 4 3 9 69 5 9 2 4 3 9 69 2 9 7 4 3 9 69 7 9 2 4 3 9 6
- 【hdu】 Intersection 线段相交 cross+quick
- 线段相交:Intersection
- poj 1410 Intersection 线段相交
- POJ 1410 Intersection(线段相交)
- pku 1410 Intersection(判断线段是否相交)
- poj1410 Intersection 线段与矩形相交
- Intersection(poj1401线段和矩形相交)
- POJ 1410 Intersection(线段非规范相交)
- POJ 1410 Intersection --几何,线段相交
- POJ 1410 || Intersection(线段矩形相交
- poj 1410 Intersection(线段相交判断)
- uva191 Intersection(线段之间相交)
- poj 1410 Intersection (线段相交判定)
- hdu 1086 线段相交
- hdu 1086 线段相交
- hdu 1147(线段相交)
- HDU-5120-Intersection【几何-相交圆面积】
- hdu 5120 Intersection(求相交环面积)
- QT 远程调试
- JavaScript动态创建Html内容
- 发送短信
- 孙鑫《VC++深入详解》完整版PDF 下载
- 遍历Hashtable中的key和value的几种方法
- 【hdu】 Intersection 线段相交 cross+quick
- c语言集合
- jsp中折叠式标签的代码实现
- LoadRunner中的一个关联技巧
- 如何把maven项目转成web项目
- 递归实现倒序打印
- android+捕获home键方法
- Linux c编程 exec execl
- POJ 1985 Cow Marathon