hdu 5120 Intersection (圆面积交)
程序员文章站
2022-04-02 16:56:12
...
解题思路:画一个图可以很明显的看出答案对应的公式,就是一道圆面积交模板题。
AC代码:
/*
* @Author: wchhlbt
* @Last Modified time: 2017-11-01
*/
//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>
#define inf 0x3f3f3f3f
#define pb push_back
#define AA first
#define BB second
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef pair<int, int> P;
typedef long long ll ;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);
inline int read(){ int num; scanf("%d",&num); return num;}
const int maxn = 1025;
typedef struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
}Vector;
Vector operator + (const Vector & A,const Vector & B) { return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (const Point & A,const Point & B) { return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (const Vector & A,double p) { return Vector(A.x*p,A.y*p);}
Vector operator / (const Vector & A,double p) { return Vector(A.x/p,A.y/p);}
//向量点乘
double Dot(const Vector &A, const Vector &B) { return A.x*B.x+A.y*B.y; }
//求向量的长度或两点距离
double Length(const Vector &A){ return sqrt(Dot(A,A)); }
//二维平面上圆的定义
struct Circle
{
Point c;
double r;
Circle(){};
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) //求此圆上圆心角为a的点
{
return Point(c.x+cos(a)*r , c.y+sin(a)*r);
}
};
//求两个圆相交的公共面积
double calArea(Circle c1, Circle c2)
{
double d =Length(c1.c-c2.c);
double s,s1,s2,s3,angle1,angle2;
if(d>=(c1.r+c2.r))//两圆相离
return 0;
if((c1.r-c2.r)>=d)//两圆内含,c1大
return acos(-1.0)*c2.r*c2.r;
if((c2.r-c1.r)>=d)//两圆内含,c2大
return acos(-1.0)*c1.r*c1.r;
angle1=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
angle2=acos((c2.r*c2.r+d*d-c1.r*c1.r)/(2*c2.r*d));
s1=angle1*c1.r*c1.r;s2=angle2*c2.r*c2.r;
s3=c1.r*d*sin(angle1);
s=s1+s2-s3;
return s;
}
int main()
{
int t = read();
for(int kase = 1; kase<=t; kase++){
int r,R;
scanf("%d%d",&r,&R);
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Circle c1 = Circle(Point(x1,y1),r);
Circle c2 = Circle(Point(x1,y1),R);
Circle c3 = Circle(Point(x2,y2),r);
Circle c4 = Circle(Point(x2,y2),R);
double ans = calArea(c2,c4) - calArea(c1,c4) - calArea(c3,c2) + calArea(c1,c3);
printf("Case #%d: %.6f\n",kase,ans);
}
return 0;
}