博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ——2546
阅读量:6969 次
发布时间:2019-06-27

本文共 2659 字,大约阅读时间需要 8 分钟。

  题目链接:

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

  大意是给出两圆心,及圆的半径,求两个圆相交部分的面积,求解关键是对分好case进行处理

Accepted:

1 #include 
2 #include
3 #include
4 #define Pi 3.1415926 5 6 using namespace std; 7 typedef struct Point 8 { 9 float x, y;10 }Point;11 typedef struct Circle12 {13 Point p;14 float r;15 }Circle;16 double Dis(Point p1, Point p2);17 double CArea( Circle c1, Circle c2);18 int main()19 {20 float x1, y1, r1, x2, y2, r2;21 cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;22 double s = 0;23 Circle c1, c2;24 Point p1, p2;25 if(r1 <= r2)26 {27 p1.x = x1, p1.y = y1, c1.p = p1, c1.r = r1;28 p2.x = x2, p2.y = y2, c2.p = p2, c2.r = r2;29 }30 else31 {32 p1.x = x2, p1.y = y2, c1.p = p1, c1.r = r2;33 p2.x = x1, p2.y = y1, c2.p = p2, c2.r = r1;34 }35 36 s = CArea(c1, c2);37 cout << setiosflags(ios::fixed)<
<< s << endl;38 return 0;39 }40 double CArea( Circle c1, Circle c2)41 {42 double s = 0;43 double x = 0, y = 0, h = 0;44 double alpha = 0, beta = 0;45 double d = Dis(c1.p, c2.p);46 if(d >= (c1.r + c2.r))47 {48 s = 0;49 //cout << "case 1" << endl;50 }51 else if(c2.r >= (c1.r + d))52 {53 s = Pi*c1.r*c1.r;54 //cout << "case 2" << endl;55 }56 else if(d >= sqrt(pow(c2.r, 2) - pow(c1.r, 2)) && d < c2.r + c1.r)57 {58 x = (pow(c2.r, 2) - pow(c1.r, 2) + pow(d, 2)) / (2*d);59 h = sqrt(pow(c2.r, 2) - pow(x, 2));60 alpha = asin(h/c1.r);61 beta = asin(h/c2.r);62 s = alpha*pow(c1.r, 2) + beta*pow(c2.r, 2) - d*h;63 //cout << "case 3" << endl;64 }65 else if(d <= sqrt(pow(c2.r, 2) - pow(c1.r, 2)) && d >= c2.r - c1.r)66 {67 68 y = (pow(c2.r, 2) - pow(c1.r, 2) - pow(d, 2)) /(2*d);69 h = sqrt(pow(c1.r, 2) - pow(y, 2));70 alpha = asin(h/c1.r);71 beta = asin(h/c2.r);72 s = (Pi - alpha)*pow(c1.r, 2) + h*y + beta*pow(c2.r, 2) - h*(d+y);73 //cout << "case 4" << endl;74 }75 return s;76 }77 double Dis(Point p1, Point p2)78 {79 return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));80 }

转载地址:http://lgisl.baihongyu.com/

你可能感兴趣的文章
使用gulp-connect实现web服务器
查看>>
Android APP弱网测试问题和解决分析
查看>>
深入研究EF Core AddDbContext 引起的内存泄露的原因
查看>>
set集合
查看>>
今天不谈技术,说说一些常用的软件~By 逆天
查看>>
Python爬虫入门二之爬虫基础了解
查看>>
java入门概念个人理解之访问修饰符
查看>>
分布式 vs 集群 主从 vs 集群
查看>>
javascript数组操作汇总
查看>>
静态链表
查看>>
Ubuntu 12.04中文输入法的安装
查看>>
[转] 你真的了解回流和重绘吗
查看>>
[转] babel-present-env 与 babel-polyfill 学习总结
查看>>
openstack学习(一)kvm-libvirt
查看>>
使用pytesseract识别简单验证码
查看>>
1103 Integer Factorization
查看>>
Promise 简易实现 - 面试专用
查看>>
PHP —— 读取文件到二维数组
查看>>
Mysql中select的正确姿势
查看>>
iOS block示例
查看>>