The problem: Although the data range is too small, O (n*n) algorithm can be too, but I still write for practiced hand half-plane intersection.
Semi-planar Cross:
We stipulate that: a datum point + a vector (essentially a straight line,) even if a half plane, now requires a half-plane intersection, and then do something.
Then you can cross the datum point of each half plane to make a right ray parallel to the X axis, and this ray and the vector represent the angle of the line as its "polar angle".
We can sort all half-planes (line, or called straight lines) with their polar angles as key values,
Then sweep a circle and surround it with a graphic that requires half-plane intersection.
The implementation process may look at the code, there Detail comments.
There are several places in the code that need to be drawn, and the diagram will be appended to the code.
Send a template first:
struct point{double x, y; Point (Double _x,double _y): X (_x), Y (_y) {}point () {}void read () {scanf ("%lf%lf", &x,&y);} Point operator + (const point &a) Const{return Point (X+A.X,Y+A.Y); Point operator-(const point &a) Const{return Point (a.x-x,a.y-y); Point operator * (const double A) Const{return point (x*a,y*a);} P[n];struct line{point u,v;double Ang; Line (Point _u,point _v): U (_u), V (_v) {ang=atan2 (v.y,v.x);} Line () {}bool operator < (const line &a) Const{return Ang<a.ang;} L[n];d ouble Xmul (point A,point b) {return a.x*b.y-a.y*b.x;} inline bool Onleft (point A,line a) {return Xmul (a.v,a.u-a) >eps;} Point Line_intersection [line A,line b] {point u=a.u-b.u;double T=xmul (U,B.V)/xmul (A.V,B.V); return a.u+a.v*t;} int half_planes_intersection (int size) {sort (l+1,l+size+1); int i,l,r; Line q[n];q[l=r=1]=l[1]; Point P[n];for (i=2;i<=size;i++) {while (L<r&&!onleft (P[r-1],l[i])) R--;while (l<r&&!onleft (P[l],l[i])) L++;q[++r]=l[i];if (Fabs (Xmul (Q[R].V,Q[R-1].V)) <eps) {r--;if (OnleFT (L[i].u,q[r])) q[r]=l[i];} if (l<r) p[r-1]=line_intersection (Q[r-1],q[r]);} while (L<r&&!onleft (P[r-1],q[l])) r--;if (r-l<=1) return 0;p[r]=line_intersection (Q[l],q[r]); int m=0; for (i=l;i<=r;i++) P[++m]=p[i];return m;}
Re-send code:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define N 2000#define eps 1e-10using namespace std;int cnt;struct point{double x, y; Point (Double _x,double _y): X (_x), Y (_y) {}point () {}void read () {scanf ("%lf%lf", &x,&y);} Overload Vector Plus, two points between vectors, vector multiply point operator + (const dot &a) Const{return A (X+A.X,Y+A.Y);} Point operator-(const point &a) Const{return Point (a.x-x,a.y-y); Point operator * (const double A) Const{return point (x*a,y*a);} p[n];//P: Record the point struct on a convex polygon line{point u,v;//u represents a datum point, V is a vector double ang;//polar line (points _u,point _v): U (_u), V (_v) {ang=atan2 ( v.y,v.x);} Line () {}//in the case of a polar angle, two straight lines parallel to the semi-planar intersection function, bool, operator < (const linear &a) Const{return Ang<a.ang;} l[n];//records all edges, and the left side of the fetch line is the desired half plane, double Xmul (point A,point b) {return a.x*b.y-a.y*b.x;} Cross product inline bool Onleft (point A,line a) {return Xmul (a.v,a.u-a) >eps;} Determine if point A is on the left side of line A, 1 left 0 right points line_intersection (line a,line B)//Two straight intersection, {point u=a.u-b.u;double T=xmul (U,B.V)/xmul (A.V,B.V); return a.u+a.v*t;//the length of the vector is the original T-times,}int half_planes_intersection (int size) {sort (l+1,l+size+1);// Order by pole angle, int i,l,r; Line q[n];q[l=r=1]=l[1]; Point P[n];for (i=2;i<=size;i++) {//new half plane cut more,//even scrap some of the previous half-plane,//need to be out of the team some elements//These three while the specific deletions see blog mouse painted while (l<r &&!onleft (P[r-1],l[i])) R--;while (L<r&&!onleft (P[l],l[i])) l++;q[++r]=l[i];//because of the order of the platoon, So, in any case, add the half plane into the IF (Fabs (Xmul (Q[R].V,Q[R-1].V)) <eps) {//If the two straight lines (half plane) are parallel, r--;//there must be a scrap/**/if (!onleft (p[r-1],l[ I])) q[r]=l[i];} if (l<r) p[r-1]=line_intersection (q[r],q[r-1]),//for two straight line intersection, that is, to calculate the current more than a convex polygon on the point}while (L<r&&!onleft (p[ R-1],q[l]) r--;if (l+1>=r) return 0;//half-plane intersection without ~~p[r]=line_intersection (Q[l],q[r]);//FINAL intersection 、、、 int m=0;for (i= l;i<=r;i++) p[++m]=p[i];//the point on the convex polygon to copy out, the template, the constant is not very good. Return m;//returns the number of points on a convex polygon. }double asks (int size)//area,, principle I will be on the blog to the mouse painted. {double ret=0;for (int i=1;i<=size;i++) Ret+=xmul (p[i],p[i==size?1:i+1]); return fabs (ret/2.0);} Point po[55];//Temporary record points (non-template content) int main () {//freopen ("test.in", "R", stdin); int I,g,n;scaNF ("%d", &g), while (g--) {//each convex polygon is dotted with several sides, scanf ("%d", &n); for (i=1;i<=n;i++) po[i].read (); L[++cnt]=line (po[1],po[n]-po[1]); for (i=2;i<=n;i++) L[++cnt]=line (Po[i],po[i-1]-po[i]);} function Processing: int num=half_planes_intersection (CNT);p rintf ("%.3lf", asks (num)); return 0;}
School computer A bit of slag,, pictures in the evening after home pits--2014.12.04, if I did not fill, please leave a message remind me thank you.
Deep detail analysis of the semi-planar intersection and algorithm of "BZOJ2618" "Cqoi2006" convex polygon.