POJ 1410 Intersection 数据错误
题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?)
思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可。
然后题目的数据,(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 数据错误的更多相关文章
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- POJ 1410 Intersection(线段相交&&推断点在矩形内&&坑爹)
Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...
- POJ 1410 Intersection (线段和矩形相交)
题目: Description You are to write a program that has to decide whether a given line segment intersect ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- POJ 1410 Intersection(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...
随机推荐
- css3 tranform perspective属性
perspective 属性用于规定观察点距离元素的距离, 1 观察点距离元素越近,元素变形就越大,灭点距离越近. 2 观察点距离元素越远,元素变形越小,灭点距离也就越远. 比如设置perspecti ...
- AI设计的若干规则阐述
转自:http://www.gameres.com/491742.html 一般来讲,网络游戏的AI历来就是很简单的AI.相比之下,很多单机游戏的AI就要得复杂一些.而笔者并未从事过大型单机游戏的AI ...
- k8s基础 etcd参数
name 节点名称data-dir 指定节点的数据存储目录listen-peer-urls 监听URL,用于与其他节点通讯listen-client-urls 对外提供服务的地址:比如 http:// ...
- Pycharm的远程代码编辑
作为一个从java转到python的程序猿,一直觉得python的远程代码调试能力不如java,远程调试一把需要各种改代码,牵扯到eventlet库的时候,问题就更严重,需要调整eventlet的各种 ...
- day02 秘钥生成,免密访问命令
hadoop免密登陆: 生成秘钥: ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/aut ...
- mysql 简单的sql优化示例[不定时更新]
对于慢sql的分析步骤: 1) desc|explain sql 查看执行计划, 对于type很慢的, 分析是否建立了对应字段的索引 2) 进行排除法, 把子查询抽离出来, 单独执行,定位慢查询是哪个 ...
- TCP/IP以及Socket对象基本
1 OSI七层模型概念介绍 物理层:数据以比特的方式进行传递,典型的设备是集线器.该层主要规定了设备的电压或者端口等等一些列物理层面上的规定 数据链路层:该层数据以帧的方式进行传递,主要是两个 ...
- 并行fp-growth图解(mahout)
FP-growth Apriori算法的一个主要瓶颈在于,为了获得较长的频繁模式,需要生成大量的候选短频繁模式.FP-Growth算法是针对这个瓶颈提出来的全新的一种算法模式.目前,在数据挖掘领域,A ...
- Python学习——输入和输出
(转自:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316434841 ...
- Makefiles
A tutorial by example Compiling your source code files can be tedious, specially when you want to in ...