POJ 1410 Intersection(判断线段交和点在矩形内)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9996 | Accepted: 2632 |
Description
An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
Figure 1: Line segment does not intersect rectangle
The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.
Input
xstart ystart xend yend xleft ytop xright ybottom
where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.
Output
Sample Input
1
4 9 11 2 1 5 7 1
Sample Output
F
Source
/************************************************************
* Author : kuangbin
* Email : kuangbin2009@126.com
* Last modified : 2013-07-15 10:14
* Filename : POJ1410Intersection.cpp
* Description :
* *********************************************************/ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
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;
}
//绕原点旋转角度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 _s,Point _e)
{
s = _s;e = _e;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为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((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= ;
} //判断点在线段上
//判断点在线段上
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)) <= ;
}
//判断点在凸多边形内
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<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 ;
}
//判断点在任意多边形内
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inPoly(Point p,Point poly[],int n)
{
int cnt;
Line ray,side;
cnt = ;
ray.s = p;
ray.e.y = p.y;
ray.e.x = -100000000000.0;//-INF,注意取值防止越界 for(int i = ;i < n;i++)
{
side.s = poly[i];
side.e = poly[(i+)%n]; if(OnSeg(p,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()
{
int T;
double x1,y1,x2,y2;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line line = Line(Point(x1,y1),Point(x2,y2));
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
if(x1 > x2)swap(x1,x2);
if(y1 > y2)swap(y1,y2);
Point p[];
p[] = Point(x1,y1);
p[] = Point(x2,y1);
p[] = Point(x2,y2);
p[] = Point(x1,y2);
if(inter(line,Line(p[],p[])))
{
printf("T\n");
continue;
}
if(inter(line,Line(p[],p[])))
{
printf("T\n");
continue;
}
if(inter(line,Line(p[],p[])))
{
printf("T\n");
continue;
}
if(inter(line,Line(p[],p[])))
{
printf("T\n");
continue;
}
if(inConvexPoly(line.s,p,) >= || inConvexPoly(line.e,p,) >= )
{
printf("T\n");
continue;
}
printf("F\n");
}
return ;
}
POJ 1410 Intersection(判断线段交和点在矩形内)的更多相关文章
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- [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(线段相交&&推断点在矩形内&&坑爹)
Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- poj 1410 Intersection 线段相交
题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...
随机推荐
- laravel中的命名公约规范及relation N+1问题
User: model ; users: 表名: user_id 键值 relation: public function tasks(){return $this->belongsToMa ...
- Unable to locate package update
碰到这个问题后找到这个帖子就转了过来 当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是: ...
- EF CodeFirst-----简单demo示例
关于EF CodeFirst的文章院子里有很多的学习资料,但大多数都是一些讲Model通过特性或是Fluent API与数据库之间形成映射的关系,看了相关的文章之后,Model如何映射到数据还是有些迷 ...
- 【转】 ARM Linux 3.x的设备树(Device Tree)
1. ARM Device Tree起源 http://blog.csdn.net/21cnbao/article/details/8457546 Linus Torvalds在2011年3月1 ...
- HDU 1496 Train Problem I 火车问题1(桟,水)
题意: 给出两个串,串中的数字i 代表编号为i的火车进入车站的顺序,车站如桟一样,先进后出.第二个串是火车出站的顺序,问若按照第一个串那样进站,是否有可能如第二个串一样的出站顺序?火车顶多9辆,即1- ...
- WWDC 2015 - 概记
WWDC 2015已经过去快一个月了,今年似乎没有像去年那样变化巨大,一切都在慢慢演进,iOS.Mac OS.watchOS都变得越来越好. 新的三大平台的发布,iOS 9/Mac OS EL Cap ...
- 20160201.CCPP体系详解(0011天)
内容概要:C语言基本数据类型及运算题库(含答案) 第二章 基本数据类型及运算 一.选择题 1. 若以下选项中的变量已正确定义,则正确的赋值语句是[C]. A) x1=26.8%3; B) 1+2=x2 ...
- python练习程序(c100经典例8)
题目: 输出9*9口诀. for i in range(1,10): for j in range(1,i+1): print str(j)+"*"+str(i)+"=& ...
- In App Purchase翻译
一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品的信息,并将它们显示出来供用户购买.当用户需要购 ...
- yii 打印sql
$query = TableModel::find()->where([‘xxx’=>xxx]); var_dump($query->prepare(\Yii::$app->d ...