poj 2546 Circular Area

裸的,求两圆相交面积。。这个以前A过 T T 


我只是为了验证我的模板。。。无视这个吧。。。


这个模板是比较麻烦的。。


#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

struct point { double x,y;};
point a,b;
const double eps = 1e-6;
const double pi = acos(-1.0);
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
double disp2p(point a,point b) 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
bool c2c_inst(point a,double r1,point b,double r2)
{
	if( xy(disp2p(a,b),r1+r2) && dy(disp2p(a,b),fabs(r1-r2)) )
		return true;
	return false;
}
point l2l_inst_p(point u1,point u2,point v1,point v2)
{
	point ans = u1;
	double t = ((u1.x - v1.x)*(v1.y - v2.y) - (u1.y - v1.y)*(v1.x - v2.x))/
				((u1.x - u2.x)*(v1.y - v2.y) - (u1.y - u2.y)*(v1.x - v2.x));
	ans.x += (u2.x - u1.x)*t;
	ans.y += (u2.y - u1.y)*t;
	return ans;
} 
void l2c_inst_p(point c,double r,point l1,point l2,point &p1,point &p2)
{
	point p = c;
	double t;
	p.x += l1.y - l2.y;
	p.y += l2.x - l1.x;
	p = l2l_inst_p(p,c,l1,l2);
	t = sqrt(r*r - disp2p(p,c)*disp2p(p,c))/disp2p(l1,l2);
	p1.x = p.x + (l2.x - l1.x)*t;
	p1.y = p.y + (l2.y - l1.y)*t;
	p2.x = p.x - (l2.x - l1.x)*t;
	p2.y = p.y - (l2.y - l1.y)*t;
}
void c2c_inst_p(point c1,double r1,point c2,double r2,point &p1,point &p2)
{
	point u,v;
	double t;
	t = (1 + (r1*r1 - r2*r2)/disp2p(c1,c2)/disp2p(c1,c2))/2;
	u.x = c1.x + (c2.x - c1.x)*t;
	u.y = c1.y + (c2.y - c1.y)*t;
	v.x = u.x + c1.y - c2.y;
	v.y = u.y - c1.x + c2.x;
	l2c_inst_p(c1,r1,u,v,p1,p2);
}
bool c2c_ainb(point a,double r1,point b,double r2)
{
	return xyd(disp2p(a,b),r2 - r1);	//a在b中 包括内切。 
}
double gongxing_area(point a,point b,double r)
{
	double l = disp2p(a,b);
	if( dd(l,2*r) ) return r*r*pi/2;
	double A = acos((2*r*r - l*l)/(2*r*r));
	return r*r*(A - sin(A))/2;
}
double disp2l(point a,point l1,point l2)
{
	return fabs( crossProduct(a,l1,l2) )/disp2p(l1,l2);
}
double c2c_inst_area(point a,double r1,point b,double r2)
{
	if( c2c_ainb(a,r1,b,r2) )
		return pi*r1*r1;
	if( c2c_ainb(b,r2,a,r1) )
		return pi*r2*r2;
	if( !c2c_inst(a,r1,b,r2) )
		return 0;
	point p1,p2;
	c2c_inst_p(a,r1,b,r2,p1,p2);
	double l = disp2p(a,b);
	if( dyd(max(disp2l(b,p1,p2),disp2l(a,p1,p2)),l) )
		if( dy(disp2l(b,p1,p2),disp2l(a,p1,p2)) )
			return pi*r1*r1 - gongxing_area(p1,p2,r1) + gongxing_area(p1,p2,r2);
		else
			return pi*r2*r2 - gongxing_area(p1,p2,r2) + gongxing_area(p1,p2,r1);
	return gongxing_area(p1,p2,r1) + gongxing_area(p1,p2,r2);
}
int main()
{
	double ar,br;

	while( ~scanf("%lf%lf%lf",&a.x,&a.y,&ar) )
	{
		scanf("%lf%lf%lf",&b.x,&b.y,&br);
		double ans = c2c_inst_area(a,ar,b,br);
		printf("%.3lf\n",fabs(ans));
	}

return 0;
}


你可能感兴趣的:(c)