POJ 1410 线段相交模版
来源:互联网 发布:formtalk数据 编辑:程序博客网 时间:2024/05/16 09:33
WA了好多次,有很多小陷阱,第一个是注意输入时的排序,第二注意线段在矩形内部的情况。
题目比较直接
模板抄错,wa了好多次,晕~~~
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const double eps=1e-8;int sgn(double x){ if(fabs(x)<eps) return 0; else return x<0 ? -1:1;}struct point{ double x,y; point(){} point(double _x,double _y) { x=_x; y=_y; }};typedef point vector;vector operator + (vector A,vector B) //向量相加{ return vector(A.x+B.x , A.y+B.y);}vector operator - (point A,point B) //两点相减得到向量{ return vector(A.x-B.x , A.y-B.y);}double operator ^ (vector A,vector B) //叉乘{ return A.x*B.y-A.y*B.x;}double operator * (vector A,vector B) //点乘{ return A.x*B.x+A.y*B.y;}struct line{ point p,q; line() {} line(point _p,point _q) { p=_p; q=_q; }};int segcrossseg(line l1,line l2) //2 规范相交 1 不规范相交 0 不相交{ int d1=sgn((l1.q-l1.p)^(l2.p-l1.p)); int d2=sgn((l1.q-l1.p)^(l2.q-l1.p)); int d3=sgn((l2.q-l2.p)^(l1.p-l2.p)); int d4=sgn((l2.q-l2.p)^(l1.q-l2.p)); //cout<<d1<<endl; //cout<<d2<<endl; //cout<<d3<<endl; //cout<<d4<<endl; if((d1^d2)==-2 && (d3^d4)==-2) return 2; return (d1==0 && sgn((l2.p-l1.p)*(l2.p-l1.q))<=0) || (d2==0 && sgn((l2.q-l1.p)*(l2.q-l1.q))<=0) || (d3==0 && sgn((l1.p-l2.p)*(l1.p-l2.q))<=0) || (d4==0 && sgn((l1.q-l2.p)*(l1.q-l2.q))<=0) ;}int judge(line a,line b,line c,line d,line l){ //cout<<segcrossseg(l,a)<<endl; //cout<<segcrossseg(l,b)<<endl; //cout<<segcrossseg(l,c)<<endl; //cout<<segcrossseg(l,d)<<endl; if(segcrossseg(l,a)!=0) return 1; if(segcrossseg(l,b)!=0) return 1; if(segcrossseg(l,c)!=0) return 1; if(segcrossseg(l,d)!=0) return 1; return 0;}int main(){ //freopen("input.txt","r",stdin); int N; scanf("%d",&N); while(N--) { int ans=0; double x1,x2,x3,x4,y1,y2,y3,y4; double t; line a,b,c,d,l; scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); if(x3>x4) { t=x3;x3=x4;x4=t; } if(y3<y4) { t=y3;y3=y4;y4=t; } //printf("(%lf %lf),(%lf,%lf)\n",x3,y3,x4,y4); if (max(x1,x2)<x4 && max(y1,y2)<y3 && min(x1,x2)>x3 && min(y1,y2)>y4) ans=1; else { l.p=point(x1,y1); l.q=point(x2,y2); a.p=point(x3,y3); a.q=point(x3,y4); b.p=point(x3,y4); b.q=point(x4,y4); c.p=point(x4,y4); c.q=point(x4,y3); d.p=point(x4,y3); d.q=point(x3,y3); ans=judge(a,b,c,d,l); } if(ans) printf("T\n"); else printf("F\n"); }}
0 0
- POJ 1410 线段相交模版
- poj 1410 Intersection 线段相交
- POJ 1410 Intersection(线段相交)
- 贴个线段相交模版
- poj 1410 矩形与线段相交判断
- POJ 1410 Intersection(线段非规范相交)
- poj 1410 线段与矩形相交
- POJ 1410 Intersection --几何,线段相交
- POJ 1410 || Intersection(线段矩形相交
- poj 1410 Intersection(线段相交判断)
- poj 1410 Intersection (线段相交判定)
- POJ 1556 线段相交
- POJ 2653 线段相交
- POJ 1066 线段相交
- poj 1127(线段相交)
- POJ 1066 线段相交
- POJ 2653 线段相交
- POJ 1066 线段相交
- php模拟登陆校园教务网络管理系统
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- linux firefox能正常那个上网,但是在软件中心却不能下载软件
- JavaScript与Objective-C之间的通信
- 服务器端编程的10大性能问题
- POJ 1410 线段相交模版
- sprintf函数的用法
- 1_oracle_11gr2_install_on_centos_x86_64
- js select option对象小结
- MYSQL添加新用户并指定数据库
- java中 HashMap和Hashtable,list、set和map 的区别
- JAVA面试宝典8
- 优化
- hdu5475 An easy problem(线段树+单点更新+区间求积)