POJ 1410 Intersection
2013-05-18 19:43
295 查看
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; struct Point { double x,y; }; struct Segment { Point p1,p2; }; double cross(const Point& o,const Point & a,const Point& b) { return (a.x - o.x)*(b.y-o.y) - (a.y - o.y)* (b.x - o.x); } double dot(const Point& o,const Point & a,const Point& b) { return (a.x - o.x) * (b.x - o.x) + (a.y-o.y) * (b.y - o.y); } const double eps = 1E-8; int sig(double d) { return (d > eps) - (d < -eps); } int btw(const Point& x,const Point& a,const Point&b) { return sig(dot(x,a,b)); } int SegCross(const Point & a,const Point & b,const Point& c,const Point& d) { int d1,d2,d3,d4; d1 = sig(cross(a,b,c)); d2 = sig(cross(a,b,d)); d3 = sig(cross(c,d,a)); d4 = sig(cross(c,d,b)); if((d1^d2)==-2&&(d3^d4)==-2) { return 1; } if(d1 == 0 && btw(c,a,b)<=0|| d2 == 0 && btw(d,a,b)<=0 || d3 == 0 && btw(a,c,d)<=0 || d4 == 0 && btw(b,c,d) <=0) return 2; return 0; } int main() { int n; cin >> n; Segment seg; Segment rseg[4]; Point p1,p2,p3,p4; int flag; while(n--) { flag = 0; scanf("%lf%lf%lf%lf",&seg.p1.x,&seg.p1.y,&seg.p2.x,&seg.p2.y); scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y); p3.x = p2.x; p3.y = p1.y; p4.x = p1.x; p4.y = p2.y; flag += SegCross(seg.p1,seg.p2,p1,p3); flag += SegCross(seg.p1,seg.p2,p1,p4); flag += SegCross(seg.p1,seg.p2,p2,p3); flag += SegCross(seg.p1,seg.p2,p2,p4); //cout << "flag = " << flag << endl; if(flag) cout << "T" << endl; else { if((min(p1.x,p2.x) < min(seg.p1.x,seg.p2.x )) && (min(p1.y,p2.y) < min(seg.p1.y,seg.p2.y )) && (max(p1.x,p2.x) > max(seg.p1.x,seg.p2.x )) && (max(p1.y,p2.y) > max(seg.p1.y,seg.p2.y )) ) cout << "T" << endl; else cout << "F" << endl; } } return 0; }
相关文章推荐
- poj 1410 Intersection(线段与矩形相交)
- POJ 1410 Intersection
- POJ 1410 Intersection 笔记
- POJ 1410 Intersection(一线段是否与矩阵相交)
- poj 1410 Intersection
- poj1410 - Intersection
- POJ 1410 || Intersection(线段矩形相交
- poj 1410 Intersection
- POJ 1410 Intersection 判断矩形和线段相交
- poj 1410 Intersection 【判断线段 与矩形面是否相交】
- poj 1410 Intersection
- POJ 1410 Intersection --几何,线段相交
- 线段覆盖POJ1410 Intersection
- poj1410 Intersection 线段与矩形相交
- POJ 1410 Intersection(判断线段交和点在矩形内)
- POJ 1410 Intersection(计算几何)
- poj 1410 Intersection
- POJ 1410 Intersection --几何,线段相交
- POJ 1410 Intersection
- POJ 1410 Intersection