求两圆相交的面积

一,思想

设两圆圆心距离为d,r1>r2

  1. d>=r1+r2,显然两圆无相交面积
  2. 当r1>=r2+d时,说明圆内含,相交面积为小圆面积:acos(-1)*r2*r2

其余情况如下图

  1. 蓝色面积可以看成
#define pow(x) ((x)*(x))//定义取平方
struct circle
{
	int x,y,r;
} a[N];

double cal(circle a,circle b)
{
	double d=sqrt(pow(a.x-b.x)+pow(a.y-b.y));//先计算圆心距离d
	if(d>=a.r+b.r)return 0;//相离或者相切就返回0
	if(abs(a.r-b.r)>=d)return acos(-1)*pow(min(a.r,b.r));//内含,acos就是arccos()
	double c1=acos((pow(a.r)+pow(d)-pow(b.r))/(2*a.r*d)),c2=acos((pow(b.r)+pow(d)-pow(a.r))/(2*b.r*d));//计算夹角
	return c1*pow(a.r)+c2*pow(b.r)-sin(c1)*a.r*d;
}

 例题:C-平泽唯删圆_第二十届宁波大学程序设计竞赛

思路:

  1. 我们每次可以取一个重叠面积,然后删除重叠的圆中的其中一个。直到删完,显然,我们取了n-1个面积
  2. 如果我们把圆看成点,把面积看成边,就变成了在一个图中,选n-1条边获得最大生成图。
  3. 我们使用kruskal算法即可( 把删除一个圆看成两个圆融合)
  4. 我们如何确保我们取的面积就是上面获取的呢?每次取剩余度数为小于等于1的点即可。
#include <bits/stdc++.h>
using namespace std;
#define ll               long long
#define endl             "\n"
#define int              long long
typedef pair<int, int> pii;
//---------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------//
//double 型memset最大127,最小128
const int INF = 0x3f3f3f3f;         //int型的INF
const ll llINF = 0x3f3f3f3f3f3f3f3f;//ll型的llINF
const int N = 510;

#define pow(x) ((x)*(x))//定义取平方
struct circle
{
	int x,y,r;
} a[N];

double cal(circle a,circle b)
{
	double d=sqrt(pow(a.x-b.x)+pow(a.y-b.y));//先计算圆心距离d
	if(d>=a.r+b.r)return 0;//相离或者相切就返回0
	if(abs(a.r-b.r)>=d)return acos(-1)*pow(min(a.r,b.r));//内含,acos就是arccos()
	double c1=acos((pow(a.r)+pow(d)-pow(b.r))/(2*a.r*d)),c2=acos((pow(b.r)+pow(d)-pow(a.r))/(2*b.r*d));//计算夹角
	return c1*pow(a.r)+c2*pow(b.r)-sin(c1)*a.r*d;
}

struct node
{
	int u,v;
	double dis;
	bool operator<(const node&k)const
	{
		return dis>k.dis;
	}
} b[N*N];
int fa[N];
vector<int>edge[N];

int find(int x)
{
	if(fa[x]!=x)fa[x]=find(fa[x]);
	return fa[x];
}

void mysolve()
{
	int n;
	cin>>n;
	for(int i=1; i<=n; ++i)cin>>a[i].x>>a[i].y>>a[i].r,edge[i].clear();
	priority_queue<int,vector<int>,greater<int>>q;
	vector<bool>vis(n+1);
	vector<int>in(n+1);
	int cnt=0;
	for(int i=1; i<=n; ++i)for(int j=i+1; j<=n; ++j)b[++cnt]= {i,j,cal(a[i],a[j])};//存入每个点之间的重叠面积
	sort(b+1,b+1+cnt);
	for(int i=1; i<=n; ++i)fa[i]=i;
	for(int i=1; i<=cnt; ++i)//贪心每次取最大的建立生成树
		{
			int fx=find(b[i].u),fy=find(b[i].v);
			if(fx==fy)continue;
			++in[b[i].u],++in[b[i].v];
			edge[b[i].u].push_back(b[i].v),edge[b[i].v].push_back(b[i].u);
			fa[fy]=fx;
		}
	for(int i=1; i<=n; ++i)if(in[i]<=1)q.push(i),vis[i]=1;;
	while(!q.empty())
		{
			int u=q.top();
			q.pop();
			cout<<u<<" ";
			for(auto v:edge[u])if(!vis[v]&&--in[v]<=1)q.push(v),vis[v]=1;
		}
	cout<<endl;
}

