"CQ Training Day3" Burning sky

Source: Internet
Author: User
Description

Given a number of triangles, the total area covered by the I triangle is recorded as area (i) region (i), and the output of each of the areas (i) (1<=i<=n) zone (i) (1. Solution

Scan lines.
Sets x that handles the horizontal axis of all vertices and segment intersections first.
Consider the contribution of the graph between X[i] x[i] to x[i+1] x[i+1) to the answer, which can be seen as a trapezoid.
A variable k can be used to record "contribution to area (k) area (k) currently being discussed". You only need to divide the initial input line into two categories: the K+1 and the Out Edge (k-1). Code

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
Cmath> #include <cstdlib> using namespace std;
const int MAXN = 50000+5;

Const double EPS = 1e-8;
    int dcmp (double x) {if (Fabs (x) <eps) return 0; Return x>0?
1:-1;
    } struct point{double x, y; Point () {}-(double x,double y): x (x), Y (y) {} bool operator < (const point P) Const {return dcmp (x-p . x) <0| |
    (DCMP (x-p.x) ==0 && dcmp (Y-P.Y) <0);
    } bool operator = = (const point& p) Const {return dcmp (x-p.x) ==0 && dcmp (y-p.y) ==0;
    } void Init () {scanf ("%lf%lf", &x,&y);}
void put () {cout<< "(" <<x<< "," <<y<< ")";}};
typedef point Vector;
Vector operator + (vector a,vector b) {return vector (A.X+B.X,A.Y+B.Y);}
Vector operator-(vector a,vector b) {return vector (A.X-B.X,A.Y-B.Y);}
Vector operator/(vector v,double t) {return vector (v.x/t,v.y/t);} Vector opErator * (vector v,double t) {return vector (v.x*t,v.y*t);} double Cross (vector a,vector b) {return a.x*b.y-a.y*b.x;}

BOOL Onleft (point A1,point a2,point p) {return cross (P-A1,A2-A1) <0;}
    BOOL Intersect (Point a1,point a2,point b1,point B2) {Double C1 = Cross (A2-A1,B1-A1), C2 = Cross (A2-A1,B2-A1);
    Double C3 = Cross (B2-B1,A1-B1), C4 = Cross (B2-B1,A2-B1); 
Return dcmp (C1) *dcmp (C2) <0 &&dcmp (C3) *dcmp (C4) <0;
    } point intersection (Point A1,point a2,point b1,point b2) {Vector V1 = A2-a1,v2 = B2-B1;
    Double T = Cross (B1-A1,V2)/cross (V1,V2);
return a1+v1*t;
    } struct seg{point a1,a2;
    int type;
        SEG () {} seg (point a1,point a2,int Type): A1 (A1), A2 (A2), type (type) {} BOOL operator < (const SEG s) Const { Return dcmp (a1.y-s.a1.y) ==-1 | |
    (DCMP (A1.Y-S.A1.Y) ==0 && dcmp (A2.Y-S.A2.Y) ==-1);

}}Q[MAXN],A[MAXN];
int N,n,tot;

Double X[MAXN],ANS[MAXN];
    int main () {int i,j,k;
    scanf ("%d", &n); for (i=1;i<=n;i++){Point p[3];
        for (j=0;j<3;j++) P[j].init (), x[++tot] = p[j].x;
        if (Cross (p[0]-p[1],p[0]-p[2]) ==0) continue;
            for (k=2,j=0;j<3;k=j++) {Point A = P[k],b = P[j];
            if (dcmp (a.x-b.x) ==0) continue;
            if (b<a) swap (A, b);
            Point C = p[3-j-k];
            if (Onleft (a,b,c)) a[++n] = Seg (a,b,1);
        else A[++n] = Seg (a,b,-1); }} for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) if (Intersect (A[I].A1,A[I].A2,A[J].A1,A[J].A2)
                ) {Point temp = intersection (A[I].A1,A[I].A2,A[J].A1,A[J].A2);
            X[++tot] = temp.x;
    } sort (X+1,x+1+tot); int t = 0;
    X[0] =-1;
    for (i=1;i<=tot;i++) if (dcmp (x[i]-x[t))!=0) x[++t] = X[i];
    tot = t;
        for (i=1;i<tot;i++) {int top = 0; for (j=1;j<=n;j++) if (A[j].a1.x<=x[i] && a[j].a2.x>=x[i+1]) {Point L = interse Ction (A[j].A1,a[j].a2,point (x[i],-1000), point (x[i],1000));
                Point r = Intersection (A[j].a1,a[j].a2,point (x[i+1],-1000), point (x[i+1],1000));
            Q[++top] = Seg (L,r,a[j].type);
        } sort (q+1,q+1+top);
        k = 0;
            for (j=1;j<top;j++) {k + = Q[j].type;
            Double area = ((Q[J+1].A1.Y-Q[J].A1.Y) + (Q[J+1].A2.Y-Q[J].A2.Y)) * (X[i+1]-x[i]) *0.5;
        Ans[k] + = area;
    }} for (i=1;i<=n;i++) printf ("%.4lf\n", Ans[i]);
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.