题目大意:给一个线段和一个矩形,判断线段是否和矩形有公共点。
 
分析:用矩形的四个边当线段判断与所给的线段是否有交点,需要注意的是给的矩形是不标准的,需要自己转换,还需要注意线段有可能在矩形内部。
 
代码如下:
===============================================================================================================================================
#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. iOS 跳转到应用所在的App Store市场

    代码入下 #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @end ...

  2. 使用ArrayList对大小写字母的随机打印

    从a~z以及A~Z随机生成一个字母并打印:打印全部的字母 package com.liaojianya.chapter1; import java.util.ArrayList; /** * This ...

  3. 『重构--改善既有代码的设计』读书笔记----Change Reference to Value

    如果你有一个引用对象,很小且不可改变,而且不易管理,你就需要考虑将他改为一个值对象.在Change Value to Reference我们说过,要在引用对象和值对象之间做选择,有时候并不容易,有了重 ...

  4. U盘安装ubuntu时出现的gfxboot.c32:not a COM32R image问题

    方法特别简单:只需在提示后面输入   live  然后回车 就OK了

  5. 有效解决js中添加border后动画bug问题

    做了个demo发现如果一个div不加border属性,用对象的offsetWidth属性来控制width没问题,但是如果一旦加了border属性,问题就来了. 其实offsetWidth属性获取的的是 ...

  6. 学习hamcrest和mockito时的总结和demo

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...

  7. 什么XSS攻击?PHP防止XSS攻击函数

    什么XSS攻击?PHP防止XSS攻击函数 发布时间: 2013-05-14 浏览次数:22325 分类: PHP教程 XSS 全称为 Cross Site Scripting,用户在表单中有意或无意输 ...

  8. [Linux]关机和重启命令

     Linux中常用的关机和重新启动命令有shutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 1. shu ...

  9. PHPExcel导出excel

    如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312: $yourStr = mb_convert_encoding("g ...

  10. PHP扩展Redis编译安装

    PHP扩展Redis编译安装 1.下载PHP官方Redis源码包  wget http://pecl.php.net/get/redis-2.2.4.tgz  注:我用的是Redhat系统,ubunt ...