Intersection(数学公式求相交圆扇形面积)

题目描述*:

Matt is a big fan of logo design. Recently he falls in love with logo
made up by rings. The following figures are some famous examples you
may know.
在这里插入图片描述

A ring is a 2-D figure bounded by two circles sharing the common
center. The radius for these circles are denoted by r and R (r < R).
For more details, refer to the gray part in the illustration below.
在这里插入图片描述

Matt just designed a new logo consisting of two rings with the same
size in the 2-D plane. For his interests, Matt would like to know the
area of the intersection of these two rings.

输入描述

The first line contains only one integer T (T ≤ 105), which indicates
the number of test cases. For each test case, the first line contains
two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers xi, yi (0 ≤ xi,
yi ≤ 20) indicating the coordinates of the center of each ring.

输出描述

For each test case, output a single line “Case #x: y”, where x is the
case number (starting from 1) and y is the area of intersection
rounded to 6 decimal places.

Sample Input

2
2 3
0 0
0 0
2 3
0 0
5 0

Sample Output:

Case #1: 15.707963
Case #2: 2.250778

具体代码

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
double p=3.14159265358;
double shan(double x1, double y1,double r1, double x2,double y2, double r2)
{
	double d = sqrt((x1-x2)*(x1-x2)*1.0 +1.0* (y1-y2)*(y1-y2));
	if (d >= r1+r2)
		return 0;
	if (r1>r2)
	{
		double t = r1;
		r1 = r2;
		r2 = t;
	}
	if(r2 - r1 >= d)
		return p*r1*r1*1.0;
    double a;
    a=(r1+r2+d)/2;
	double an1=acos((r1*r1*1.0+d*d-r2*r2*1.0)/(2*r1*d));
	double an2=acos((r2*r2*1.0+d*d-r1*r1*1.0)/(2*r2*d));
	return an1*r1*r1*1.0 + an2*r2*r2*1.0 - 2*sqrt(a*(a-r1)*(a-r2)*(a-d));
}
int main()
{
   int n;
   cin>>n;
   int o=1;
   while(n--)
   {
       int  r1,r2;
       scanf("%d%d",&r1,&r2);
       int x1,x2,y1,y2;
       scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
       double k;
       double h;
       h=(y2-y1)*(y2-y1)*0.1+(x2-x1)*(x2-x1)*0.1;
       k=sqrt(h);
           double z,c,v;
           double s,l,m,g;
           z=shan(x1,y1,r1,x2,y2,r1);
           c=shan(x1,y1,r2,x2,y2,r2);
           v=shan(x1,y1,r1,x2,y2,r2);
           printf("Case #%d: %.6f\n",o,c-2*v+z);
           o++;
           continue;
   }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三个相交面积可以采用如下方法: 1. 定义三个心坐标和半径,可以用列表存储,例如: ``` circles = [ (x1, y1, r1), (x2, y2, r2), (x3, y3, r3) ] ``` 2. 计算任意两个的交点坐标,可以采用如下代码: ``` from math import acos, sqrt def circle_intersection(circle1, circle2): x1, y1, r1 = circle1 x2, y2, r2 = circle2 d = sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) if d > r1 + r2: return [] if d < abs(r1 - r2): return [] if d == 0 and r1 == r2: return [] a = (r1 ** 2 - r2 ** 2 + d ** 2) / (2 * d) h = sqrt(r1 ** 2 - a ** 2) x3 = x1 + a * (x2 - x1) / d y3 = y1 + a * (y2 - y1) / d x4_1 = x3 + h * (y2 - y1) / d y4_1 = y3 - h * (x2 - x1) / d x4_2 = x3 - h * (y2 - y1) / d y4_2 = y3 + h * (x2 - x1) / d return [(x4_1, y4_1), (x4_2, y4_2)] ``` 这个函数可以计算两个的交点坐标,返回一个列表,如果两个没有交点,则返回一个空列表。 3. 计算三个的交点坐标,可以采用如下代码: ``` def circles_intersection(circles): points = [] for i in range(len(circles)): for j in range(i + 1, len(circles)): p = circle_intersection(circles[i], circles[j]) if p: points.extend(p) return points ``` 这个函数可以计算三个的交点坐标,并返回一个列表。 4. 计算三个相交面积,可以采用如下代码: ``` def circle_area(xc, yc, r): return r ** 2 * acos(-1) def circles_area(circles): points = circles_intersection(circles) if len(points) < 3: return 0 areas = [] for p in points: d1 = sqrt((p[0] - circles[0][0]) ** 2 + (p[1] - circles[0][1]) ** 2) d2 = sqrt((p[0] - circles[1][0]) ** 2 + (p[1] - circles[1][1]) ** 2) d3 = sqrt((p[0] - circles[2][0]) ** 2 + (p[1] - circles[2][1]) ** 2) if d1 <= circles[0][2] and d2 <= circles[1][2] and d3 <= circles[2][2]: a = circle_area(p[0], p[1], min(d1, d2, d3)) areas.append(a) return sum(areas) ``` 这个函数可以计算三个相交面积,返回一个浮点数。函数先计算出三个的交点,然后遍历每个交点,计算出包含这个交点的三个的半径,然后计算出交点处的扇形面积,最后将所有扇形面积相加即可。 注意,以上代码中的acos函数需要用到math库,因此需要在程序开头加上以下一行代码: ``` from math import acos, sqrt ``` 希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值