poj2546—Circular Area(两圆相交面积)

题目链接: 传送门

Circular Area
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5999 Accepted: 2336

Description

Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point.

Input

In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.

Output

The output file must contain single real number - the area.

Sample Input

20.0 30.0 15.0 40.0 30.0 30.0

Sample Output

608.366


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int N = 2*1e6+100;
const int INF = 0x3fffffff;
const int mod = 1e9+7;
const double eps = 1e-8;
const double Pi = acos(-1.0);

struct Circle{
	double x,y,r;
};

double insectArea(Circle a,Circle b)
{
	double dis = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

	double r = min(a.r,b.r);

	if(a.r+b.r <= dis)
		return 0.0;         //相离
	else if(fabs(a.r-b.r) >= dis)
		return Pi*r*r;      //内含
	else{
		double ang1 = acos((a.r*a.r+dis*dis-b.r*b.r)/2.0/a.r/dis);  
		double ang2 = acos((b.r*b.r+dis*dis-a.r*a.r)/2.0/b.r/dis);  
		double ret = ang1*a.r*a.r+ang2*b.r*b.r-dis*a.r*sin(ang1);  
		return ret;        //相交
	}
}

int main()
{
	Circle c1,c2;
	while(~scanf("%lf%lf%lf%lf%lf%lf",&c1.x,&c1.y,&c1.r,&c2.x,&c2.y,&c2.r)){
		printf("%.3lf\n",insectArea(c1,c2));
	}
	return 0;
}



一直不知道下面的code为啥错了,望大佬指出:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int N = 2*1e6+100;
const int INF = 0x3fffffff;
const int mod = 1e9+7;
const double Pi = acos(-1.0);

struct Circle{
	double x,y,r;
};

double insectArea(Circle a,Circle b)
{
	double dis = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

	double r = min(a.r,b.r);

	if(a.r+b.r <= dis)
		return 0.0;         //相离
	else if(fabs(a.r-b.r) >= dis)
		return Pi*r*r;      //内含
	else{
		double o1 = 2.0*acos((b.r*b.r+dis*dis-a.r*a.r)/(2.0*b.r*dis));
		double s1 = o1*b.r*b.r/2.0;
		s1 = s1-b.r*b.r*sin(o1)/2.0;

		double o2 = 2.0*acos((a.r*a.r+dis*dis-b.r*b.r)/(2.0*a.r*dis));
		double s2 = o2*a.r*a.r/2.0;
		s2 = s2-a.r*a.r*sin(o2)/2.0;

		return s1+s2;     //相交
	}
}

int main()
{
	Circle c1,c2;
	while(~scanf("%lf%lf%lf%lf%lf%lf",&c1.x,&c1.y,&c1.r,&c2.x,&c2.y,&c2.r)){
		printf("%.3lf\n",insectArea(c1,c2));
	}
	return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值