poj1410(判断线段和矩形是否相交)
题目链接:https://vjudge.net/problem/POJ-1410
题意:判断线段和矩形是否相交。
思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两端点是否在矩形内(因为是矩形,即凸多边形,直接用叉积判断即可,如果是一般的多边形,需要用射线法判断。)
AC code:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std; const double eps=1e-;
const double inf=1e20;
int T,flag; int sgn(double x){
if(abs(x)<eps) return ;
if(x<) return -;
return ;
} struct Point{
double x,y;
Point(){}
Point(double xx,double yy):x(xx),y(yy){}
Point operator + (const Point& b)const{
return Point(x+b.x,y+b.y);
}
Point operator - (const Point& b)const{
return Point(x-b.x,y-b.y);
}
double operator * (const Point& b)const{
return x*b.x+y*b.y;
}
double operator ^ (const Point& b)const{
return x*b.y-b.x*y;
}
//绕原点旋转角度b(弧度值),后x、y的变化
void transXY(double b){
double tx=x,ty=y;
x=tx*cos(b)-ty*sin(b);
y=tx*sin(b)+ty*cos(b);
}
}; struct Line{
Point s,e;
Line(){}
Line(Point ss,Point ee){
s=ss,e=ee;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == )
{
if(sgn((s-b.e)^(b.s-b.e)) == )
return make_pair(,res);//重合
else return make_pair(,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(,res);
}
};
//判断线段相交
bool inter(Line l1,Line l2){
return
max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&
max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&
max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&
max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=&&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=;
} double dis(Point a,Point b){
return sqrt((b-a)*(b-a));
}
//判断点在线段上
bool OnSeg(Point P,Line L){
return
sgn((L.s-P)^(L.e-P))==&&
sgn((P.x-L.s.x)*(P.x-L.e.x))<=&&
sgn((P.y-L.s.y)*(P.y-L.e.y))<=;
}
//判断点在凸多边形内,复杂度O(n)
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<0改为>0)
//点的编号:0~n-1
//返回值:
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inConvexPoly(Point a,Point p[],int n){
for(int i=;i<n;++i)
if(sgn((p[i]-a)^(p[(i+)%n]-a))<) return -;
else if(OnSeg(a,Line(p[i],p[(i+)%n]))) return ;
return ;
}
//判断点在任意多边形内,复杂度O(n)
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inPoly(Point a,Point p[],int n){
int cnt=;
Line ray,side;
ray.s=a;
ray.e.y=a.y;
ray.e.x=-inf;
for(int i=;i<n;++i){
side.s=p[i];
side.e=p[(i+)%n];
if(OnSeg(a,side)) return ;
if(sgn(side.s.y-side.e.y)==) continue;
if(OnSeg(side.s,ray)){
if(sgn(side.s.y-side.e.y)>) ++cnt;
}
else if(OnSeg(side.e,ray)){
if(sgn(side.e.y-side.s.y)>) ++cnt;
}
else if(inter(ray,side)) ++cnt;
}
if(cnt%==) return ;
else return -;
} int main(){
scanf("%d",&T);
double x1,yy1,x2,yy2;
while(T--){
flag=;
scanf("%lf%lf%lf%lf",&x1,&yy1,&x2,&yy2);
Line line=Line(Point(x1,yy1),Point(x2,yy2));
scanf("%lf%lf%lf%lf",&x1,&yy1,&x2,&yy2);
if(x1>x2) swap(x1,x2);
if(yy1>yy2) swap(yy1,yy2);
Point p[];
p[]=Point(x1,yy1);
p[]=Point(x2,yy1);
p[]=Point(x2,yy2);
p[]=Point(x1,yy2);
for(int i=;i<;++i)
if(inter(line,Line(p[i],p[(i+)%]))){
flag=;
break;
}
if(inConvexPoly(line.s,p,)>&&inConvexPoly(line.e,p,)>)
flag=;
if(flag) printf("T\n");
else printf("F\n");
}
return ;
}
poj1410(判断线段和矩形是否相交)的更多相关文章
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- Intersection--poj1410(判断线段与矩形的关系)
http://poj.org/problem?id=1410 题目大意:给你一个线段和矩形的对角两点 如果相交就输出'T' 不想交就是'F' 注意: 1,给的矩形有可能不是左上 右下 所以要先判 ...
- 判断圆和矩形是否相交C - Rectangle and Circle
Description Given a rectangle and a circle in the coordinate system(two edges of the rectangle are p ...
- HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...
- POJ 1410--Intersection(判断线段和矩形相交)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16322 Accepted: 4213 Des ...
- Intersection - POJ 1410(线段与矩形是否相交)
题目大意:给一个线段和一个矩形,判断线段是否和矩形有公共点. 分析:用矩形的四个边当线段判断与所给的线段是否有交点,需要注意的是给的矩形是不标准的,需要自己转换,还需要注意线段有可能在矩形内部. ...
- 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster(判断线段是否和圆相交)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1033 [题意] https://www.zybuluo.com/Jerusalem/n ...
- POJ 3304 /// 判断线段与直线是否相交
题目大意: 询问给定n条线段 是否存在一条直线使得所有线段在直线上的投影存在公共点 这个问题可以转化为 是否存在一条直线与所有的线段同时相交 而枚举直线的问题 因为若存在符合要求的直线 那么必存在穿过 ...
- POJ 1410 判断线段与矩形交点或在矩形内
这个题目要注意的是:给出的矩形坐标不一定是按照左上,右下这个顺序的 #include <iostream> #include <cstdio> #include <cst ...
随机推荐
- 022_STM32中断优先级分组解析
(0)STM32有十六个优先级 (一)STM32分组为:组0-4 (二)分组配置在寄存器SCB->AIRCR中: (三)解析第二点 1. 组0就是4位都用来设置成响应优先级,2^4=16位都是响 ...
- 020_linux驱动之_输入子系统按键应用
(一)分配一个输入子系统结构体 static struct input_dev *buttons_dev; /*分配一个input_dev结构体*/ (二)设置这个输入子系统需要的动作 /* 1. 分 ...
- BZOJ 4025 二分图 LCT维护最大生成树
怎么说呢,我也不知道该咋讲,你就手画一下然后 yy 一下就发现这么做是对的. 为什么我明明都想出来了,却还是讲不出来啊~ #include <cstdio> #include <ve ...
- [Luogu] 软件包管理器
https://www.luogu.org/problemnew/show/P2146 几乎是一个裸题 #include<cstdio> #include<cstring> # ...
- 【luoguSP3267】--莫队,不同数字个数
题意翻译 给出一个长度为n 的数列,a1 a2 ,...an ,有q 个询问,每个询问给出数对(i,j),需要你给出ai ai+1 ,...,aj 这一段中有多少不同的数字 题目 ...
- windows游戏编程X86 32位保护模式下的内存管理概述(二)
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22448323 作者:jadeshu 邮箱: jades ...
- opensuse tumbleweed中安装code
在opensuse上启用snaps并安装visual studio code snaps是一个应用程序,运行在很多流行linux发行版上, 其所有依赖项都打包在一个构建中,并且会自动更新并能优雅地回滚 ...
- Spring事务异常回滚
最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了....... 为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...
- JAVA基础知识|小知识点
1.强烈建议,不使用char类型 那么,到底为什么java里不推荐使用char类型呢?其实,1个java的char字符并不完全等于一个unicode的字符.char采用的UCS-2编码,是一种淘汰的U ...
- uiautomator2 wifi连接手机
[实施方法] 手机和电脑同时连接到同一个wifi上 1.开启远程adb #开启远端adb,这一步需要手机通过USB连接到电脑 adb tcpip 5555 #结果如下:restarting in TC ...