1. 程式人生 > >Intersection(HDU5120 + 圓交面積)

Intersection(HDU5120 + 圓交面積)

show bsp hdu clion spa printf 鏈接 pri n)

題目鏈接:

  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 }

Intersection(HDU5120 + 圓交面積)