poj1410 Intersection 线段与矩形相交
来源:互联网 发布:算法第四版中文版 编辑:程序博客网 时间:2024/04/30 09:57
//poj1410
//题目意思:判断线段是否与矩形相交(注:线段在矩形内也是相交)
//解题思路:图形学中的线段裁剪算法,有Cohen-Sutherland,梁友栋-Barsky等,
//这里用容易理解的方法:先判断是否有一个点在矩形内,若都没有再去判断4条边与线段是否相交
//0ms AC 代码如下:
#include<iostream>#include<cstdio>#include<math.h>#define eps 1e-8struct Point{double x,y;};bool zero(double x){return x>0? x<eps: x>-eps;}double xmult(Point p1,Point p2,Point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}int dot_online_in(Point p,Point l1,Point l2)//点在线段上{return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;}int same_side(Point p1,Point p2,Point p3,Point p4){return xmult(p1,p3,p4)*xmult(p2,p3,p4)>eps;}int intersect_in(Point p1,Point p2,Point p3,Point p4)//线段相交{if(!zero(xmult(p1,p2,p3))&&!zero(xmult(p1,p2,p4)))return !same_side(p1,p2,p3,p4)&&!same_side(p3,p4,p1,p2);return dot_online_in(p1,p3,p4)||dot_online_in(p2,p3,p4)||dot_online_in(p3,p1,p2)||dot_online_in(p4,p1,p2);}bool judge(Point p0,Point p1,Point p2){if((p1.x-p0.x)*(p2.x-p0.x)<=0.0 &&(p1.y-p0.y)*(p2.y-p0.y)<=0.0)return true;return false;}int main(){int n;scanf("%d",&n);while(n--){Point start,end,p[4];scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&start.x,&start.y,&end.x,&end.y,&p[0].x,&p[0].y,&p[2].x,&p[2].y);p[1].x=p[0].x;p[1].y=p[2].y;p[3].x=p[2].x;p[3].y=p[0].y;int i;if(judge(start,p[0],p[2])||judge(end,p[0],p[2])){printf("T\n");continue;}else{for(i=0;i<4;i++){if(intersect_in(start,end,p[i],p[(i+1)%4])){printf("T\n");break;}}if(i==4)printf("F\n");}}return 0;}
- poj1410 Intersection 线段与矩形相交
- poj1410判断线段与矩形是否相交(判断线段相交)
- poj1410巧妙变成直线与线段相交
- POJ 1419 Intersection (判线段与矩形相交)
- poj 1410 Intersection(线段与矩形相交)
- POJ 1410 Intersection (判断线段与矩形是否相交)
- POJ 1410 Intersection (判断线段是否与矩形相交)
- POJ 1410 Intersection(判断线段与矩形是否相交)
- UVA 191 || Intersection (判断线段是否与矩形相交
- POJ 1410 Intersection(线段与矩形相交)
- poj 1410 Intersection 【判断线段 与矩形面是否相交】
- POJ 1410 Intersection(判断线段与矩形是否相交)
- Intersection(poj1401线段和矩形相交)
- POJ 1410 || Intersection(线段矩形相交
- 线段 矩形 矩形与矩形 相交
- poj 1410 Intersection(判断线段是否与实心矩形相交)
- 线段是否与矩形相交
- POJ 1410 Intersection 判断矩形和线段相交
- hdu1102 很好的最小生成树克鲁斯卡尔算法
- HDOJ1002
- JQuery Ajax WebService传递参数
- volatile 详解
- 彩虹的七种颜色CMYK和RGB的值是什么
- poj1410 Intersection 线段与矩形相交
- 【语录分享】
- 琉球群岛(zhuan)
- poj1696 Space Ant 点积,叉积,夹角
- Understanding Application Compatibility
- Python常用模块
- 4.0Wifi加载流程,走读代码
- MySQL索引背后的数据结构及算法原理
- xmu1341 共圆四边形