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(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...
随机推荐
- Python:format()方法
转于:https://blog.csdn.net/zhang89xiao/article/details/53818906 博主:张肖的博客 描述: format的格式 replacement_fie ...
- MY_SQLCode
一.SPC查询 根据日期查询 应用到了随机函数 NEWID()可以随机生成一个列值实现随机抓取记录 CONVERT(varchar(100),列名, 23) AS TestDat ...
- python os.startfile python实现双击运行程序 python监控windows程序 监控进程不在时重新启动
用python监控您的window服务 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://world77.blog.51cto.co ...
- k8s 基础 核心概念
Pod pod是若干相关容器的集合,Pod包含的容器运行在同一台宿主机上,这些容器使用相同的网络命名空间,ip地址和端口,相互之间能通过localhost来发现和通信.另外,这些容器还可共享一块存储空 ...
- shell入门-cut命令
命令:cut 选项:-d:-f 指定第几段由“:(分割符)”分割的段 -c 指定第几个字符 说明:选取命令,选取一段数据中我们想要的,一般是针对每行来分析选取的 [root@wangshaoj ...
- JS 数组的一些方法
1.push() //可以接受任意参数,然后添加到数组的末尾 2.pop()//栈方法,在数组末尾删除一条数据,并且返回这条数据 3.shift()//队列方法,与pop()相似,但是与其相反,在数组 ...
- DNS服务器的配置与管理
安装DNS服务器: 在"服务器管理器"-"角色"-"添加角色"中安装DNS服务器. 选择DNS服务器 点下一步安装,然后安装 固定服务器IP ...
- strust2.2.3版本启动报错struts-plugin.xml:8:162
我用的是struts-2.2.3,开始把全部的jar包都放进去了,可是一直报 信息: Parsing configuration file [struts-plugin.xml] 2011-6-11 ...
- 代码,用c++实现线性链表
#include <iostream> #include <stdio.h> #include <malloc.h> using namespace std; #d ...
- 发起http(s)请求
发起http(s)请求我这里主要列举了3种方式: 一.命令行的方式 二.通过工具 三.通过代码 一.命令行的方式 1. curl curl官网: https://curl.haxx.se/downl ...