poj 1414

来源:互联网 发布:徐州共享网络 编辑:程序博客网 时间:2024/06/15 13:40

没有理解好相交什么意思WA无数次

       判断线段是否和矩形相交。而所谓“相交”,在计算几何的角度来看,就是线段有一点在矩形内或矩形上。判断的方法如下:

      判断线段的两端点是否在矩形内,若是,则线段在矩形内。

      判断线段是否与矩形相交,即是否和矩形的四条边中的任意一条边相交(规范相交和不规范相交都算)

#include <iostream>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


using namespace std;
#define eps 1e-10


struct Point
{
    double x, y;
    Point(){};
    Point(double a, double b)
    {
        x = a;
        y = b;
    }
}p[10], s, e;


int sig(double a)
{
    if(a < -eps)
     return -1;
    if(a > eps)
     return 1;
    return 0;
}


double Across(Point a ,Point b, Point c)
{
    return (b.x - a.x)*(c.y - a.y) - (b.y - a.y) *(c.x - a.x);
}


bool oneseg(Point a, Point b, Point c)
{
    if(c.x >= min(b.x , a.x) && c.x <= max(b.x , a.x) && c.y >= min(b.y , a.y) && c.y <= max(b.y , a.y))
     return true;
    return false;
}


bool in(Point &a)
{
    if (a.x <= p[2].x && a.x >= p[0].x && a.y <= p[2].y && a.y
            >= p[0].y)
        return true;
    return false;
}


bool SegAcorss(Point a, Point b , Point c, Point d)
{
    int d1 = sig( Across(a, b, c));
    int d2 = sig( Across(a, b, d));
    int d3 = sig( Across(c, d, a));
    int d4 = sig( Across(c, d, b));


    if(d1*d2 == -1 && d3 * d4 == -1)
     return 1;
    if(d1 == 0 && oneseg(a, b, c))
     return 1;
    if(d2 == 0 && oneseg(a, b, d))
     return 1;
    if(d3 == 0 && oneseg(c, d, a))
     return 1;
    if(d4 == 0 && oneseg(c, d, b))
     return 1;
    return 0;
}


int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &s.x, &s.y, &e.x, &e.y, &p[0].x,
                &p[0].y, &p[2].x, &p[2].y);
        if (p[0].x > p[2].x)
            swap(p[0].x, p[2].x);
        if (p[0].y > p[2].y)
            swap(p[0].y, p[2].y);
        p[1].x = p[0].x;
        p[1].y = p[2].y;
        p[3].x = p[2].x;
        p[3].y = p[0].y;
        p[4] = p[0];
        bool ok = false;
        for (int i = 0; i < 4; i++)
            if (SegAcorss(p[i], p[i + 1], s, e))
            {
                ok = true;
                break;
            }
        if (in(s) || in(e))
            ok = true;
        if (ok)
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}
0 0
原创粉丝点击