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 & ...
随机推荐
- 多线程操作(循环往listbox中添加数据)
一.先造一个窗体,其中就开始按钮,暂停按钮,以及listbox文本框 二.当点击开始的时候,数据会无限的往listbox中加,为了防止卡住和提升效率,便造了一个新的线程来执行开始操作 namespac ...
- timer的使用
; private void timer1_Tick(object sender, EventArgs e) //定时执行事件 { button1.Text = i.ToString();//显示按钮 ...
- Windows Store APP- C# to get IP Address
using Windows.Networking.Connectivity; public String GetIPString() { String ipString = String.Empty; ...
- 【转】这些编程语言程序员工资最高!Java才第四
原文网址:http://tech.hexun.com/2016-07-18/185009761.html 在众多行业中,程序员属于高薪职业.无论是在国外还是国内,程序员的薪金水平普遍高于其他行业的工作 ...
- linux内核值shmmax问题
问题:(rac安装过程中grid检测) Please run the following script on each node as "root" user to execut ...
- 如何在VMware虚拟机间建立共享磁盘?
在同一台电脑上,有时难免要安装多个虚拟机,存储空间就成了最大的问题,那么如何解决虚拟机的硬盘问题呢,Vmware自带的工具可以很好的解决此问题,下面我们就来看看如何在Vmware虚拟机间建立共享磁盘? ...
- sqlite3使用简介(内含解决sqlite内存的方法)
一.使用流程 要使用sqlite,需要从sqlite官网下载到三个文件,分别为sqlite3.lib,sqlite3.dll,sqlite3.h,然后再在自己的工程中配置好头文件和库文件,同时将dll ...
- 获取当前的 viewController
- (UIViewController *)currentController { UIViewController *result = nil; UIWindow *window = [ ...
- collect my database for test KCF tracker tools
Path Button used to set dir where avi file saves, set path set video size and start record write to ...
- 初学JavaScript(入门一)
javaScript是世界上最流行的脚本语言 在我们的手机.电脑设备上所浏览的所有网页,以及基于HTML5手机App的交互都是通过javaScript驱动的,所以javascript是前端工作的一 ...