Test instructions: In a square area, draw a few lines and ask the last remaining area in the line.
Problem: A semi-planar naked question.
Algorithm: Sorting increment method for semi-planar intersection, complexity O (NLOGN) (excerpt from "ACM-ICPC Programming series Computational Geometry and Application")
Step1: Sort all the half planes according to the polar angle, and the sorting process also takes the parallel half-plane to the weight
Step2: Use a double-ended queue deque to join the smallest two half-plane of the polar angle
Step3: The scanning process considers a new semi-intersection every time:
A:while deque top two half-plane intersections outside the current half-plane: Delete the half plane at the top of the deque
B:while Deque the intersection of two half planes on the bottom of the current half plane: Delete the half plane at the bottom of the deque
C: Add the current half plane to the top of the deque
STEP4: Delete the extra half-plane that extends at both ends of the deque:
A:while deque top two half-plane intersections at the bottom half plane: Delete the half plane at the top of the deque
B:while Deque the intersection of two half planes on the bottom of the top half plane: Delete the half plane at the bottom of the deque
STEP5: The intersection of the adjacent half-plane in the deque is obtained sequentially, and the convex polygon produced by N and half plane
Code (Rujia's template)
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Cmath> #define N 50005 using namespace std;
const double eps=1e-8;
int dcmp (double x) {if (x<eps&&x>-eps) return 0;
Return (x>0)? 1:-1;
} struct Point {double x, y;
Point (double x=0,double y=0) {x=x,y=y;
}
};
struct line {point P;
Point V;
Double ang;
Line (Point P=point (0,0), point V=point (0,0)) {p=p,v=v;
Ang=atan2 (v.y,v.x);
} BOOL operator < (const line &a) Const {return ang<a.ang;
}
};
Point operator + (point A,point b) {return point (A.X+B.X,A.Y+B.Y);}
Point operator-(point A,point b) {return point (A.X-B.X,A.Y-B.Y);}
Point operator * (Point a,double b) {return point (a.x*b,a.y*b);}
int n,l,r,m,cnt;
Double ans;
Line L[n],q[n];
Point P[n],poly[n];
Double Cross (point A,point b) {return a.x*b.y-a.y*b.x;}
Point GLI (Point p,point v,point q,point W) { Point U=p-q;
Double T=cross (w,u)/cross (V,W);
return p+v*t;
} bool Onleft (line M,point P) {point w=p-m.p;
Return dcmp (Cross (m.v,w)) >0;
} void Halfp () {sort (l+1,l+n+1);
cnt=0;
Q[L=R=1]=L[1]; for (int i=2;i<=n;++i) {while (l<r&&!
Onleft (l[i],p[r-1])--r; while (l<r&&!
Onleft (L[i],p[l]) ++l;
Q[++r]=l[i];
if (dcmp (Q[R].V,Q[R-1].V)) ==0) {--r;
if (Onleft (Q[R],L[I].P)) q[r]=l[i];
} if (L<r) P[r-1]=gli (Q[R-1].P,Q[R-1].V,Q[R].P,Q[R].V); } while (l<r&&!
Onleft (q[l],p[r-1])--r;
if (r-l<=1) return;
P[r]=gli (Q[R].P,Q[R].V,Q[L].P,Q[L].V);
for (int i=l;i<=r;++i) poly[++cnt]=p[i];
} double Area () {double ans=0;
for (int i=2;i<cnt;++i) Ans+=cross (poly[i]-poly[1],poly[i+1]-poly[1]);
Return fabs (ANS)/2;
} int main () {scanf ("%d", &n); for (int i=1;i≪=n;++i) {point p,q;
Double a,b,c,d;
scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);
P=point (A, b);
Q=point (C,D);
L[i]=line (P,Q-P);
} l[++n]=line (Point (0,0), point (10000,0));
L[++n]=line (Point (10000, 0), point (0, 10000));
L[++n]=line (Point (10000,10000), point ( -10000,0));
L[++n]=line (Point (0, 10000), point (0,-10000));
HALFP ();
printf ("%.1lf\n", Area ());
return 0; }