poj 1410 Intersection (判矩形和线段相交。。细节多)
来源:互联网 发布:江西省委党校 数据库 编辑:程序博客网 时间:2024/05/17 08:38
【题目大意】:给出一条线段和一个矩形,判断线段和矩形是否相交。
【解题思路】:判给出的线段与矩形的每条线段是否相交(包括相交和非规范相交)。
1、内含也算相交(WA一次) 2、看了discuss,发现题目给出矩形的点并不一定是左上角的点和右下角的点(WA2次)
【代码】:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define eps 1e-8struct Point{ double x, y; Point() {} Point(double a, double b) { x = a, y = b; }}point[20];struct Line{ Point a, b; Line() {} Line(Point x, Point y) { a = x, b = y; }}line,line1[20];inline int sig(double k){ return k < -eps ? -1 : k > eps;}inline double det(double x1, double y1, double x2, double y2){ return x1 * y2 - x2 * y1;}inline double xmult(Point o, Point a, Point b){ return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);}inline double dotdet(double x1, double y1, double x2, double y2){ return x1 * x2 + y1 * y2;}inline double dot(Point o, Point a, Point b){ return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);}inline int between(Point o, Point a, Point b){ return sig(dot(o, a, b));}inline int intersect1(Point a, Point b, Point c, Point d) { double s1, s2, s3, s4; int d1 = sig(s1 = xmult(a, b, c)); int d2 = sig(s2 = xmult(a, b, d)); int d3 = sig(s3 = xmult(c, d, a)); int d4 = sig(s4 = xmult(c, d, b)); if ((d1^d2) == -2 && (d3^d4) == -2) { return 1; } if (d1 == 0 && between(c, a, b) <= 0) return 2; if (d2 == 0 && between(d, a, b) <= 0) return 2; if (d3 == 0 && between(a, c, d) <= 0) return 2; if (d4 == 0 && between(b, c, d) <= 0) return 2; return 0;}inline int intersect(Line u, Line v){ return intersect1(u.a, u.b, v.a, v.b);}int main(){ int T; double p,q,m,n,p1,q1,m1,n1; scanf("%d",&T); while (T--) { scanf("%lf%lf",&p1,&q1); scanf("%lf%lf",&m1,&n1); point[1]=Point(m1,n1); point[0]=Point(p1,q1); line=Line(point[0],point[1]); scanf("%lf%lf%lf%lf",&p,&q,&m,&n); if (p>m && q<n) { swap(p,m); swap(q,n); } else if (p>m && q>n) swap(m,p); else if (p<m && q<n) swap(q,n); if ((p1>=p && p1<=m && q1<=q && q1>=n && m1>=p && m1<=m && n1<=q && n1>=n)) { printf("T\n"); continue; } point[2]=Point(p,q); point[3]=Point(m,q); point[4]=Point(p,n); point[5]=Point(m,n); line1[0]=Line(point[2],point[3]); line1[1]=Line(point[2],point[4]); line1[2]=Line(point[3],point[5]); line1[3]=Line(point[4],point[5]); int k1,k2,k3,k4; k1=intersect(line,line1[0]); k2=intersect(line,line1[1]); k3=intersect(line,line1[2]); k4=intersect(line,line1[3]); //cout << k1 << " " << k2 << " " << k3 << " " << k4 << endl; if (k1==0 && k2==0 && k3==0 && k4==0) { printf("F\n"); } else printf("T\n"); } return 0;}
【运行结果】:
- poj 1410 Intersection (判矩形和线段相交。。细节多)
- poj 1410 Intersection(判矩形和线段相交。。细节多)
- POJ 1419 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 (线段相交判定)
- Intersection(poj1401线段和矩形相交)
- POJ 1410 Intersection(线段相交&&判断点在矩形内&&坑爹)
- poj 1410 Intersection(判断线段是否与实心矩形相交)
- poj 1410 Intersection (判两直线是否相交)
- 二维带权邮局位置(选址)问题(分别求横坐标、纵坐标的带权中位数)C++实现
- 【虚假同感偏差】
- centos 5.5 下安装 jdk1.6
- 2011NOKIA笔试题目
- Android数据存储之ContentProvider&Preferences
- poj 1410 Intersection (判矩形和线段相交。。细节多)
- 董事长、总裁、总经理的关系
- 书柜灯实验
- 第五章:TCP客户-服务器程序例子
- JAXP,DOM,SAX,JDOM,DOM4J的关系
- 性能提升——好文收录
- Android中Handler作用
- js数据验证集合、js email验证、js url验证、js长度验证、js数字验证等
- UDP连接和TCP连接的异同