Intersection(poj1401线段和矩形相交)

题意:给你一条线段和一个矩形,判断线段是否和矩形相交,线段在矩形当中也算相交,并且矩形的坐标不是左上右下

思路:这道题知道自己模板错了,

1.线段相交判看是否有线段的端点在线段上,就是说要特判点在线段上

2.点在直线上要包括点在线段的两个端点,即dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);

剩下的就是细心加细心

#include
#include
#include
#include
#include
using namespace std;
struct Point
{
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-5;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
    return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

double Min(double a,double b)
{
    return a < b ? a : b;
}
double Max(double a,double b)
{
    return a > b ? a  : b;
}
bool Onsegment(Point p,Point a,Point b)
{
    return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);
}
bool SegmentIntersection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true;
    else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true;
    else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true;
    else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true;
    else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true;
    else return false;
}

bool pointInpoly(Point p,Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,p-a);
    double d2 = Cross(d-c,p-c);
    double d3 = Cross(c-b,p-b);
    double d4 = Cross(a-d,p-d);
    return dcmp(d1)*dcmp(d2) > 0 && dcmp(d3)*dcmp(d4) > 0;
}
struct Line
{
    Point s,e;
    Line(Point s = 0,Point e = 0) :s(s),e(e){}
};

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        double x,y,x1,y1,x2,y2,x3,y3;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x,&y,&x1,&y1,&x2,&y2,&x3,&y3);
        Point a,b,c,d;
        if(x3 < x2 &&  y2 < y3)
        {
            int t = x2;     x2 = x3;       x3 = t;
            t = y2;     y2 = y3;     y3 = t;
             a = Point(x2,y2),b = Point(x2,y3),c = Point(x3,y3),d = Point(x3,y2);
        }
        else if(x2 < x3 && y2 > y3)
        {
             a = Point(x2,y2),b = Point(x2,y3),c = Point(x3,y3),d = Point(x3,y2);
        }
        else if(x2 > x3 && y2 > y3)
        {
             a = Point(x2,y2),b = Point(x3,y2),c = Point(x3,y3),d = Point(x2,y3);
        }
        else if(x3 > x2 && y3 > y2)
        {
             a = Point(x3,y3),b = Point(x2,y3),c = Point(x2,y2),d = Point(x3,y2);
        }
        else
            a = Point(x3,y3),b = Point(x2,y3),c = Point(x2,y2),d = Point(x3,y2);
        if(SegmentIntersection(Point(x,y),Point(x1,y1),a,b))
            printf("T\n");
        else  if(SegmentIntersection(Point(x,y),Point(x1,y1),b,c))
            printf("T\n");
        else  if(SegmentIntersection(Point(x,y),Point(x1,y1),c,d))
            printf("T\n");
        else  if(SegmentIntersection(Point(x,y),Point(x1,y1),d,a))
            printf("T\n");
        else if( pointInpoly(Point(x,y),a,b,c,d) && pointInpoly(Point(x1,y1),a,b,c,d))
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}


你可能感兴趣的:(计算几何)