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

Intersection

大意:给你一条线段,给你一个矩形,问是否相交。

    相交:线段全然在矩形内部算相交;线段与矩形随意一条边不规范相交算相交。

思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑。

struct Point
{
    double x, y;
} A, B, C, D;
struct Line
{
    Point a, b;
} L;

int n;

double xmult(Point p1, Point p2, Point p)
{
    return (p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y);
}

///若共线,返回1;不共线,返回0。
int dot_inLine(Point p1, Point p2, Point p3){
    return zero(xmult(p1, p2, p3));
}
///判两点在线段同側,点在线段上返回0
int same_side(Point p1, Point p2, Line l){
    return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) > eps;
}
///判点是否在线段上,包含端点
int dot_onLine_in(Point p, Line l){
    return zero(xmult(p, l.a, l.b)) && (l.a.x-p.x)*(l.b.x-p.x) < eps && (l.a.y-p.y)*(l.b.y-p.y) < eps;
}
int intersect_in(Line u, Line v){
    if (!dot_inLine(u.a, u.b, v.a)
        || !dot_inLine(u.a, u.b, v.b))
        return !same_side(u.a, u.b,v) && !same_side(v.a, v.b,u);
    return dot_onLine_in(u.a, v) || dot_onLine_in(u.b, v)
        || dot_onLine_in(v.a, u) || dot_onLine_in(v.b, u);
}

bool is_Inter(Point A, Point B, Point C, Point D, Point t)
{
    if(xmult(t, A, B) > eps && xmult(t, B, C) > eps && xmult(t, C, D) > eps && xmult(t, D, A) > eps)
        return true;
    if(xmult(t, A, B) < eps && xmult(t, B, C) < eps && xmult(t, C, D) < eps && xmult(t, D, A) < eps)
        return true;
    if(t.x >= A.x && t.x <= B.x && t.y >= C.y && t.y <= B.y && (zero(xmult(t, A, B)) || zero(xmult(t, B, C)) || zero(xmult(t, C, D)) || zero(xmult(t, D, A))))
       return true;
    return false;
}

int T;

void Solve()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y, &C.x, &C.y, &D.x, &D.y);
        A.x = min(C.x, D.x);
        A.y = max(C.y, D.y);
        C.x = max(C.x, D.x);
        C.y = min(C.y, D.y);
        B.x = C.x, B.y = A.y;
        D.x = A.x, D.y = C.y;
        if(is_Inter(A, B, C, D, L.a) && is_Inter(A, B, C, D, L.b))
        {
            printf("T\n");
        }
        else if(intersect_in((Line){A, B}, L) || intersect_in((Line){B, C,}, L) || intersect_in((Line){C, D}, L) || intersect_in((Line){D, A}, L))
        {
            printf("T\n");
        }
        else
        {
            printf("F\n");
        }
    }
}

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

时间: 2024-08-08 13:42:16

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

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 2653 (线段相交判断)

http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9531   Accepted: 3517 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishi

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 --几何,线段相交

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

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

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

POJ 1410 Intersection(计算几何)

题目链接:id=1410">Intersection 推断线段与矩形的关系.与矩形相交打印T,否则打印F. 坑题,精度. . .. 思路就是,先推断 线段是否在矩形里面,再推断线段和两条对角线的关系,利用叉积模板就可以 測试数据有个坑,就是 左上角的坐标并不一定比右下角的小. ..这根本不符合题意嘛 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring&

poj 1066(枚举+线段相交)

Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6328   Accepted: 2627 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-ar

POJ 1410 Intersection

这题我就是用最原始的思考方法,其中有许多细节要注意.主体思想就是四条边分别和线段比较. 线段在矩形内要考虑. 我的代码有点乱有点长,其中有的部分可以写成函数. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int x_s,y_s,x_e,y_e,x_l,y_t,x_r,y_b; bool f1(int x) { retu

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: