HDU5120Intersection(兩圓環相交的面積)這裡寫連結内容
我的:
這個是兩個圓環相交的面積,題意是:給出兩個同心的圓環,他們的小圓大圓的半徑都一樣,隻是坐标不相等,之後要求求兩個圓環的相交的面積,這個要套用利用到兩個圓的相交面積的一個模闆,感覺很流暢。
之後再利用這個關系式:
Area圓環==intersection_area圓環A的大圓&圓環B的大圓-intersection_area圓環A的大圓&圓環B的小圓-intersection_area圓環A的小圓&圓環B的大圓+intersection_area圓環A的小圓&圓環B的小圓圓。
我的:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#define PI acos(-1.0)
using namespace std;
const int maxn=+;
struct Circle
{
double x,y;
double r;
}ca,cb,cc,cd;
double intersection_area(Circle c1,Circle c2)//求兩圓相交面積的函數
{
double d;
double angle1,angle2,s1,s2,s3,s,temp;
d=sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
// cout<<d<<endl;
if(d>=c1.r+c2.r)
return ;
if(c1.r-c2.r>=d)
return PI*c2.r*c2.r;
if(c2.r-c1.r>=d)
return PI*c1.r*c1.r;
angle1=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(*c1.r*d));
angle2=acos((c2.r*c2.r+d*d-c1.r*c1.r)/(*c2.r*d));
s1=c1.r*c1.r*angle1;
s2=c2.r*c2.r*angle2;
s3=c1.r*d*sin(angle1);
// cout<<c1.r<<" "<<d<<' '<<sin(angle1)<<endl;
// cout<<angle1<<" "<<angle2<<" "<<s1<<" "<<s2<<" "<<s3<<endl;
s=s1+s2-s3;
return s;
}
int main()
{
int time;
scanf("%d",&time);
int k=;
while(time--)
{
k++;
scanf("%lf%lf",&ca.r,&cb.r);
scanf("%lf%lf",&ca.x,&ca.y);
scanf("%lf%lf",&cc.x,&cc.y);
cc.r=ca.r;
cd.r=cb.r;
cb.x=ca.x;
cb.y=ca.y;
cd.x=cc.x;
cd.y=cc.y;
double area=intersection_area(cb,cd)-intersection_area(ca,cd)-intersection_area(cb,cc)+intersection_area(ca,cc);
printf("Case #%d: %.6llf\n",k,area);
}
return ;
}