lightoj 1118 Incredible Molecules 圆面积求交,模板
看了kuangbin神的版子,自己的都不敢往上贴了。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <stack>
using namespace std;
const double Pi=acos(-1.0);
const double eps=1e-10;
double add(double a,double b) //有误差的浮点加法
{
if(abs(a + b) < eps * (abs(a) + abs(b))) return 0;
return a + b;
}
struct Point
{
double x,y;
Point(double tx = 0,double ty = 0) : x(tx),y(ty) {}
Point operator + (Point p)
{
return Point(add(x,p.x),add(y,p.y));
}
Point operator - (Point p)
{
return Point(add(x,-p.x),add(y,-p.y));
}
Point operator * (double d)
{
return Point(x * d,y * d);
}
Point operator / (double d)
{
return Point(x / d,y / d);
}
Point Move(double a,double d)//从x正方向开始,逆时针
{
return Point(x + d * cos(a),y + d * sin(a));
}
void Read()
{
scanf("%lf%lf",&x,&y);
}
};
struct Circle
{
Point o;
double r;
Circle(double tx = 0,double ty = 0,double tr = 0) : o(tx,ty),r(tr) {}
void Read()
{
o.Read();
scanf("%lf",&r);
}
void Out()
{
printf("%.8f %.8f %.8f\n",o.x,o.y,r);
}
double Area()
{
return r*r*Pi;
}
};
double x_mult(Point sp, Point ep, Point op)//叉积
{
return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x);
}
double cross(Point a,Point b,Point c)//点积
{
return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
double dist(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double solve(Circle c1,Circle c2)
{
double d=dist(c1.o,c2.o);
if (c1.r+c2.r<d+eps)return 0;
if (d<fabs(c1.r-c2.r)+eps)
{
double r=min(c1.r,c2.r);
return Pi*r*r;
}
double x=(d*d+c1.r*c1.r-c2.r*c2.r)/(2*d);
double t1=acos(x/c1.r);
double t2=acos((d-x)/c2.r);
return c1.r*c1.r*t1+c2.r*c2.r*t2-d*c1.r*sin(t1);
}
int main()
{
int T,ncas=1;
scanf ("%d",&T);
while (T--)
{
Circle c1,c2;
c1.Read();
c2.Read();
printf ("Case %d: %.10f\n",ncas++,solve(c1,c2));
}
return 0;
}