线段和矩形相交 POJ 1410

  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 }
时间: 2024-10-23 08:37:10

线段和矩形相交 POJ 1410的相关文章

判断线段和直线相交 POJ 3304

1 // 判断线段和直线相交 POJ 3304 2 // 思路: 3 // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 #include <map> 10 #include <set> 11 #inclu

poj 1410 Intersection (判断线段与矩形相交 判线段相交)

题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 3125 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:

POJ 1410 Intersection (线段和矩形相交)

题目: 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 doe

简单几何(线段相交) POJ 1410 Intersection

题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /************************************************ * Author :Running_Time * Created Time :2015/10/27 星期二 13:17:49 * File Name :POJ_1410.cpp ******************************************

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

poj 1410 线段相交判断

http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11329   Accepted: 2978 Description You are to write a program that has to decide whether a given line segment intersects a given rectangle. An ex

POJ 1410 Intersection --几何,线段相交

题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单纯做了两次跨立实验,在下图这种情况是错误的: 这样的话线段与右边界的两次跨立实验(叉积<=0)都会通过,但是并不相交. 所以要加快速排斥. 还有就是这题题目说给出的不一定是左上角,右下角依次的顺序.所以干脆重新自己定义左上角,右下角. 代码: #include <iostream> #inc

线段相交 poj 1066

1 // 线段相交 poj 1066 2 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数 3 4 // #include <bits/stdc++.h> 5 #include <iostream> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <algorithm> 9 #include <vector> 10 #include <math.h>