POJ 1410 Intersection
来源:互联网 发布:广电授权的网络机顶盒 编辑:程序博客网 时间:2024/04/26 03:44
注意两个问题:
1.这里的相交是指线段不在矩形的外面,也就是说 线段与矩形有交点 或者线段完全在矩形内部。
2.题目描述有问题, 题目中说是按照 xstart ystart xend yend xleft ytop xright ybottom 的format (格式)给出,
按照题意,也就是说给出的坐标顺序是
起点,终点,左上角,右下角。
但是根据实际给出的数据看出来,应该是
起点,终点,对角顶点。
因此,后面两个数据就不一定是 左上角,右下角 了。
思路:先判断线段所在的直线是否与矩形相交,进一步判断线段是否与矩形相交两种情况:
1.斜率存在,
直线方程:y=k*x+b ,
令s =y-k*x-b , 由s符号可判断点与直线的位置
相交的两种可能:
(1).两个点 位于线段所在直线的两侧的时候,则s[i]与s[j] 异号,乘积sum<0;
(2).其中一个点正好在 线段上,即 其中一个s为0,则 sum=s[i]*s[j] =0,这里斜率有误差,取0.00000001 近似为 0
如果直线与矩形相交,进一步判断线段是否与矩形相交
2.斜率不存在的时候,很好判断了
代码:
#include<iostream>#include<stdio.h>using namespace std;int main(){float xstart,ystart ,xend,yend;float xleft,ytop ,xright,ybottom;float x1,y1 ,x2,y2;//矩形两个对角坐标int n;int i,j;scanf("%d",&n);while(n--){scanf("%f%f%f%f",&xstart,&ystart,&xend,¥d);scanf("%f%f%f%f",&x1,&y1,&x2,&y2);//这里的输入仅仅是两个对角的坐标,哪两个角并不确定xleft=x1<x2?x1:x2;xright=x1>x2?x1:x2;ytop=y1>y2?y1:y2;ybottom=y1<y2?y1:y2;if(xstart!=xend){//斜率存在的时候float k=(yend-ystart)/(xend-xstart);float b=ystart-k*xstart;float s[4];//y=k*x+b ,令s =y-k*x-b ,由s符号可判断点在直线的位置s[0]=ytop-k*xleft-b;s[1]=ytop-k*xright-b;s[2]=ybottom-k*xleft-b;s[3]=ybottom-k*xright-b;//先判断直线是否与矩形相交bool flag=false;//假设直线与矩形不相交for(i=0;i<4;i++)for(j=0;j<4;j++){//枚举两个点的位置关系float sum=s[i]*s[j];//相交的两种可能:if(sum<=0.00000001)//1.两个点 位于线段所在直线的两侧的时候,则s[i]与s[j] 异号,乘积sum<0;flag=true; //2.其中一个点正好在 线段上,即 其中一个s为0,则 sum=s[i]*s[j] =0,这里斜率有误差,取0.00000001 近似为 0 }if(flag==false)printf("F\n");//进一步判断else{//如果线段所在的直线与矩形相交,进一步判断线段是否与矩形相交if(ystart>ytop&¥d>ytop||ystart<ybottom&¥d<ybottom)//线段在矩形上或下,不想交flag=false;else if(xstart<xleft&&xend<xleft||xstart>xright&&xend>xright)//线段在矩形左或右,不想交flag=false;if(flag)printf("T\n");elseprintf("F\n");}}else{//斜率不存在的时候if(ystart>ytop&¥d>ytop||ystart<ybottom&¥d<ybottom)//线段在矩形上或下,不想交printf("F\n");else if(xstart<xleft||xstart>xright)//线段在矩形左或右,不想交printf("F\n");else printf("T\n");}}return 0;}
官方测试数据:
in
68
4 9 11 2 1 1 7 5
11 2 4 9 1 1 7 5
12 12 24 24 19 5 25 17
4 6 15 9 1 1 11 11
19 5 25 17 12 12 24 24
0 18 8 12 1 1 11 11
2 4 4 2 1 1 11 11
-4 9 -11 2 -1 1 -7 5
-11 2 -4 9 -1 1 -7 5
-12 12 -24 24 -19 5 -25 17
-4 6 -15 9 -1 1 -11 11
-19 5 -25 17 -12 12 -24 24
0 18 -8 12 -1 1 -11 11
-2 4 -4 2 -1 1 -11 11
4 -9 11 -2 1 -1 7 -5
11 -2 4 -9 1 -1 7 -5
12 -12 24 -24 19 -5 25 -17
4 -6 15 -9 1 -1 11 -11
19 -5 25 -17 12 -12 24 -24
0 -18 8 -12 1 -1 11 -11
2 -4 4 -2 1 -1 11 -11
-4 -9 -11 -2 -1 -1 -7 -5
-11 -2 -4 -9 -1 -1 -7 -5
-12 -12 -24 -24 -19 -5 -25 -17
-4 -6 -15 -9 -1 -1 -11 -11
-19 -5 -25 -17 -12 -12 -24 -24
0 -18 -8 -12 -1 -1 -11 -11
-2 -4 -4 -2 -1 -1 -11 -11
9 1 9 2 4 3 9 6
9 2 9 1 4 3 9 6
10 3 13 3 4 3 9 6
13 3 10 3 4 3 9 6
10 6 14 6 4 3 9 6
14 6 10 6 4 3 9 6
9 7 9 10 4 3 9 6
9 10 9 7 4 3 9 6
4 7 4 10 4 3 9 6
4 10 4 7 4 3 9 6
0 6 3 6 4 3 9 6
3 6 0 6 4 3 9 6
1 3 3 3 4 3 9 6
3 3 1 3 4 3 9 6
4 0 4 2 4 3 9 6
4 2 4 0 4 3 9 6
5 3 8 5 4 3 9 6
8 5 5 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
6 4 6 5 4 3 9 6
6 5 6 4 4 3 9 6
4 3 9 6 4 3 9 6
9 6 4 3 4 3 9 6
4 3 5 4 4 3 9 6
5 4 4 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
5 3 9 3 4 3 9 6
9 3 5 3 4 3 9 6
4 4 4 5 4 3 9 6
4 5 4 4 4 3 9 6
4 3 4 5 4 3 9 6
4 5 4 3 4 3 9 6
4 3 4 6 4 3 9 6
4 6 4 3 4 3 9 6
9 2 9 5 4 3 9 6
9 5 9 2 4 3 9 6
9 2 9 7 4 3 9 6
9 7 9 2 4 3 9 6
out
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection
- Poj-1410-Intersection
- POJ 1410 Intersection
- poj 1410 Intersection
- poj 1410 Intersection
- POJ 1410 Intersection 笔记
- POJ 1410 Intersection
- Check Soduku Solution
- TCP中在发送的数据的ACK未回来前,能继续发送其他数据包吗?
- oracle的存储过程
- 在论坛中出现的比较难的sql问题:27(字符串拆分、字符串合并、非连续数字的间隔范围、随机返回字符串)
- spring3源码编译学习
- POJ 1410 Intersection
- Sort Colors
- JavaScript函数式编程
- 为什么 我 的myeclipse调试的时候,有时鼠标放在变量上就显示变量值,有时就没有,提示不可以显示
- sicily1198
- 给基础学科唱些赞歌
- A*寻路
- UVA 11796 Dog Distance(点与向量)
- 为什么JavaScript声明变量的时候鼓励加var关键字