题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?)

思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可。

然后题目的数据,(xleft,ytop) 和 (xright,ybottom)不是按顺序给出的,需要自己判断下顺序。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
}a,b;
struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
}line[maxn],seg;
const double eps = 1e-;
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
bool OnSegment (coor a,coor b,coor c) //判断点C是否在线段ab上
{
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (c.x>=min_x && c.x<=max_x && c.y>=min_y && c.y<=max_y) return true;
else return false;
}
bool SegmentIntersect (coor a,coor b,coor c,coor d)
{
double d1 = (b-a)^(d-a); //direction(a,b,d);以a为起点,计算ab和ab的叉积
double d2 = (b-a)^(c-a);
double d3 = (d-c)^(a-c);
double d4 = (d-c)^(b-c);
if (d1*d2< && d3*d4<) return true;
else if (same(d1,) && OnSegment(a,b,d)) return true;
else if (same(d2,) && OnSegment(a,b,c)) return true;
else if (same(d3,) && OnSegment(c,d,a)) return true;
else if (same(d4,) && OnSegment(c,d,b)) return true;
else return false;
}
void work ()
{
scanf("%lf%lf%lf%lf",&seg.point1.x,&seg.point1.y,&seg.point2.x,&seg.point2.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);
line[] = Line(a,coor(b.x,a.y));
line[] = Line(coor(a.x,b.y),b);
line[] = Line(a,coor(a.x,b.y));
line[] = Line(coor(b.x,a.y),b);
for (int i=;i<=;++i)
{
if (SegmentIntersect(seg.point1,seg.point2,line[i].point1,line[i].point2))
{
printf ("T\n");
return ;
}
}
if (seg.point1.x>=a.x&&seg.point1.x<=b.x&&seg.point2.x>=a.x&&seg.point2.x<=b.x
&&seg.point1.y>=b.y&&seg.point1.y<=a.y&&seg.point2.y>=b.y&&seg.point2.y<=a.y)
{
printf ("T\n");
return ;
}
printf ("F\n");
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin>>t;
while(t--) work();
return ;
}

然而这题应该用判断点在多边形里比较好

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
}a,b,liu[maxn];
struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
}line[maxn],seg;
const double eps = 1e-;
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
bool OnSegment (coor a,coor b,coor c) //判断点C是否在线段ab上
{
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (c.x>=min_x && c.x<=max_x && c.y>=min_y && c.y<=max_y) return true;
else return false;
}
bool SegmentIntersect (coor a,coor b,coor c,coor d)
{
double d1 = (b-a)^(d-a); //direction(a,b,d);以a为起点,计算ab和ab的叉积
double d2 = (b-a)^(c-a);
double d3 = (d-c)^(a-c);
double d4 = (d-c)^(b-c);
if (d1*d2< && d3*d4<) return true;
else if (same(d1,) && OnSegment(a,b,d)) return true;
else if (same(d2,) && OnSegment(a,b,c)) return true;
else if (same(d3,) && OnSegment(c,d,a)) return true;
else if (same(d4,) && OnSegment(c,d,b)) return true;
else return false;
}
bool PointInPolygon (coor p[],int n,coor cmp)
{
//思路:求解y=cmp.y与多边形一侧有多少个交点,奇数就在里面,偶数就在外面,cmp在边上是不行的
int cnt = ; //记录单侧有多少个交点,这里的p[],必须有顺序
for (int i=;i<=n;++i)
{
int t = (i+)>n ? :(i+); //下标为1要这样
coor p1=p[i],p2=p[t];
if (cmp.y >= max(p1.y,p2.y)) continue;//交点在延长线上和在凸顶点都不要
if (cmp.y < min(p1.y,p2.y)) continue;//交点在凹顶点上就要,这里没取等
if (same(p1.y,p2.y)) continue; //与cmp.y是平行的
double x = (cmp.y-p1.y)*(p1.x-p2.x)/(p1.y-p2.y) + p1.x; //求交点 p1.y != p2.y不会除0错误
if (x>cmp.x) cnt++;//只统计一侧的交点
}
return cnt&;
}
void work ()
{
scanf("%lf%lf%lf%lf",&seg.point1.x,&seg.point1.y,&seg.point2.x,&seg.point2.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);
line[] = Line(a,coor(b.x,a.y));
line[] = Line(coor(a.x,b.y),b);
line[] = Line(a,coor(a.x,b.y));
line[] = Line(coor(b.x,a.y),b); liu[]=a;
liu[]=coor(b.x,a.y);
liu[]=b;
liu[]=coor(a.x,b.y); for (int i=;i<=;++i)
{
if (SegmentIntersect(seg.point1,seg.point2,line[i].point1,line[i].point2))
{
printf ("T\n");
return ;
}
}
if (PointInPolygon(liu,,seg.point1)&&PointInPolygon(liu,,seg.point2))
{
printf ("T\n");
return ;
}
printf ("F\n");
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin>>t;
while(t--) work();
return ;
}

POJ 1410 Intersection 数据错误的更多相关文章

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

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

  2. POJ 1410 Intersection (计算几何)

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 1410 Intersection(计算几何)

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

随机推荐

  1. iOS获取设备型号的方法

    1. [UIDevice currentDevice].model   自己写的看只抓到模拟器和iPhone.暂时不推荐. 2.自己写的找的方法再添加.直接  NSString * deviceMod ...

  2. HDOJ1238(string)

    Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. LVS实战1

    (一).NAT模式:NAT模型:地址转换类型,主要是做地址转换,类似于iptables的DNAT类型,它通过多目标地址转换,来实现负载均衡:特点和要求: 1.LVS(Director)上面需要双网卡: ...

  4. 奇异值分解(SVD)详解

    2012-04-10 17:38 45524人阅读 评论(18) 收藏 举报  分类: 数学之美 版权声明:本文为博主原创文章,未经博主允许不得转载. SVD分解 SVD分解是LSA的数学基础,本文是 ...

  5. WSGI 简介(使用python描述)

    WSGI 简介 背景 Python Web 开发中,服务端程序可以分为两个部分,一是服务器程序,二是应用程序.前者负责把客户端请求接收,整理,后者负责具体的逻辑处理.为了方便应用程序的开发,我们把常用 ...

  6. mysql错误代号大全

    B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: · 错误消息信息列在share/errmsg.txt文件中."%d"和"%s"分别代表编号和字符 ...

  7. Sequence Models 笔记(一)

    1 Recurrent Neural Networks(循环神经网络) 1.1 序列数据 输入或输出其中一个或两个是序列构成.例如语音识别,自然语言处理,音乐生成,感觉分类,dna序列,机器翻译,视频 ...

  8. PopupWindow 从底部弹出窗体

    第一步  : 初始化PopupWindow private void initPop() { if (view == null) { // 照片 view = View.inflate(Registe ...

  9. CountDownLatch分析

    1 什么是CountDownLatch呢? 先看看官网的定义 :一种同步帮助,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成. 现在由我来解释什么是CountDownLatch吧:比如说我 ...

  10. TortoiseSVN 日常操作指南

    TortoiseSVN A Subversion client for Windows Stefan Küng Lübbe Onken Simon Large 2005/01/17 19:09:21 ...