1 //segment and rectangle intersect POJ 14102 3 //#include <bits/stdc++.h>4#include <iostream>5#include <cstdio>6#include <cstdlib>7#include <algorithm>8#include <vector>9#include <math.h>Ten using namespacestd; One #defineLL Long Long Atypedef pair<int,int>PII; - Const intINF =0x3f3f3f3f; - ConstLL MOD =100000000LL; the Const intN = the; - #defineCLC (A, B) memset (A,b,sizeof (a)) - Const DoubleEPS = 1e-8; - voidFre () {freopen ("In.txt","R", stdin);} + voidFreout () {freopen ("OUT.txt","W", stdout);} -InlineintRead () {intx=0, f=1;CharCh=getchar (); while(ch>'9'|| ch<'0') {if(ch=='-') f=-1; Ch=getchar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();}returnx*F;} + A intSgnDoublex) { at if(Fabs (x) < EPS)return 0; - if(X <0)return-1; - Else return 1; - } - structpoint{ - Doublex, y; in Point () {} -Point (Double_x,Double_y) { tox = _x;y =_y; + } -Pointoperator-(ConstPoint &b)Const{ the returnPoint (X-b.x,y-b.y); * } $ Double operator^(ConstPoint &b)Const{Panax Notoginseng returnx*b.y-y*b.x; - } the Double operator*(ConstPoint &b)Const{ + returnx*b.x + y*b.y; A } the }; + - structline{ $ Point s,e; $ intInx; - Line () {} - Line (Point _s,point _e) { theS=_s;e=_e; - }Wuyi }; the - //Line Line[n]; Wu BOOLInter (line L1,line L2) { - return AboutMax (l1.s.x,l1.e.x) >= min (l2.s.x,l2.e.x) && $Max (l2.s.x,l2.e.x) >= min (l1.s.x,l1.e.x) && -Max (l1.s.y,l1.e.y) >= min (l2.s.y,l2.e.y) && -Max (l2.s.y,l2.e.y) >= min (l1.s.y,l1.e.y) && -SGN ((l2.s-l1.s) ^ (L1.E-L1.S)) *SGN ((l2.e-l1.s) ^ (L1.E-L1.S)) <=0&& ASGN ((L1.S-L2.S) ^ (L2.E-L2.S)) *SGN ((L1.E-L2.S) ^ (L2.E-L2.S)) <=0; + } the BOOLonseg (Point p,line L) { - return $SGN ((l.s-p) ^ (l.e-p)) = =0&& theSGN ((p.x-l.s.x) * (p.x-l.e.x)) <=0&& theSGN ((P.Y-L.S.Y) * (P.Y-L.E.Y)) <=0; the } the - intInconvexpoly (Point A,point p[],intN) { in for(intI=0; i<n;i++){ the if(SGN (p[i]-a) ^ (p[(i+1)%n]-a) <0)return-1; the Else if(Onseg (A,line (p[i],p[(i+1))) (%n]))return 0; About } the return 1; the } the +Point p[4]; - intMain () { the intT;Bayiscanf"%d",&T); the while(t--){ the DoubleX1,y1,x2,y2; -scanf"%LF%LF%LF%LF",&x1,&y1,&x2,&y2); -Line line=Line (Point (X1,y1), point (X2,y2)); thescanf"%LF%LF%LF%LF",&x1,&y1,&x2,&y2); the if(x1>x2) Swap (X1,X2); the if(y1>y2) swap (y1,y2); thep[0]=Point (x1,y1); -p[1]=Point (x2,y1); thep[2]=Point (x2,y2); thep[3]=Point (x1,y2); the if(Inter (line (p[0],p[1] ) (line)) {94printf"t\n"); the Continue; the } the if(Inter (line (p[1],p[2] ) (line)) {98printf"t\n"); About Continue; - }101 if(Inter (line (p[2],p[3] ) (line)) {102printf"t\n");103 Continue;104 } the if(Inter (line (p[3],p[0] ) (line)) {106printf"t\n");107 Continue;108 }109 if(Inconvexpoly (Line.s,p,4) >=0|| Inconvexpoly (Line.e,p,4) >=0){ theprintf"t\n");111 Continue; the }113 Else theprintf"f\n"); the } the return 0;117}
Segment and rectangle intersect POJ 1410