int32_t main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll t=1;
	cin >> t;
	while (t--)
		{
			mysolve();
		}
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下的C代码来两个相交面积: ``` #include <stdio.h> #include <math.h> double overlap_area(double x1, double y1, double r1, double x2, double y2, double r2) { double d = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); if (d >= r1 + r2) { return 0.0; } else if (d <= fabs(r1 - r2)) { double r = fmin(r1, r2); return M_PI * r * r; } else { double a1 = acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d)); double a2 = acos((r2 * r2 + d * d - r1 * r1) / (2 * r2 * d)); double s1 = r1 * r1 * a1; double s2 = r2 * r2 * a2; double s3 = d * r1 * sin(a1); return s1 + s2 - s3; } } int main() { double x1, y1, r1, x2, y2, r2; scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &r1, &x2, &y2, &r2); double area = overlap_area(x1, y1, r1, x2, y2, r2); printf("%.4lf\n", area); return 0; } ``` 其中,`x1`和`y1`分别代表第一个心的横纵坐标,`r1`为第一个的半径,`x2`和`y2`分别代表第二个心的横纵坐标,`r2`为第二个的半径。函数`overlap_area`返回两个相交面积。 ### 回答2: 要两个相交面积,我们可以使用以下步骤: 1. 首先,需要获得两个心坐标和半径长度。 2. 根据两个心距离和半径长度,可以计算出它们是否相交。 3. 如果两个相交,我们可以进一步计算出相交部分的弧度。 4. 根据弧度的大小,可以计算出相交部分的面积。 以下是具体的示例代码来两个相交面积: ```c #include <stdio.h> #include <math.h> // 定义一个函数来计算两个相交面积 float calculateIntersectionArea(float x1, float y1, float r1, float x2, float y2, float r2) { // 计算两个心之间的距离 float distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); // 判断两个是否相交 if (distance <= r1 + r2) { // 计算出相交部分的弧度 float angle = acos((pow(r1, 2) + pow(distance, 2) - pow(r2, 2)) / (2 * r1 * distance)); // 计算相交部分的面积 float intersectionArea = pow(r1, 2) * (angle - sin(angle)); return intersectionArea; } else { // 如果两个相交,则返回0 return 0; } } int main() { // 输入两个心坐标和半径长度 float x1, y1, r1, x2, y2, r2; printf("请输入第一个心坐标和半径:"); scanf("%f %f %f", &x1, &y1, &r1); printf("请输入第二个心坐标和半径:"); scanf("%f %f %f", &x2, &y2, &r2); // 计算两个相交面积 float intersectionArea = calculateIntersectionArea(x1, y1, r1, x2, y2, r2); // 输出相交面积 printf("两个相交面积为:%.2f\n", intersectionArea); return 0; } ``` 通过上述代码,我们可以输入两个心坐标和半径,然后计算它们相交面积,并最终输出结果。 ### 回答3: 首先,要计算两个相交面积,我们需要知道两个的半径和心之间的距离。 假设第一个的半径为r1,心坐标为(x1, y1);第二个的半径为r2,心坐标为(x2, y2)。 首先,根据两个心之间的距离,我们可以使用勾股定理来计算: d = sqrt((x2-x1)^2 + (y2-y1)^2) 接下来,我们可以使用以下三种情况来计算两个相交面积: 1. 如果两个心之间的距离大于两个半径之和(d > r1 + r2),则两个相交相交面积为0。 2. 如果一个完全包含在另一个内,即一个的半径小于等于另一个的半径,并且两个心之间的距离小于半径差(d < |r1 - r2|),则相交面积为小面积,即π * r1^2(如果r1<r2)或π * r2^2(如果r1>r2)。 3. 如果上述两个条件都不满足,则两个相交。我们可以使用以下公式计算相交面积: area = r1^2 * acos((d^2 + r1^2 - r2^2) / (2 * d * r1)) - sqrt((r1 + r2 - d) * (r1 - r2 + d) * (r2 + d - r1) * (r1 + r2 + d)) / 2 最后,我们可以根据上述情况计算出两个相交面积,并将结果输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值