poj 1410 Intersection(线段与矩形相交)
来源:互联网 发布:java返回值有什么用 编辑:程序博客网 时间:2024/04/30 13:08
Intersection
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8611 Accepted: 2254
Description
You are to write a program that has to decide whether a given line segment intersects a given rectangle.
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.
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
The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
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.
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
For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.
Sample Input
14 9 11 2 1 5 7 1
Sample Output
F
Source
Southwestern European Regional Contest 1995
题目:http://poj.org/problem?id=1410
题意:判断给定的线段与矩形是否相交,线段在矩形内部也算相交
分析:这题还算比较裸的,不过貌似数据中矩形给的并不是左上角,右下角,还有一开始以为是直线与矩形相交,晕,wa了啊
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef double mType;/**表示点或向量*/struct Tpoint{ mType x,y; Tpoint(){} Tpoint(mType _x,mType _y):x(_x),y(_y){}};/**有起点和终点的向量或线段*/struct Tsegment{ Tpoint start,end; Tsegment(){} Tsegment(Tpoint _start,Tpoint _end):start(_start),end(_end){} Tsegment(mType sx,mType sy,mType tx,mType ty):start(sx,sy),end(tx,ty){}};struct Trectangle{ mType l,r,b,t; Trectangle(){} Trectangle(mType _l,mType _r,mType _b,mType _t):l(_l),r(_r),b(_b),t(_t){}};/**生成一个点P到点Q的向量*/Tpoint MakeVector(Tpoint P,Tpoint Q){ return Tpoint(Q.x-P.x,Q.y-P.y);}/**向量P与Q的叉积PQ*/mType CrossProduct(Tpoint P,Tpoint Q){ return P.x*Q.y-P.y*Q.x;}/**向量QP与向量QR的叉积,用来判断向量的拐向* 返回值: >0 向右拐, <0 向右拐,等于零同向或反向*/mType MultiCross(Tpoint P,Tpoint Q,Tpoint R){ return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));}/**判断线段P和线段Q是否相交*/bool IsIntersect(Tsegment P,Tsegment Q){ if(max(P.start.x,P.end.x)<min(Q.start.x,Q.end.x)||max(Q.start.x,Q.end.x)<min(P.start.x,P.end.x)|| max(P.start.y,P.end.y)<min(Q.start.y,Q.end.y)||max(Q.start.y,Q.end.y)<min(P.start.y,P.end.y))return 0; return (MultiCross(P.end,P.start,Q.start)*MultiCross(P.end,P.start,Q.end)<=0&& MultiCross(Q.end,Q.start,P.start)*MultiCross(Q.end,Q.start,P.end)<=0);}mType sx,sy,ex,ey,l,r,t,b;Tsegment P;Trectangle Q;int n;bool IsSegRectInter(Tsegment P,Trectangle Q){ if(Q.l<=P.start.x&&P.start.x<=Q.r&&Q.b<=P.start.y&&P.start.y<=Q.t)return 1; if(Q.l<=P.end.x&&P.end.x<=Q.r&&Q.b<=P.end.y&&P.end.y<=Q.t)return 1; if(IsIntersect(P,Tsegment(Q.l,Q.t,Q.r,Q.b)))return 1; if(IsIntersect(P,Tsegment(Q.l,Q.b,Q.r,Q.t)))return 1; return 0;}int main(){ while(~scanf("%d",&n)) while(n--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&sx,&sy,&ex,&ey,&l,&t,&r,&b); if(t<b)swap(t,b); if(r<l)swap(r,l); P=Tsegment(sx,sy,ex,ey); Q=Trectangle(l,r,b,t); puts(IsSegRectInter(P,Q)?"T":"F"); } return 0;}
- poj 1410 Intersection(线段与矩形相交)
- POJ 1410 Intersection(线段与矩形相交)
- POJ 1410 Intersection(判断线段与矩形是否相交)
- POJ 1410 Intersection (判断线段与矩形是否相交)
- POJ 1410 Intersection (判断线段是否与矩形相交)
- POJ 1410 Intersection(判断线段与矩形是否相交)
- poj 1410 Intersection 【判断线段 与矩形面是否相交】
- POJ 1419 Intersection (判线段与矩形相交)
- POJ 1410 || Intersection(线段矩形相交
- POJ 1410 Intersection(判断线段和矩形是否相交)
- poj 1410 Intersection(判断线段是否与实心矩形相交)
- POJ 1410 Intersection 判断矩形和线段相交
- poj1410 Intersection 线段与矩形相交
- poj 1410 Intersection (线段相交判定)
- poj 1410 Intersection (判矩形和线段相交。。细节多)
- poj 1410 Intersection(判矩形和线段相交。。细节多)
- POJ 1410 Intersection(线段相交&&判断点在矩形内&&坑爹)
- poj 1410 矩形与线段相交判断
- 用Forall与bulk collect快速复制表数据
- ubuntu12.04下nfs安装、配置、测试
- 移动开发四国语言概览一:背景
- vim小全
- 开发网站
- poj 1410 Intersection(线段与矩形相交)
- UVA12396/HDU4196 Remoteland 素数拆分+快速求幂
- printk()调试信息分级显示脚本的编写方法
- Python基础教程——2列表和元组
- DAO350.dl给我怎样的小收获
- Android 筆記-Linux Kernel SMP (Symmetric Multi-Processors) 開機流程解析 Part(4) Linux 多核心啟動流程-kthreadd 與相關的
- 进入nano-X方法
- ubuntu 下 vlc ,smplayer 播放电影时字幕乱码解决方法
- 子线程向主线程传值,主线程向子线程传值