您的位置:首页 > 其它

HDU - 5120 Intersection(简单几何)——2014ACM/ICPC亚洲区北京站

2017-06-25 20:53 537 查看
传送门

Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know

.

A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.



Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.

[align=left]Input[/align] The first line contains only one integer T (T ≤ 105), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers xi, yi (0 ≤ xi, yi ≤ 20) indicating the coordinates of the center of each ring.
[align=left]Output[/align] For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.

[align=left]Sample Input[/align]
2

2 3

0 0

0 0

2 3

0 0


题目大意:

给两个一样大的圆环,圆心坐标不同,求两个圆环相交的面积

解题思路:

在纸上画画图发现,就是:

两个大圆相交面积-大圆1与小圆2相交面积-大圆2与小圆1相交面积+两个小圆相交面积

求圆相交面积有模板,一套就好了。

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1);
// 几何误差修正
inline int cmp(double x) {
return x < -eps ? -1 : (x > eps);
}
// 计算x的平方
inline double sqr(double x) {
return x * x;
}
// 开方误差修正
inline double mySqrt(double n) {
return sqrt(max((double)0, n));
}

// 二维点(向量)类
struct Point {
double x, y;
Point() {}
Point(double x, double y): x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
friend Point operator + (const Point& a, const Point& b) {
return Point(a.x + b.x, a.y + b.y);
}
friend Point operator - (const Point& a, const Point& b) {
return Point(a.x - b.x, a.y - b.y);
}
friend bool operator == (const Point& a, const Point& b) {
return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
}
friend Point operator * (const Point& a, const double& b) {
return Point(a.x * b, a.y * b);
}
friend Point operator * (const double& a, const Point& b) {
return Point(a * b.x, a * b.y);
}
friend Point operator / (const Point& a, const double& b) {
return Point(a.x / b, a.y / b);
}
// 返回本向量的长度
double norm() {
return sqrt(sqr(x) + sqr(y));
}
// 返回本向量对应的单位向量
Point unit() {
return Point(x, y) / norm();
}
};
//求两圆相交面积
double intersect(double x1,double y1,double r1,double x2,double y2,double r2){
double s,temp,p,l,ans;
l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if(l>=r1+r2) ans=0;
else if(l<=fabs(r1-r2)){
if(r1<=r2) ans=PI*r1*r1;
else ans=PI*r2*r2;
}
else{
p=(l+r1+r2)/2;
s=2*sqrt(p*(p-l)*(p-r1)*(p-r2));
if(r1>r2){
temp=x1;x1=x2;x2=temp;
temp=y1;y1=y2;y2=temp;
temp=r1;r1=r2;r2=temp;
}
ans=acos((r1*r1+l*l-r2*r2)/(2*r1*l))*r1*r1+acos((r2*r2+l*l-r1*r1)/(2*r2*l))*r2*r2-s;
}
return ans;
}

int main()
{
int T; scanf("%d", &T);
for(int cas=1; cas<=T; cas++){
double r, R; scanf("%lf%lf", &r, &R);
double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
double ans1 = intersect(x1, y1, R, x2, y2, R);
double ans2 = intersect(x1, y1, R, x2, y2, r);
double ans3 = intersect(x1, y1, r, x2, y2, R);
double ans4 = intersect(x1, y1, r, x2, y2, r);
double ans = ans1-ans2-ans3+ans4;
printf("Case #%d: %.6f\n",cas,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: