POJ 1410 Intersection(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交
解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和右下的坐标,需要自己去判断下左上点与右下点的坐标。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const double eps = 1e-8;
//判断doubule型的正负或0
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
//构建点,且重载运算符
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
//重载减号 因为在求两个点相减构成一个向量时候会用到
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//这是叉积运算,很重要,不多说
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//判断线段相交
bool inter(Line l1,Line l2)
{
return
//这是2个矩形是否相交
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((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
//求距离
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
} int n;
Line line[4];
Line endli;
double a1,a2,a3,a4,xtop,ytop,xbott,ybott;
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE
SI(n);
rep(t,n)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a1,&a2,&a3,&a4,&xtop,&ytop,&xbott,&ybott);
endli=Line(Point(a1,a2),Point(a3,a4));
//把4条边存进line里
line[0]=Line(Point(xtop,ytop),Point(xtop,ybott));
line[1]=Line(Point(xtop,ytop),Point(xbott,ytop));
line[2]=Line(Point(xtop,ybott),Point(xbott,ybott));
line[3]=Line(Point(xbott,ytop),Point(xbott,ybott));
int fl=0;
//判断两条线是否相交
rep(i,4)
{
if (inter(line[i],endli))
{
fl=1;break;
}
}
//判断是否在矩形内
if (max(a1,a3)<max(xbott,xtop)&&min(a1,a3)>min(xbott,xtop)&&max(a2,a4)<max(ytop,ybott)&&min(a2,a4)>min(ytop,ybott))
{
fl=1;
}
if (fl) puts("T");
else puts("F");
}
return 0;
}
POJ 1410 Intersection(计算几何)的更多相关文章
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- 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
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- POJ 1410 Intersection 数据错误
题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) ...
随机推荐
- U盘
U盘里的.Trashes是什么文件,要怎么去掉?为什么会出现这个文件? 这是苹果电脑的垃圾文件. 1.在苹果电脑上删除文件后,没有清空回收站,就会留下这些文件. 2.可以重新插入Mac, 然后会发现T ...
- UVALive-4329 Ping pong (树状数组)
题目大意:有n个数排成一列,问从中能找出几个三元组(ai,aj,ak)满足i<j<k并且这三个数严格单调. 题目分析:枚举中间的数字aj,如果aj前面有c(j)个数a(j)小,后面有d(j ...
- objective-c new关键字
xxx *a = [xxx new] 等价于 xxx *a = [[xxx alloc]init] ,但如果类的构造函数带参数就不能使用new了. 练习了下<Objective-C 基础教程&g ...
- 论文笔记之:Deep Reinforcement Learning with Double Q-learning
Deep Reinforcement Learning with Double Q-learning Google DeepMind Abstract 主流的 Q-learning 算法过高的估计在特 ...
- javascript零散要点收集
1.this永远指向函数对象的所有者 2.ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 3.pr ...
- dedecms的特性-----不完整
1.前后台分离彻底,连模板引擎都不同 2.多入口,但使用相同的基类--------每个入口都清晰
- 使用支持向量机训练mnist数据
# encoding: utf-8 import numpy as np import matplotlib.pyplot as plt import cPickle import gzip clas ...
- 007. 自定义ListBox的item的宽高, 字体居中
/// <summary> /// 自定义ListBox的item的宽高, 字体居中 /// </summary> /// <param name="sende ...
- 【转载】写runat="server"有什么用
aspx运行时会被编译,其中没有runat="server"属性的html标签会被直接写入response,有runat="server"属性的html标签会转 ...
- 使用面向对象思想处理cookie
实例:使用面向对象思想处理cookie如果读者对cookie 不熟悉,可以在第七章学习它的使用方法,虽然在那里创建了几个通用函数用于cookie 的处理,但这些函数彼此分离,没有体现出是一个整体.联想 ...