码迷,mamicode.com
首页 > 其他好文 > 详细

线段和矩形相交 POJ 1410

时间:2016-09-09 16:59:12      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

  1 // 线段和矩形相交 POJ 1410
  2 
  3 // #include <bits/stdc++.h>
  4 #include <iostream>
  5 #include <cstdio>
  6 #include <cstdlib>
  7 #include <algorithm>
  8 #include <vector>
  9 #include <math.h>
 10 using namespace std;
 11 #define LL long long
 12 typedef pair<int,int> pii;
 13 const int inf = 0x3f3f3f3f;
 14 const LL MOD =100000000LL;
 15 const int N =110;
 16 #define clc(a,b) memset(a,b,sizeof(a))
 17 const double eps = 1e-8;
 18 void fre() {freopen("in.txt","r",stdin);}
 19 void freout() {freopen("out.txt","w",stdout);}
 20 inline int read() {int x=0,f=1;char ch=getchar();while(ch>9||ch<0) {if(ch==-) f=-1; ch=getchar();}while(ch>=0&&ch<=9) {x=x*10+ch-0;ch=getchar();}return x*f;}
 21 
 22 int sgn(double x){
 23     if(fabs(x) < eps)return 0;
 24     if(x < 0)return -1;
 25     else return 1;
 26 }
 27 struct Point{
 28     double x,y;
 29     Point(){}
 30     Point(double _x,double _y){
 31         x = _x;y = _y;
 32     }
 33     Point operator -(const Point &b)const{
 34         return Point(x - b.x,y - b.y);
 35     }
 36     double operator ^(const Point &b)const{
 37         return x*b.y - y*b.x;
 38     }
 39     double operator *(const Point &b)const{
 40         return x*b.x + y*b.y;
 41     }
 42 };
 43 
 44 struct Line{
 45     Point s,e;
 46     int inx;
 47     Line(){}
 48     Line(Point _s,Point _e){
 49         s=_s;e=_e;
 50     }
 51 };
 52 
 53 // Line line[N];
 54 bool inter(Line l1,Line l2){
 55     return 
 56         max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
 57         max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
 58         max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
 59         max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
 60         sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
 61         sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
 62 }
 63 bool OnSeg(Point P,Line L){
 64     return
 65     sgn((L.s-P)^(L.e-P))==0&&
 66     sgn((P.x-L.s.x)*(P.x-L.e.x))<=0&&
 67     sgn((P.y-L.s.y)*(P.y-L.e.y))<=0;
 68 }
 69 
 70 int inConvexPoly(Point a,Point p[],int n){
 71     for(int i=0;i<n;i++){
 72         if(sgn((p[i]-a)^(p[(i+1)%n]-a))<0) return -1;
 73         else if (OnSeg(a,Line(p[i],p[(i+1)%n]))) return 0;
 74     }
 75     return 1;
 76 }
 77 
 78 Point p[4];
 79 int main(){
 80     int T;
 81     scanf("%d",&T);
 82     while(T--){
 83         double x1,y1,x2,y2;
 84         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 85         Line line=Line(Point(x1,y1),Point(x2,y2));
 86         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 87         if(x1>x2) swap(x1,x2);
 88         if(y1>y2) swap(y1,y2);
 89         p[0]=Point(x1,y1);
 90         p[1]=Point(x2,y1);
 91         p[2]=Point(x2,y2);
 92         p[3]=Point(x1,y2);
 93         if(inter(Line(p[0],p[1]),line)){
 94             printf("T\n");
 95             continue;
 96         }
 97         if(inter(Line(p[1],p[2]),line)){
 98             printf("T\n");
 99             continue;
100         }
101         if(inter(Line(p[2],p[3]),line)){
102             printf("T\n");
103             continue;
104         }
105         if(inter(Line(p[3],p[0]),line)){
106             printf("T\n");
107             continue;
108         }
109         if(inConvexPoly(line.s,p,4)>=0||inConvexPoly(line.e,p,4)>=0){
110             printf("T\n");
111             continue;
112         }
113         else 
114             printf("F\n");
115     }
116     return 0;
117 }

 

线段和矩形相交 POJ 1410

标签:

原文地址:http://www.cnblogs.com/ITUPC/p/5857099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!