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;坑爹)的更多相关文章

  1. poj 1410 Intersection 线段相交

    题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...

  2. POJ 1410 Intersection (线段和矩形相交)

    题目: Description You are to write a program that has to decide whether a given line segment intersect ...

  3. POJ 1410 Intersection --几何,线段相交

    题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...

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

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  5. [POJ 1410] Intersection(线段与矩形交)

    题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  7. POJ 1410 Intersection (计算几何)

    题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...

  8. [poj 1039]Pipes[线段相交求交点]

    题意: 无反射不透明管子, 问从入口射入的所有光线最远能到达的横坐标. 贯穿也可. 思路: 枚举每一组经过 up [ i ] 和 down [ j ] 的直线, 计算最远点. 因为无法按照光线生成的方 ...

  9. POJ 1410 Intersection(计算几何)

    题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...

随机推荐

  1. 2015腾讯暑期实习生 Web前端开发 面试经历 --作者imwtr

    1.现在有100亿个数字大小为1到10亿的数字,在这100亿个数字里边只有一个数字出现的次数是奇数次的,你用什么方法,找出这个数字呢? 答:显然至少要全部过一遍,可以用位运算,遍历数组,最后剩下的数字 ...

  2. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  3. HDU 3729 二分匹配 反向匹配

    题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定  点 ...

  4. Android 开发笔记 “The constructor AlertDialog.Builder(new View.OnKeyListener(){}) is undefined”

    1.The constructor AlertDialog.Builder(new View.OnKeyListener(){}) is undefined等,应该有很多类似问题 比如你的源码文件名是 ...

  5. Java format 简单应用

    一.前言 String.format 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.format("Hello %s", " ...

  6. [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  7. night Mode 夜间模式css

    *,*:before,*:after,html[mode='nightmode'] * { color: #61615f !important; border-color: #212a32 !impo ...

  8. java牛人给新人的几点建议

    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过的1 ...

  9. 帝国cms语句调用

    帝国cms系统,灵动标签,有着非常强大的数据调用功能.这里为广大菜鸟站长普及一下. 我们来看这段代码. [e:loop={,,}] <li>·<a target="_bla ...

  10. 电脑cmos是什么?和bois的区别?

    很多人都分不清电脑cmos和bois区别,有人一会儿说什么bois设置,有人一会儿说cmos设置.而看起来这两个又似乎差不多,本文将用最简单的白话文告诉各位,什么是cmos,以及cmos和bois的的 ...