The area of the Union of Circles

Source: Internet
Author: User
Tags acos cos sin

#include <stdio.h> #include <math.h> #include <algorithm>using namespace Std;const double eps=1e-8;    Const double Pi=acos ( -1.0); struct circle{double x,y,r; Circle () {} circle (double xx,double yy) {x=xx;y=yy;}};    struct node{//in point +1, which can indicate how many layers of double angle are covered;    int flag; Node () {} node (double A,int b) {angle=a;flag=b;}}; Circle c[1005]; Node p[2005];d ouble ss[100];int n;double Dist (Circle a,circle b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y)) ;} int cmp (Circle a,circle b) {return A.R&LT;B.R;}    void Init () {sort (c,c+n,cmp);    int cnt; for (int i=1,j=0;i<n;i++) {//Remove inner-cut and included circles if (c[i].x==c[j].x && c[i].y==c[j].y | |        C[i].r<eps) continue;        if ((C[I].R-C[J].R) <dist (C[i],c[j]) +eps) c[++j]=c[i];    cnt=j+1; } n=cnt;}    void Getarc (Circle p,circle q,double &a,double &b) {//a is a small corner, B is a large angle double d=dist (P,Q);    Double Tmp1=acos ((D*D+P.R*P.R-Q.R*Q.R)/(2*D*P.R));    Double tmp2=atan2 (q.y-p.y,q.x-p.x);    A=TMP2-TMP1; b=TMP1+TMP2;    if (B&GT;PI) B-=2*pi;    if (A&LT;-PI) A+=2*pi;    printf ("%lf%f\n", A, b); printf ("%f\n", TMP2);}    int Cmp1 (Node A,node b) {if (A.angle!=b.angle) return a.angle<b.angle; else return a.flag>b.flag;} Double Cross (Circle a,circle b) {return a.x*b.y-a.y*b.x;} Double Getarea (double a,double r) {return r*r* (a/2.0);}    void Solve () {double area=0;        for (int i=0;i<n;i++) {int cnt=0,flag=0; for (int j=0;j<n;j++) {if (I==j | |            C[i].r+c[j].r<dist (C[i],c[j]) +eps) continue;//does not intersect directly skips double A, b;            Getarc (C[I],C[J],A,B); P[cnt++]=node (a,1);    P[cnt++]=node (b,-1);//a is a small corner, B is a large angle if (a>b) flag++;//record the leftmost point of the circle is covered how many times} p[cnt++]=node (-pi,flag);        P[cnt++]=node (Pi,-flag);        Sort (P,P+CNT,CMP1);                int s=flag+1;//+1 is because each segment arc is at least c[i] this circle overrides for (int j=1;j<cnt;j++) {if (s==1) {//is covered 1 times and above the area                Circle A, B; A.x=c[i].x+c[i].r*cos (P[j-1].angle); A.y=c[i].y+c[i].r*sin (P[j-1].angle); B.x=c[i].x+c[i].r*cos (P[j].angle);                B.y=c[i].y+c[i].r*sin (P[j].angle);                Double K=p[j].angle-p[j-1].angle;         area+= (K-sin (k)) *c[i].r*c[i].r*0.5;//Bow Area Area+=cross (A, B) *0.5;//area} S+=p[j].flag; }} printf ("%f\n", area);}    int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin); #endif//Online_judge while (scanf ("%d", &n)!=eof) {for (int i=0;i<n;i++) scanf ("%lf%lf%lf", &c[i].x,&am        P;C[I].Y,&AMP;C[I].R);        Init ();    Solve (); } return 0;}

The area of the Union of Circles

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.