【Educational Codeforces Round 2D】【计算几何 圆面积交 模板】Area of Two Circles' Intersection

本文介绍了一种计算两个圆交集面积的方法,并提供了一个C++实现示例。该算法适用于不同位置和大小的圆,包括相离、外切、相交、内切和内含等情形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Area of Two Circles' Intersection
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two circles. Find the area of their intersection.

Input

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.

Output

Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed10 - 6.

Sample test(s)
input
0 0 4
6 0 4
output
7.25298806364175601379
input
0 0 5
11 0 5
output
0.00000000000000000000
#include<stdio.h>
#include<iostream>
#include<iomanip>//这个库含有setprecision(15)的精度设置
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
const long double PI=acos(-1.0);
struct Round
{
	long double x,y;
	long double r;
	long double K(long double x)
	{
		return x*x;
	}
	long double Dis(Round a,Round b)
	{
		return sqrt(K(a.x-b.x)+K(a.y-b.y));
	}
	long double Intersection_area(Round a,Round b)
	{
		long double dis=Dis(a,b);
		if(a.r==0||b.r==0||dis>=a.r+b.r)return 0;
		else if(dis<=fabs(a.r-b.r))return PI*K(min(a.r,b.r));
		else
		{
			long double angA=2*acos( (K(a.r)+K(dis)-K(b.r))/(2*a.r*dis) );
			long double angB=2*acos( (K(b.r)+K(dis)-K(a.r))/(2*b.r*dis) );
			long double areaA=K(a.r)*(angA-sin(angA))/2;
			long double areaB=K(b.r)*(angB-sin(angB))/2;
			return areaA+areaB;
		}
	}
}a,b;
int main()
{
	while(cin>>a.x>>a.y>>a.r>>b.x>>b.y>>b.r)
	{
		cout<<setprecision(25)<<a.Intersection_area(a,b);
	}
    return 0;
}
/*
~两圆关系模板,wkc倾力打造~

类型:两圆之间的关系可能是相离,外切,相交,内切,内含这五种。它们都可以利用圆心距离与半径之间的关系来确定。
设两圆圆心距为dis,两圆半径分别是r0和r1,那么,我们有——
if(dis>r0+r1)then 两圆相离
if(dis==r0+r1)then 两圆外切
if(dis==fabs(r0-r1))then 两圆内切
if(dis<fabs(r0-r1))then 两圆内含
if(dis>fabs(r0-r1)&&dis<r0+r1)then 两圆相切

相离,外切:相交面积为0
内切,内含:相交面积为小圆面积
相交:相交面积可以用扇形面积减去三角形面积求得

特别说明:long double要用cin cout输入输出。用iomanip库中的setprecision(15)输出精度,最高15位。

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值