Semi-planar intersection template bzoj2618 Cqoi2006 convex polygon __ Computational geometry

Source: Internet
Author: User

The main effect of the topic:
To two convex polygons, to find the area of intersection.

Topic Analysis:
Ask for a half plane turn.

The code is as follows:

#include <cstdio> #include <algorithm> #include <cmath> using namespace std;
    struct point{double x,y; Point () {} point (double x,double y): x (x), Y (y) {} point operator + (const point &c) const {return point (x+c.x,y+ C.Y);
    Point operator-(const point &c) const {return point (X-C.X,Y-C.Y);}
    Double operator * (const point &c) const {return x*c.y-y*c.x;} Point operator * (const double &c) const {return point (X*c,y*c);}}
B[1000];
    struct line{point p,v;
    Double Alpha;
    BOOL operator < (const line &AMP;C) const{return alpha<c.alpha;}
    Line () {} line (point A,point B):p (a), V (b-a) {alpha=atan2 (v.y,v.x);}
        Point operator ^ (const line &c) const {point tmp=p-c.p;
        Double rate= (c.v*tmp)/(V*C.V);
    return p+v*rate;
}}a[1000000],p[1000000];
int tot,m,n,l,r;
    BOOL Onleft (Point p,line L) {point tmp=p-l.p;
Return l.v*tmp>=0;
   } void Half_plane_intersection () { Sort (a+1,a+1+tot);
        for (int i=1;i<=tot;i++) {while (r-l>=2 &&!onleft (p[r-1]^p[r-2],a[i)) r--;
        if (r-l>=1 && fabs (P[R-1].V*A[I].V) <=0) P[r-1]=onleft (A[i].p,p[r-1]) a[i]:p [r-1];
    else P[r++]=a[i];
    for (;;)
        {if (r-l>=2 &&!onleft (p[r-1]^p[r-2],p[l)) r--;
        else if (r-l>=2 &&!onleft (p[l]^p[l+1],p[r-1)) l++;
    else break;
    int main () {scanf ("%d", &m);
    tot=0;
        for (int j=1;j<=m;j++) {scanf ("%d", &n);
            for (int i=1;i<=n;i++) {scanf ("%lf%lf", &b[i].x,&b[i].y);
        if (i!=1) A[++tot]=line (B[i-1],b[i]);
    } a[++tot]=line (b[n],b[1]);
    } half_plane_intersection ();
    if (r-l<=2) printf ("0.000\n");
        else {int sum=0;
        for (int i=l+1;i<r;i++) b[++sum]=p[i-1]^p[i];
        B[++SUM]=P[R-1]^P[L];
        Double ans=0; for (int i=2;i<=sum;i++) Ans+=b[i-1]*b[i];
        ANS+=B[SUM]*B[1];
    printf ("%.3lf\n", ans/2.0);
return 0; }

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.