题目
You are given two circles. Find the area of their intersection.
输入
The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle. The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.
输出
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
Code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<VI> VII;
ll MOD;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}());
const int N = 2e5 + 10;
#define PI acos(-1)
struct Point {
long double x,y,R;
Point(long double x=0,long double y=0,long double R=0):x(x),y(y),R(R) {}
};
//计算两圆心之间的长度
//题目思路:先读取数据,然后计算圆心距,判断两圆的位置关系,最后输出结果
long double long_circle(Point,Point);
int main() {
Point R1,R2;
long double x,y,R;
scanf("%Lf %Lf %Lf",&x,&y,&R);
R1.x=x,R1.y=y,R1.R=R;
scanf("%Lf %Lf %Lf",&x,&y,&R);
R2.x=x,R2.y=y,R2.R=R;
long double l=long_circle(R1,R2);
if(l>=(R1.R+R2.R)) { //相离以及外切的情况
printf("0.000\n");
} else if(l<=fabs(R1.R-R2.R)) { //内切或者内含的情况
if(R1.R<R2.R) {
printf("%.12Lf\n",PI*R1.R*R1.R);
} else {
printf("%.12Lf\n",PI*R2.R*R2.R);
}
} else { //最复杂的相交情况,使用余弦定理计算三角形的面积,因为我们已知两条半径,还有圆心距,这就构成了一个三角形
//先求出圆心角,根据余弦定理有a^2+b^2-c^/2*a*b
long double degree1=2*acos((R1.R*R1.R+l*l-R2.R*R2.R)/(2*R1.R*l));//R1的圆心角
long double degree2=2*acos((R2.R*R2.R+l*l-R1.R*R1.R)/(2*R2.R*l));//R2的圆心角
//得到圆心角之后我们就可以计算三角形的面积和扇形的面积了
//扇形面积公式S=1/2*a*R*R,此处的a是圆心角,弧度制
long double s1=degree1/2*R1.R*R1.R;
long double s2=degree2/2*R2.R*R2.R;
//求三角形面积用面积公式s=1/2*a*b*sinc;
long double t1=R1.R*R1.R*sin(degree1)/2;
long double t2=R2.R*R2.R*sin(degree2)/2;
//最后把扇形面积相加再减去两个三角形就是我我们所求的面积了
long double s=s1+s2-t1-t2;
printf("%.12Lf\n",s);
}
return 0;
}
long double long_circle(Point A,Point B) {
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}