首页 > 代码库 > 求两圆相交的面积

求两圆相交的面积

微笑走自己的路,你会发现,在走的过程中你会收获很多

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;

double dis(int x1,int y1,int x2,int y2){
    double len = sqrt((x1-x2+0.0)*(x1-x2+0.0)+(y1-y2+0.0)*(y1-y2+0.0));
    return len;
}

double f(int x1,int y1,int r1,int x2,int y2,int r2){
    double a = dis(x1,y1,x2,y2), b = r1+0.0, c = r2+0.0;
	double cta1 = acos((a * a + b * b - c * c) / 2 / (a * b)),
		cta2 = acos((a * a + c * c - b * b) / 2 / (a * c));
	double s1 = (r1+0.0)*r1*cta1 - (r1+0.0)*r1*sin(cta1)*(a * a + b * b - c * c) / 2 / (a * b);
	double s2 = (r2+0.0)*r2*cta2 - (r2+0.0)*r2*sin(cta2)*(a * a + c * c - b * b) / 2 / (a * c);
	return s1 + s2;
}

int main(){
    int x1,y1,x2,y2,r1,r2;

    while(~scanf("%d%d%d%d%d%d",&x1,&y1,&r1,&x2,&y2,&r2)){
        double ans = f(x1,y1,r1,x2,y2,r2);
        printf("%.4lf\n",ans);
    }

    return 0;
}


求两圆相交的面积