POJ 1410 Intersection(线段非规范相交)

题目链接:POJ 1410 Intersection

判断一条线段是否与一个矩形有交点,如果线段在矩形内部,也算有交点。

这题挺多细节需要考虑,要不是discuss上给的68组数据,我还得用更多时间去改bug。

有两个点在矩形内部比较容易处理,面积法搞定,难点在于相交的处理。刘汝佳的模版是线段规范相交的,而这道题是非规范的,就是说可能一条线段的端点恰好与另一条线段相交。但是仅仅加上OnSegment也是不行的,因为这个函数不能处理两个线段恰好端点相交这种情况。

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const double eps = 1e-10;

struct Point
{
	int x, y;
	Point(int x=0, int y=0):x(x),y(y) { }
};

struct Line
{
    Point a, b;
};
typedef Point Vector;

Vector operator + (const Vector& A, const Vector& B)
{
    return Vector(A.x+B.x, A.y+B.y);
}

Vector operator - (const Point& A, const Point& B)
{
    return Vector(A.x-B.x, A.y-B.y);
}

Vector operator * (const Vector& A, double p)
{
    return Vector(A.x*p, A.y*p);
}

bool operator < (const Point& a, const Point& b)
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}


bool operator == (const Point& a, const Point &b)
{
	return (a.x-b.x) == 0 && (a.y-b.y) == 0;
}


double Cross(const Vector& A, const Vector& B)
{
    return A.x*B.y - A.y*B.x;
}

bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
	int c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
		c3 = Cross(b2-b1,a1-b1), c4=Cross(b2-b1,a2-b1);
	return c1*c2<0 && c3*c4<0;
}

int Dot(const Vector& A, const Vector& B)
{
    return A.x*B.x + A.y*B.y;
}

bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
	return Cross(a1-p, a2-p) == 0 && Dot(a1-p, a2-p) < 0;
}

Point p[4];

int get_area(Point a, Point b, Point c)
{
    return abs(a.x * b.y + c.x * a.y + b.x * c.y - c.x * b.y - a.x * c.y - b.x * a.y);
}
bool is_in(int sum, Point k1, Point k2)
{
    double area1, area2, area3, area4;
    area1 = get_area(p[0], p[1], k1);
    area2 = get_area(p[1], p[2], k1);
    area3 = get_area(p[2], p[3], k1);
    area4 = get_area(p[3], p[0], k1);
    if(2 * sum == area1 + area2 + area3 + area4)
        return true;
    area1 = get_area(p[0], p[1], k2);
    area2 = get_area(p[1], p[2], k2);
    area3 = get_area(p[2], p[3], k2);
    area4 = get_area(p[3], p[0], k2);
    if(2 * sum == area1 + area2 + area3 + area4)
        return true;
    return false;
}
bool is_intersect(Point k1, Point k2)
{
    if(SegmentProperIntersection(k1, k2, p[0], p[1]))
        return true;
    if(SegmentProperIntersection(k1, k2, p[1], p[2]))
        return true;
    if(SegmentProperIntersection(k1, k2, p[2], p[3]))
        return true;
    if(SegmentProperIntersection(k1, k2, p[3], p[0]))
        return true;
    if(OnSegment(k1, p[0], p[1]) || OnSegment(k1, p[1], p[2]) || OnSegment(k1, p[2], p[3]) || OnSegment(k1, p[3], p[0]))
        return true;
    if(OnSegment(k2, p[0], p[1]) || OnSegment(k2, p[1], p[2]) || OnSegment(k2, p[2], p[3]) || OnSegment(k2, p[3], p[0]))
        return true;
    if(OnSegment(p[0], k1, k2) || OnSegment(p[1], k1, k2) || OnSegment(p[2], k1, k2) || OnSegment(p[3], k1, k2))
        return true;
    if(k1 == p[0] || k1 == p[1] || k1 == p[2] || k1 == p[3])
        return true;
    if(k2 == p[0] || k2 == p[1] || k2 == p[2] || k2 == p[3])
        return true;

    return false;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int T;
    scanf("%d", &T);
    Line line;
    while(T--)
    {
        scanf("%d%d%d%d", &line.a.x, &line.a.y, &line.b.x, &line.b.y);
        int lx, rx, by, ty, temp;
        scanf("%d%d%d%d", &lx, &ty, &rx, &by);
        if(lx > rx)
            temp = lx, lx = rx, rx = temp;
        if(by > ty)
            temp = by, by = ty, ty = temp;
        p[0] = Point(lx, ty), p[1] = Point(rx, ty), p[2] = Point(rx, by), p[3] = Point(lx, by);
        int area = (ty - by) * (rx - lx);
        if(is_in(area, line.a, line.b) || is_intersect(line.a, line.b))
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}


你可能感兴趣的:(POJ 1410 Intersection(线段非规范相交))