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

    1.IE6怪异解析之padding与border算入宽高 原因:未加文档声明造成非盒模型解析 解决方法:加入文档声明<!doctype html> 2.IE6在块元素.左右浮动.设定mar ...

  2. YZOI Easy Round 2_化简(simplify.c/cpp/pas)

    Description 给定一个多项式,输出其化简后的结果. Input 一个字符串,只含有关于字母x 的多项式,不含括号与分式,没有多余的空格. Output 一个字符串,化简后的多项式,按照次数从 ...

  3. NLPIR.user Not valid license or your license expired! Please feel free to contact pipy_zhang@msn.com

    NLPIR.user Not valid license or your license expired! Please feel free to contact pipy_zhang@msn.com ...

  4. [转] 小tips: 使用&#x3000;等空格实现最小成本中文对齐 ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4562 一.重见天日第 ...

  5. python 元组问题解决

    a = (1,2,{'k1':'b2'}) a[2]['k1'] = 5 print(a) (1, 2, {'k1': 5}) 为什么元素'b2' = 5 应该是元素'k1' = 5 求解 a[2][ ...

  6. awk基础 [马哥视频]

    awk基础 1.1 print print的使用格式: print item1,item2, …. 要点: 各项目自己使用逗号隔开,而输出时则以空白字符分隔: 输出的item可以为字符串或者数值,当前 ...

  7. 完全卸载mysql 停止服务、卸载相关程序、删除注册表

    本节主要介绍了完全卸载mysql的具体步骤包括停止服务.卸载相关程序.删除注册表等等   1. 停止服务MySQL 2. 卸载mysql相关的程序 3. 删除注册表(运行->regedit),m ...

  8. common头文件

    #ifndef COMMON_HHH #define COMMON_HHH #define ASSERT(p) \ do{\ if (!p){\ printf("%s:%d\n", ...

  9. Contest 20140708 testB dp 组合数

    testB 输入文件: testB.in  输出文件testB.out 时限3000ms 问题描述: 定义这样一个序列(a1,b1),(a2,b2),…,(ak,bk)如果这个序列是方序列的话必须满足 ...

  10. [BZOJ 2127] happiness 【最小割】

    题目链接:BZOJ - 2127 题目分析 首先,每个人要么学文科,要么学理科,所以可以想到是一个最小割模型. 我们就确定一个人如果和 S 相连就是学文,如果和 T 相连就是学理. 那么我们再来确定建 ...