您的位置:首页 > 其它

zoj 3058 Circle and Ring

2011-07-04 18:47 246 查看
求一个圆与一个圆环的相交面积。
只要求得与大圆的面积减去与小圆相交的面积即可。当然要考虑内含,外离,相切的情况。

求圆的相交面积我是根据高中的数学知识算的~高中数学水平过关的就一定能求出来,我不多说。

#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);
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 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;
}
bool c2c_ainb(point a,double r1,point b,double r2)
{
	return xyd(disp2p(a,b),r2 - r1);	//a在b中 包括内切。 
}
double S(double r1,double r2,double l)
{
    double cosaa = (r1*r1 + l*l - r2*r2)/(2*r1*l);        // cos值   
    double fcosaa = acos(cosaa)*2;      //夹角   
    double hulen = fcosaa*r1;           //弧长   
    double Shu = hulen * r1/2;          // 扇形面积   
    double Ssan = r1*r1*sin(fcosaa)/2;  // 三角形面积   
    return (Shu - Ssan);                //扇形面积减去三角形面积  
}
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;
	double l = disp2p(a,b);
	return S(r1,r2,l) + S(r2,r1,l);
}
int main()
{
	double ar,br,bR,ans;

	while( ~scanf("%lf%lf%lf",&a.x,&a.y,&ar) )
	{
		scanf("%lf%lf%lf%lf",&b.x,&b.y,&br,&bR);

		double s1 = c2c_inst_area(a,ar,b,bR);
		double s2 = c2c_inst_area(a,ar,b,br);
		ans = s1 - s2;
		printf("%.3lf\n",fabs(ans));
	}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: