Intersection--poj1410(判断线段与矩形的关系)
http://poj.org/problem?id=1410
题目大意:给你一个线段和矩形的对角两点 如果相交就输出'T' 不想交就是'F'
注意:
1,给的矩形有可能不是左上 右下 所以要先判断的
2,线段在矩形的内部输出T
3,如果交点是矩形的顶点的话 是不相交的
情况有点多 刚开始考虑的太少 wa的我心疼
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream> using namespace std;
#define N 20
const double ESP = 1e-;
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a)) struct Point
{
double x,y;
Point(double x=,double y=):x(x),y(y) {}
Point operator - (const Point &temp)const
{
return Point(x-temp.x,y-temp.y);
}
int operator * (const Point &temp)const
{
double t=(x*temp.y)-(y*temp.x);
if(t>ESP)
return ;
if(fabs(t)<ESP)
return ;
else
return -;
}
} p[N],j; Point line(Point u1,Point u2,Point v1,Point v2)///求交点模板
{
Point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t; return ret;
} int main()
{
int n;
scanf("%d",&n);
while(n--)
{
j.x=j.y=INF;
memset(p,);
double x1,x2,y1,y2;
double a,b,c,d;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
if(a>c)
swap(a,c),swap(b,d);
int u,D;
u=max(b,d);
D=min(b,d);
if(x1>=a && x1<=c && x2>=a && x2<=c && y1>=D && y1<=u && y2>=D && y2<=u)
{
printf("T\n");
continue;
}
///判断线段与矩形的一条边是否相交
p[]=Point(x1,y1);
p[]=Point(x2,y2);
p[]=Point(a,b);
p[]=Point(c,b);
p[]=Point(c,b);
p[]=Point(c,d);
p[]=Point(a,b);
p[]=Point(a,d);
p[]=Point(a,d);
p[]=Point(c,d);
int k,kk;
int flag=;
for(int i=; i<=; i++)
{
k=abs((p[]-p[i*-])*(p[i*]-p[i*-])+(p[]-p[i*-])*(p[i*]-p[i*-]));
kk=abs((p[i*-]-p[])*(p[]-p[])+(p[i*]-p[])*(p[]-p[]));
if(k!= && kk!=)///如果相交
{
if((p[]-p[i*-])*(p[i*]-p[i*-])== && (p[]-p[i*-])*(p[i*]-p[i*-])==)///共线
{
if(i== || i==)
{
if((p[].x>a && p[].x<c)||(p[].x>a && p[].x<c) || (p[].x<=a && p[].x>=c) || (p[].x<=a && p[].x>=c))
{
flag=;
break;
}
}
else
{
if((p[].y>D && p[].y<u)||(p[].y>D && p[].y<u) || (p[].y<=D && p[].y>=u) || (p[].y<=D && p[].y>=u))
{
flag=;
break;
}
}
}
j=line(p[],p[],p[i*],p[i*-]);
if(i==)
if(j.x>a && j.x<c && j.y==b)///如果交点在a和c之间(不包括a c)
{
flag=;
break;
}
if(i==)
{
if(j.x==c && j.y>D && j.y<u)
{
flag=;
break;
}
}
if(i==)
{
if(j.x==a && j.y>D && j.y<u)
{
flag=;
break;
}
}
if(i==)
{
if(j.x>a && j.x<c && j.y==d)
{
flag=;
break;
}
}
}
}
if(flag==)
printf("F\n");
else
printf("T\n");
}
return ;
}
Intersection--poj1410(判断线段与矩形的关系)的更多相关文章
- poj1410(判断线段和矩形是否相交)
题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两 ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- POJ 1410--Intersection(判断线段和矩形相交)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16322 Accepted: 4213 Des ...
- Intersection - POJ 1410(线段与矩形是否相交)
题目大意:给一个线段和一个矩形,判断线段是否和矩形有公共点. 分析:用矩形的四个边当线段判断与所给的线段是否有交点,需要注意的是给的矩形是不标准的,需要自己转换,还需要注意线段有可能在矩形内部. ...
- POJ 1410 判断线段与矩形交点或在矩形内
这个题目要注意的是:给出的矩形坐标不一定是按照左上,右下这个顺序的 #include <iostream> #include <cstdio> #include <cst ...
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1410 Intersection (线段和矩形相交)
题目: Description You are to write a program that has to decide whether a given line segment intersect ...
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
随机推荐
- IOStableviewsectionSet
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (t ...
- 【转载】Hierarchal Temporal Memory (HTM)
最近在看机器学习,看能否根据已有的历史来预测Hardware的故障发生概率.下文是一篇很有意思的文章,转自 http://numenta.org/htm.html. NuPIC是一个开源项目,用来实现 ...
- 【HEVC帧间预测论文】P1.7 Content Based Hierarchical Fast Coding Unit Decision Algorithm
Content Based Hierarchical Fast Coding Unit Decision Algorithm For HEVC <HEVC标准介绍.HEVC帧间预测论文笔记> ...
- ZooKeeper读书笔记
<ZooKeeper读书笔记> 1.Zookeeper是什么?Zookeeper是一个典型的分布式数据一致性的解决方案,分布式应用可以基于它实现诸如数据发布/订阅.负载均衡.命名服务.分布 ...
- Apache的HttpClient的使用
Apache的HttpClient可以被用于从客户端发送HTTP请求到服务器端,其中封装了客户端发送http的get和post请求 使用Apache的HttpClient发送GET和POST请求的步骤 ...
- 职业生涯手记——电视剧剧情O.O
很多电视剧.偶像剧.电影里出现过一些场景,从来没想过狗血剧情是来源于现实.. 直到上周一开始,我慢慢相信了.. 事情是这样的. 我们小组有个组员H,从上周一开始他每天都去公司的座机电话接1~2个电话, ...
- CentOS 7上修改主机名
如何在CentOS 7上修改主机名 在Cent ...
- Chrome浏览器安装React developer tools
1. 到 https://github.com/facebook/react-devtools 下载 react-devtools 2. 进入 react-devtools 目录 运行命令 npm ...
- 基于纯注解的spring开发的介绍
几个核心注解的介绍1.@Configuration它的作用是:将一个java类修饰为==配置文件==,在这个java类进行组件注册1package com.kkb.config; import org ...
- B6. Concurrent 内存模型与线程交互
[概述]