hdu 5120 Intersection 两环面积交


Intersection

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 2499    Accepted Submission(s): 929


Problem Description
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.
 

Input
The first line contains only one integer T (T ≤ 10 5), 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 x i, y i (0 ≤ x i, y i ≤ 20) indicating the coordinates of the center of each ring.
 

Output
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
 

Source
 

解法:答案=两外圆面积交-外1内2面积交-外2内1面积交+两内圆面积交。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
#define sqr(x)  ((x)*(x))

typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
//const int maxn=    ;

const double PI=acos(-1.0);
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps)  return 0;
    else return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {};
    bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}

    bool operator<(const Point& b)const
    {
        return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
    }
};
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }


double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}

double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}


Vector operator +(Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y); }
Vector operator *(Vector A,double p) {return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); }
Vector operator -(Vector A)  {return  Vector(-A.x,-A.y);}

double Length(Vector A){return sqrt(Dot(A,A));}

double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}
struct Circle
{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r){}
    Point point(double a)
    {
        return Point(c.x+cos(a)*r,c.y+sin(a)*r);
    }
};

double Helen(Point A,Point B,Point C)//海伦公式
{
     Vector AB=B-A;double c= Length(AB);
     Vector AC=C-A;double b= Length(AC);
     Vector BC=C-B;double a= Length(BC);
     double p=(a+b+c)/2;
     return sqrt( p*(p-a)*(p-b)*(p-c) );

}
double angle(Vector v)
{
    return atan2(v.y,v.x);
}

double dis(Point A,Point B)
{
    return sqrt(sqr(A.x-B.x)+sqr(A.y-B.y));
}


double getS(Point c1, double r1, Point c2, double r2)
{
    double d = dis(c1,c2);
    if(dcmp(r1 + r2 - d )<=0 ) return 0;//相离 外切
    if(dcmp(d - fabs(r1 - r2) )<=0  )//内含 内切
    {
        double r = min(r1,r2);
        return PI*r*r;
    }
    double x = (d*d + r1*r1 - r2*r2)/(2*d);//x=rad1*r1
    double rad1 = acos(x / r1);
    double rad2 = acos((d - x)/r2);
    return r1*r1*rad1 + r2*r2*rad2 - d*r1*sin(rad1);
}


Circle a[2],b[2];

double solve()
{
    double S1=getS(a[0].c,a[0].r,a[1].c,a[1].r);
    double S2=getS(a[0].c,a[0].r,b[1].c,b[1].r);
    double S3=getS(a[1].c,a[1].r,b[0].c,b[0].r);
    double S4=getS(b[0].c,b[0].r,b[1].c,b[1].r);
//    cout<<S1<<" "<<S2<<" "<<S3<<" "<<S4<<endl;
    double ans=S1-S2-S3+S4;
    return ans;
}
int main()
{
//   std::ios::sync_with_stdio(false);
   int T,kase=0;cin>>T;
   while(T--)
   {
       scanf("%lf%lf",&b[0].r,&a[0].r);
       b[1].r=b[0].r;
       a[1].r=a[0].r;

//       cin>>b[0].c.x>>b[0].c.y;
       scanf("%lf%lf",&b[0].c.x,&b[0].c.y);
       a[0].c=b[0].c;

//       cin>>b[1].c.x>>b[1].c.y;
       scanf("%lf%lf",&b[1].c.x,&b[1].c.y);
       a[1].c=b[1].c;


       printf("Case #%d: %.6f\n",++kase,solve());
   }

   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值