热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

IntersectionHDU-5120(求两圆交集面积)

Mattisabigfanoflogodesign.Recentlyhefallsinlovewithlogomadeupbyrings.The

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 这里写图片描述
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.
Input
The first line contains only one integer T (T ≤ 10 5), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r

Each of the following two lines contains two integers x i, y i (0 ≤ x i, y i ≤ 20) indicating the coordinates of the center of each ring.
Output
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.
Sample Input
2
2 3
0 0
0 0
2 3
0 0
5 0
Sample Output
Case #1: 15.707963
Case #2: 2.250778

大致题意:就是告诉你两个圆环的圆心坐标,小圆和大圆的半径,让你求两个相同的圆环的交集的面积。如下图所示
这里写图片描述

思路:所求阴影部分的面积=两个大圆相交的面积+两个小圆相交的面积-2*大圆和小圆相交的面积。

注意精度!pi的值多取几位

代码如下

#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const double pi=3.141592654;
//网上找的一份求两圆交集面积的代码
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<=abs(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;
int n;
int r,R;
int x1,y1,x2,y2;
double ans;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
ans=0;
scanf("%d%d",&r,&R);
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ans+=intersect(x1,y1,R,x2,y2,R)+intersect(x1,y1,r,x2,y2,r);
ans-=2*intersect(x1,y1,R,x2,y2,r);
printf("Case #%d: %.6lf\n",cas,ans);
}
return 0;
}

推荐阅读
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本题探讨了一种字符串变换方法,旨在判断两个给定的字符串是否可以通过特定的字母替换和位置交换操作相互转换。核心在于找到这些变换中的不变量,从而确定转换的可能性。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
author-avatar
Hcl
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有