POJ 2540 Hotter Colder--semi-flat cross

Source: Internet
Author: User
Tags acos

Test instructions: A (0,0) to (10,10) the rectangle, the target point is uncertain, starting from (0,0), if go to a new point is "hotter", then the meaning is close to the target point, if it is "Colder", then is far away, "same" is the same. To speculate on the area of the possible location of the target point.

Solution: Half-plane cross-water problem. From one point to another point far away, indicating the target point at two points between the perpendicular bisector of the source point closer to the side, that we can get a line each time to cut the plane, or cut to the left, or cut the right side, or all cut, and then a half-plane intersection can be obtained possible area.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#definePi ACOs (-1.0)#defineEPS 1e-8using namespacestd;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {}voidInput () {scanf ("%LF%LF",&x,&y); }};typedef point Vector;structline{point P;    Vector v; Doubleang; Line () {} line (point P, Vector v):p (P), V (v) {ang=atan2 (v.y,v.x);} Point Point (DoubleT) {returnPoint (p.x + t*v.x, p.y + t*v.y); }    BOOL operator< (ConstLine &l)Const{returnAng <L.ang;}};intDCMP (Doublex) {if(x <-eps)return-1; if(X > EPS)return 1; return 0;} Template<classT> T Sqr (t x) {returnX *x;} Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y); }vectoroperator-(vector A, vector B) {returnVector (a.x-b.x, A.Y-b.y); }vectoroperator* (Vector A,DoubleP) {returnVector (A.x*p, a.y*p); }vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, a.y/p); }BOOL operator< (Constpoint& A,Constpoint& b) {returna.x < b.x | | (a.x = = b.x && A.y <b.y); }BOOL operator>= (Constpoint& A,Constpoint& b) {returna.x >= b.x && a.y >=b.y;}BOOL operator<= (Constpoint& A,Constpoint& b) {returna.x <= b.x && a.y <=b.y;}BOOL operator== (Constpoint& A,Constpoint& b) {returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0; }DoubleDot (vector A, vector B) {returna.x*b.x + a.y*b.y;}DoubleLength (Vector A) {returnsqrt (Dot (A, a));}DoubleAngle (vector A, vector B) {returnACOs (Dot (A, B)/Length (a)/Length (B)); }DoubleCross (vector A, vector B) {returna.x*b.y-a.y*b.x;} Vector vectorunit (vector x) {returnX/Length (x);} Vector Normal (vector x) {returnPoint (-X.Y, x.x)/Length (x);}DoubleAngle (Vector v) {returnatan2 (V.Y, v.x);} Point Getlineintersection (line A, line B) {Vector u= A.P-B.P; Doublet = Cross (B.V, u)/Cross (A.V, B.V); returnA.P + a.v*t;}DoubleDisP (Point a,point B) {returnLength (b-A);}DoubleCalcconvexarea (point* p,intN) {//Convex bag Area    DoubleArea =0.0;  for(intI=1; i<n-1; i++) Area+ = Cross (p[i]-p[0],p[i+1]-p[0]); returnFabs (area*0.5);}BOOLOnleft (line L, point P) {returnDCMP (Cross (L.V,P-L.P)) >0; }BOOLCmppolarline (line A,line b) {//linear Polar Angle sorting    returnAngle (A.V) <angle (B.V);}intHalfplaneintersection (line* L,intN, point* Poly) {//Half-plane intersection deposit PolySort (l,l+n,cmppolarline); intFirst,last; Point*p =NewPoint[n]; Line*q =NewLine[n]; Q[first=last=0] = l[0];  for(intI=1; i<n;i++) {         while(First < last &&!) Onleft (l[i],p[last-1])) last--;  while(First < last &&!) Onleft (L[i],p[first]) first++; q[++last] =L[i]; if(DCMP (Cross (Q[LAST].V, q[last-1].V)) = =0) { last--; if(Onleft (Q[last], L[I].P)) q[last] =L[i]; }        if(First < last) p[last-1] = Getlineintersection (q[last-1],q[last]); }     while(First < last &&!) Onleft (q[first],p[last-1])) last--; if(Last-first <=1)return 0;//point or line or unbounded plane, return 0P[last] =getlineintersection (Q[last],q[first]); intm =0;  for(inti=first;i<=last;i++) poly[m++] =P[i]; Delete p;    Delete q; returnm;} Line l[102],tl[103]; Point poly[104];intMain () {intI,j,tot =-1;    Point N,p; Charss[Ten]; P.x= P.Y =0.0; tl[++tot] = line (0,0), Vector (Ten,0)); tl[++tot] = line (Ten,0), Vector (0,Ten)); tl[++tot] = line (Ten,Ten), Vector (-Ten,0)); tl[++tot] = line (0,Ten), Vector (0,-Ten));  while(SCANF ("%lf%lf%s", &AMP;N.X,&AMP;N.Y,SS)! =EOF) {        if(ss[0] =='H') tl[++tot] = line ((n.x+p.x)/2.0, (N.Y+P.Y)/2.0), Vector (Normal (P-n))) ; Else if(ss[0] =='C') tl[++tot] = line ((n.x+p.x)/2.0, (N.Y+P.Y)/2.0), Vector (Normal (np)); Else{tl[++tot] = line ((n.x+p.x)/2.0, (N.Y+P.Y)/2.0), Vector (Normal (P-n))) ; tl[++tot] = line ((n.x+p.x)/2.0, (N.Y+P.Y)/2.0), Vector (Normal (np)); } P=N;  for(i=0; i<=tot;i++) L[i] =Tl[i]; intm = Halfplaneintersection (l,tot+1, poly); if(!m) puts ("0.00"); Elseprintf"%.2f\n", Calcconvexarea (poly,m)); }    return 0;}
View Code

POJ 2540 Hotter Colder--semi-flat cross

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.