判线段和矩形交+神坑——poj1410
Posted zsben991126
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判线段和矩形交+神坑——poj1410相关的知识,希望对你有一定的参考价值。
坑点:
1.要重新组织一下左上右下的坐标
2.在矩形内也算相交
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; typedef double db; const db eps=1e-6; const db pi=acos(-1); int sign(db k){if (k>eps) return 1; else if (k<-eps) return -1; return 0;} int cmp(db k1,db k2){return sign(k1-k2);} struct point{ db x,y; point(){} point(db x,db y):x(x),y(y){} point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} point operator * (db k1) const{return (point){x*k1,y*k1};} point operator / (db k1) const{return (point){x/k1,y/k1};} }; db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} int intersect(db l1,db r1,db l2,db r2){ if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1; } int checkSS(point k1,point k2,point k3,point k4){ return intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&& sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<=0&& sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<=0; } point k1,k2,k3,k4,k5,k6; int judgein(point k){ if(k.x<=k4.x&&k.x>=k3.x&&k.y<=k3.y&&k.y>=k4.y)return 1; return 0; } int main(){ int t;cin>>t; while(t--){ scanf("%lf%lf",&k1.x,&k1.y);//start scanf("%lf%lf",&k2.x,&k2.y);//end scanf("%lf%lf",&k3.x,&k3.y);//left top scanf("%lf%lf",&k4.x,&k4.y);//right bottom if(k3.x>k4.x)swap(k3.x,k4.x); if(k3.y<k4.y)swap(k3.y,k4.y); k5.x=k3.x;k5.y=k4.y; //left bottom k6.x=k4.x;k6.y=k3.y; //right top if(checkSS(k1,k2,k3,k6) || checkSS(k1,k2,k3,k5) || checkSS(k1,k2,k4,k5) || checkSS(k1,k2,k4,k6) ) puts("T"); else if(judgein(k1) || judgein(k2))puts("T"); else puts("F"); } }
以上是关于判线段和矩形交+神坑——poj1410的主要内容,如果未能解决你的问题,请参考以下文章