POJ 1410 Intersection(线段相交)
来源:互联网 发布:ubuntu steam 游戏 编辑:程序博客网 时间:2024/04/30 08:53
判断线段是否与矩形有交集。。
刚开始看成直线错了好几发。。
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;typedef struct point { double x; double y;}point;typedef struct v { point start; point end;}v;const double eps = 1e-8;point rec[4], seg[2];double crossProduct(v* v1, v* v2){ v vt1, vt2; double result = 0; vt1.start.x = 0; vt1.start.y = 0; vt1.end.x = v1->end.x - v1->start.x; vt1.end.y = v1->end.y - v1->start.y; vt2.start.x = 0; vt2.start.y = 0; vt2.end.x = v2->end.x - v2->start.x; vt2.end.y = v2->end.y - v2->start.y; result = vt1.end.x * vt2.end.y - vt1.end.y * vt2.end.x; return result;}bool onLine(point pi, point pj, point Q){ v v1, v2; v1.start = v2.start = Q; v1.end = pi; v2.end = pj; if(crossProduct(&v1, &v2) == 0) return true; return false;}double multi(point p1, point p2, point p0) //used for Across{ return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);}int Across(v v1, v v2) //segmentCross,use functions: multi(){ if(max(v1.start.x,v1.end.x) >= min(v2.start.x, v2.end.x) && max(v2.start.x,v2.end.x) >= min(v1.start.x, v1.end.x) && max(v1.start.y,v1.end.y) >= min(v2.start.y, v2.end.y) && multi(v2.start, v1.end, v1.start) * multi(v1.end, v2.end, v1.start) > 0 && multi(v1.start, v2.end, v2.start) * multi(v2.end, v1.end, v2.start) > 0) return 1; else return 0;}bool onSegment(point Pi, point Pj, point Q) //Pi, Pj, Q are all different points{//same slopes or same x-coordinates or same y-coordinates if((Q.x - Pi.x) * (Pj.y - Pi.y) == (Pj.x - Pi.x) * (Q.y - Pi.y) && min(Pi.x, Pj.x) <= Q.x && Q.x <= max(Pi.x, Pj.x)&& min(Pi.y, Pj.y) <= Q.y && Q.y <= max(Pi.y, Pj.y)) //determine that Q is "between" Pi and Pj return true; else return false;}bool inRectangle(point p, point a, point b){ if(p.x >= a.x && p.x <= b.x && p.y >= b.y && p.y <= a.y) return true; return false;}int main(){ //freopen("1", "r", stdin); int t; for(scanf("%d", &t); t--;) { bool ok = false; double x1, y1, x2, y2; v v1, v2; scanf("%lf%lf%lf%lf", &seg[0].x, &seg[0].y, &seg[1].x, &seg[1].y); scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); rec[1].x = min(x1, x2); rec[1].y = max(y1, y2); rec[3].x = max(x1, x2); rec[3].y = min(y1, y2); rec[2].y = rec[1].y; rec[2].x = rec[3].x; rec[0].x = rec[1].x; rec[0].y = rec[3].y; v1.start = seg[0]; v1.end = seg[1]; for(int i = 0; i < 2; i++) { if(inRectangle(seg[i], rec[1], rec[3])) { ok = true; break; } } for(int i = 0;!ok && i < 4; i++) { v2.start = rec[i]; v2.end = rec[(i+1)%4]; if(onSegment(seg[0], seg[1], rec[i]) || onSegment(rec[i], rec[(i+1)%4], seg[0]) || onSegment(rec[i], rec[(i+1)%4], seg[1]) || Across(v1, v2)) { ok = true; break; } } puts(ok ? "T" : "F"); } return 0;}
0 0
- 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 (判断线段与矩形是否相交)
- UVA 191 Intersection POJ 1410【向量法判断线段相交】
- POJ 1410 Intersection (判断线段是否与矩形相交)
- POJ 1410 Intersection 判断矩形和线段相交
- POJ 1410 Intersection(判断线段与矩形是否相交)
- POJ 1410 Intersection(线段与矩形相交)
- POJ 1410 Intersection(判断线段和矩形是否相交)
- poj 1410 Intersection 【判断线段 与矩形面是否相交】
- POJ 1410 Intersection(判断线段与矩形是否相交)
- POJ 1410 Intersection [线段相交+点在多边形内]
- 循环链表的表示和实现
- 腾讯2014实习生招聘宣讲会随想
- 数据库 书籍推荐
- linux shell(三):关于linux shell 变量中一些命令场景
- Ajax缓存解决办法
- POJ 1410 Intersection(线段相交)
- GetModuleHandle和AfxGetInstanceHandle和CWinApp->m_hInstance的区别
- Algorithm学习笔记 --- The Pilots Brothers' refrigerator
- Perl 网络编程socket练习(二)
- ios基础
- udhcpc 后台运行的方法【总结】
- 使用Tcmalloc的性能测试结果报告,比malloc有大幅度提高
- 在堆中构建对象以及释放对象
- coder-strike 2014 A Password Check