题目大意:给一个线段和一个矩形,判断线段是否和矩形有公共点。
 
分析:用矩形的四个边当线段判断与所给的线段是否有交点,需要注意的是给的矩形是不标准的,需要自己转换,还需要注意线段有可能在矩形内部。
 
代码如下:
===============================================================================================================================================
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const int MAXN = 1e3+;
const int oo = 1e9+;
const double EPS = 1e-; struct point
{
double x, y;
point(double x=, double y=):x(x), y(y){}
point operator - (const point &t) const{
return point(x-t.x, y-t.y);
}
int operator * (const point &t) const{
double ans = x*t.y - y*t.x; if(ans > EPS)return ;
if(fabs(ans) < EPS)return ;
return -;
}
};
struct segment
{
point A, B;
segment(point A=, point B=):A(A), B(B){}
bool Intersect(const segment &t)const{
int t1 = (A-B) * (t.A-B);
int t2 = (A-B) * (t.B-B); if(t1== && t.A.x>=min(A.x, B.x) && t.A.x <= max(A.x, B.x)
&& t.A.y>=min(A.y, B.y) && t.A.y <= max(A.y, B.y))return true;
if(t2== && t.B.x>=min(A.x, B.x) && t.B.x <= max(A.x, B.x)
&& t.B.y>=min(A.y, B.y) && t.B.y <= max(A.y, B.y))return true;
if(t1==&&t2== && max(t.A.x,t.B.x)>=max(A.x,B.x) &&min(t.A.x,t.B.x)<=min(A.x,B.x)
&& max(t.A.y,t.B.y)>=max(A.y,B.y) &&min(t.A.y,t.B.y)<=min(A.y,B.y) )return true;
if(t1*t2 == -)return true; return false;
}
};
bool Find(segment a, segment sg[], int N)
{
for(int i=; i<N; i++)
{
if(a.Intersect(sg[i]) && sg[i].Intersect(a))
return true;
} return false;
} int main()
{
int T, t=; scanf("%d", &T); while(T--)
{
segment a, sg[];
point A, B; scanf("%lf%lf%lf%lf", &a.A.x, &a.A.y, &a.B.x, &a.B.y);
scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y); if(A.x > B.x)swap(A.x, B.x);
if(A.y < B.y)swap(A.y, B.y); sg[] = segment(A, point(A.x, B.y));
sg[] = segment(A, point(B.x, A.y));
sg[] = segment(B, point(A.x, B.y));
sg[] = segment(B, point(B.x, A.y)); /// printf("Case %d: ", t++);
if(Find(a, sg, ) == true || (a.A.x>=A.x&&a.A.x<=B.x && a.A.y>=B.y && a.A.y <= A.y) )
printf("T\n");
else
printf("F\n");
} return ;
}
/**
2
4 2 4 0 4 3 9 6
**/

Intersection - POJ 1410(线段与矩形是否相交)的更多相关文章

  1. poj1410(判断线段和矩形是否相交)

    题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两 ...

  2. poj 1410 线段相交判断

    http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  3. hdu 1410(直线与矩形相交)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13528   Accepted: 3521 Des ...

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

    Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...

  5. poj 1410 Intersection (判断线段与矩形相交 判线段相交)

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

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

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

  7. 线段和矩形相交 POJ 1410

    // 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...

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

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

  9. poj 1410 Intersection 线段相交

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

随机推荐

  1. vi 替换字符串

    假如说我想把该文件中所有的Web替换成SOR_SYS,那么我们可以用vi打开该文件,然后按一下: 你的命令行的最后一行会出现:,这个时候就是提醒你输入替换的命令 %s/Web/SOR_SYS/g 按一 ...

  2. c语言中文件相关操作

    一 .首先介绍一下数据文件的类型: 1.二进制文件(映像文件):在内存中以二进制形式存取. 2.文本文件(ascii文件):以ascii码形式存取的文件. 通俗的讲,在Mac下,你把一个文件丢进记事本 ...

  3. list集合练习一

    package com.java.c.domain; public class Person { private String name; private int age; public Person ...

  4. Strategy 模式

    可以看到 Strategy 模式和 Template 模式解决了类似的问题,也正如在 Template 模式中分析的,Strategy模式和 Template 模式实际是实现一个抽象接口的两种方式:继 ...

  5. hibernate中一对多 多对多 inverse cascade

    ----------------------------一对多------------------------------------------- inverse属性:是在维护关联关系的时候起作用的 ...

  6. WF学习笔记(二)

    -DoWhile循环:当[Condition]条件为真时会执行[Body]中的内容, 当[Condition]条件为假时会执行[Body]中的内容一次 -ForEach<T> 循环 :[V ...

  7. laravel学习:修改时区

    config/app.php 'timezone' => 'UTC', 改为 'timezone' => 'PRC',

  8. bcov进行覆盖率统计

    kcov是在bcov基础上进行的,bcov已经很久没有维护了: 首先需要下载依赖库libdwraft,然后在configure时候进行指定: ./configure --with-libdwarf=/ ...

  9. 正则表达式替换img标签src值!!!

    方法一: 相关链接:http://bbs.csdn.net/topics/320185735 实例:此实例自己做的时候讲字符串加了alt进行了有关修改  不清楚看上面链接 string test = ...

  10. C# mvc 验证码3

    //// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长 ...