joj 1131: Intersection (判断直线与矩形是否有交点)

来源:互联网 发布:天津广电网络宽带 编辑:程序博客网 时间:2024/06/05 19:13

 You are to write a program that has to decide whether a given line segment intersects a given rectangle.

The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.

Input

The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format: xstart ystart xend yend xleft ytop xright ybottom where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.

Output

For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.

Sample Input

14 9 11 2 1 5 7 1

Sample Output

F

 

方法一:

常规计算几何方法 :判断

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
using namespace std;
const double esp=1e-8;
struct point
{
      double x,y;
}s,e,p[5];
//近似判断浮点数与0的关系
int dblcmp (double a)
{
    if(fabs(a)<esp) return 0;
    return (a>0)?1:-1 ;
}
double min(double a, double b)
{
        return a < b ? a : b;
}
double max(double a, double b)
{
       return a > b ? a : b;
}
double det (double x1,double y1,double x2,double y2)
{
       return x1*y2-x2*y1;
}
//叉乘
double cross (point a,point b)
{
return a.x*b.y-a.y*b.x;
}
//利用点积判断已知在线段所在直线上的点是否在
double dotdet (point a,point b)
{
       return a.x*b.x+a.y*b.y;
}
double dot (point a, point b,point c)
{
       point x,y;
       x.x=c.x-b.x;
       x.y=c.y-b.y;
       y.x=a.x-b.x;
       y.y=a.y-b.y;
       return dotdet(x,y);
}
//利用叉乘判断点的方向 (顺or逆)
double dir(point p1,point p2 ,point p3)
{
       point x1,x2;
       x1.x = p2.x-p1.x;
       x1.y = p2.y-p1.y;
       x2.x = p3.x-p1.x;
       x2.y = p3.y-p1.y;
       return cross(x1,x2);
}
//判断p3是否是p1 p2的凸组合
bool on_seg(point p1 , point p2 , point p3)
{
     return (p3.x<=max(p2.x,p1.x)&&p3.x>=min(p2.x,p1.x)&&p3.y<=max(p2.y,p1.y)&&p3.y>=min(p2.y,p1.y));
}
bool init(double a, double b , double c)
{
     return (c>=a && c<=b) ? 1 : 0;
}
bool intersect()
{
     double s1,s2,s3,s4;
     int d1,d2,d3,d4;
     for (int i=1 ; i<=4 ; i++)
     {
         int j=i+1;if(j==5)j=1;
         d1=dblcmp(s1=dir(p[i],p[j],s));
         d2=dblcmp(s2=dir(p[i],p[j],e));
         d3=dblcmp(s3=dir(s,e,p[i]));
         d4=dblcmp(s4=dir(s,e,p[j]));
         if( (d1^d2)==-2 && (d3^d4)==-2 ) return 1;
         if(d3==0 && ( on_seg(s,e,p[i]) ))return 1;
         if(d4==0 && (on_seg(s,e,p[j])) ) return 1;
     }
     return 0;
}
int main ()
{
    int n;
    scanf("%d",&n);
    while (n--)
    {
          //scanf("%d%d%d%d%d%d%d%d",&s.x,&s.y,&e.x,&e.y,&p[1].x,&p[1].y,&p[3].x,&p[3].y);
          scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&s.x,&s.y,&e.x,&e.y,&p[1].x,&p[1].y,&p[3].x,&p[3].y);
          if(p[3].x<p[1].x){double t=p[3].x;p[3].x=p[1].x;p[1].x=t;}
          if(p[3].y>p[1].y){double t=p[3].y;p[3].y=p[1].y;p[1].y=t;}
          p[2].x=p[3].x;
          p[2].y=p[1].y;
          p[4].x=p[1].x;
          p[4].y=p[3].y;
          //先判断线段端点是否在矩形中 的情况
          if ( (init(p[1].x,p[3].x,s.x) && init(p[3].y,p[1].y,s.y) ) || (init (p[1].x,p[3].x,e.x)&&init(p[3].y,p[1].y,e.y)) )
          {printf("T\n");continue;}
          //再判断线段端点不在矩形中时,与矩形边是否规范相交 的情况以及判断4个顶点是否在线段上
          if(intersect())printf("T\n");
          else printf("F\n");
    }
    return 0;
}

 

方法二 : 在网上看到其他大牛的超短代码,经分析后发现巧妙的利用了这道题矩形四边与x轴 y轴垂直的条件,先判断线段所在直线是否穿过某一矩形对角线(1),若不穿过,则线段与矩形必不相交,若穿过在判断线段两个端点是否在矩形的一侧,因为有了条件(1)则不在一侧时必与矩形相交,否则不相交。

根据大牛的算法写的代码:

计算几何应避免除法 所以 直线方程应设为(y1-y2)*x+(x2-x1)*y+x1*y2-x2*y1=0.而不是kx-y+b=0的形式。

#include <cstdio>

using namespace std;

struct {
    int x,y;
}m1,m2,s,e;

int main ()
{
    int n;
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d%d%d%d%d%d%d%d",&s.x,&s.y,&e.x,&e.y,&m1.x,&m1.y,&m2.x,&m2.y);
        int a=s.y-e.y , b=e.x-s.x , c=e.y*s.x-e.x*s.y;
        if(m1.x>m2.x){int x=m1.x ; m1.x=m2.x ; m2.x=x ;}
        if(m1.y<m1.y){int y=m1.y ; m1.y=m2.y ; m2.y=y ;}
        if( ((a*m1.x+b*m1.y+c)*(a*m2.x+b*m2.y+c))<=0 || ((a*m2.x+b*m1.y+c)*(a*m1.x+b*m2.y+c))<=0 )
        // judge of whether straight line thought the rectangular diagonal
        {
            if( (s.x<m1.x && e.x<m1.x)||(s.x>m2.x && e.x>m2.x)||
                 (s.y>m1.y && e.y>m1.y)||(s.y<m2.y && e.y<m2.y) )
        // judge of whether the two points in one side of rectangular
                 printf("F\n");
            else printf("T\n");
        }
        else printf("F\n");
    }
    return 0;
}

 

 

原创粉丝点击