Intersection(HDU5120 + 圆交面积)

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5120

题目:

题意:

  求两个圆环相交的面积。

思路:

  两个大圆面积交-2×大圆与小圆面积交+两小圆面积交。

代码实现如下:

 

 1 #include <set>
 2 #include <map>
 3 #include <deque>
 4 #include <ctime>
 5 #include <stack>
 6 #include <cmath>
 7 #include <queue>
 8 #include <string>
 9 #include <cstdio>
10 #include <vector>
11 #include <iomanip>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 typedef long long LL;
18 typedef pair<LL, LL> pll;
19 typedef pair<LL, int> pli;
20 typedef pair<int, int> pii;
21 typedef unsigned long long uLL;
22 
23 #define lson rt<<1
24 #define rson rt<<1|1
25 #define name2str(name)(#name)
26 #define bug printf("**********\n");
27 #define IO ios::sync_with_stdio(false);
28 #define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
29 #define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin);
30 
31 const double eps = 1e-8;
32 const int maxn = 1e5 + 7;
33 const int inf = 0x3f3f3f3f;
34 const double pi = acos(-1.0);
35 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
36 
37 int t, r, R;
38 int x1, x2, yy, y2;
39 
40 double cirinter(int x1,int y1,int r1,int x2,int y2,int r2)//圆交面积公式
41 {
42     double d,s,t,a1,a2,s1,s2,s3;
43     if(r1<r2) swap(r1, r2);
44     d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //两圆心距离
45     if(d>=r1+r2)return 0; //两圆相离
46     else if(d<=(r1-r2)) //两圆内含
47         s=pi*r2*r2;
48     else { //两圆相交
49         a1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));//大圆中扇形圆心角的一半
50         a2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));//小圆中扇形圆心角的一半
51         s1=a1*r1*r1;//大圆中的那个扇形面积
52         s2=a2*r2*r2;//小圆中的那个扇形面积
53         s3=r1*sin(a1)*d;//两圆心与两交点组成的四边形面积
54         s=s1+s2-s3;//圆交面积
55     }
56     return s;
57 }
58 
59 int main() {
60 #ifndef ONLINE_JUDGE
61     FIN;
62 #endif
63     scanf("%d", &t);
64     int icase = 0;
65     while(t--) {
66         scanf("%d%d", &r, &R);
67         scanf("%d%d%d%d", &x1, &yy, &x2, &y2);
68         double ans = cirinter(x1, yy, R, x2, y2, R) - 2 * cirinter(x1, yy, R, x2, y2, r) + cirinter(x1, yy, r, x2, y2, r);
69         printf("Case #%d: %.6f\n", ++icase, ans);
70     }
71     return 0;
72 }

 

转载于:https://www.cnblogs.com/Dillonh/p/9747380.html

  • 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、付费专栏及课程。

余额充值