HDU 5120 Intersection(几何模板题)
题意:给定两个圆环,求两个圆环相交的面积。
思路:由于圆心和半径不一样,分了好多种情况,后来发现只要把两个圆相交的函数写好之后就不需要那么复杂了。两个圆相交的面积的模板如下:
double area_of_overlap(point c1, double r1, point c2, double r2)
{
double d = dist(c1, c2);
if (sgn(d - r1 - r2) >= ) return ;
if (sgn(fabs(r1 - r2) - d) >= )
{
double r = min(r1, r2);
return PI * r * r;
}
double x = (d * d + r1 * r1 - r2 * r2) / (2.0 * d);
double t1 = acos(x / r1);
double t2 = acos((d - x) / r2);
return r1 * r1 * t1 + r2 * r2 * t2 - d * r1 * sin(t1);
}
其实是求的这个面积:

完整代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const double eps = 1e-;
const double PI = acos() * ;
int sgn(double x)//精度控制函数
{
if (fabs(x) < eps) return ;
return x > ? : -;
}
//点的定义
struct point {
double x, y;
point (double x = , double y = ):x(x), y(y) {}
};
//获得两点的距离
double dist(const point &a, const point &b)
{
double x = (a.x - b.x) * (a.x - b.x);
double y = (a.y - b.y) * (a.y - b.y);
return sqrt(x + y);
}
//求两个圆相交的面积,如果不相交返回0,c1为第一个圆心坐标,r1为第一个圆的半径,c2为第二个圆心坐标, r2为第二个圆的半径
double area_of_overlap(point c1, double r1, point c2, double r2)
{
double d = dist(c1, c2);
if (sgn(d - r1 - r2) >= ) return ;
if (sgn(fabs(r1 - r2) - d) >= )
{
double r = min(r1, r2);
return PI * r * r;
}
double x = (d * d + r1 * r1 - r2 * r2) / (2.0 * d);
double t1 = acos(x / r1);
double t2 = acos((d - x) / r2);
return r1 * r1 * t1 + r2 * r2 * t2 - d * r1 * sin(t1);
}//下面此题求两个圆环相交的面积
int main()
{
int T, kase = ;
point a, b;
double r, R;
scanf("%d", &T);
while (T--)
{
scanf("%lf %lf", &r, &R);
scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
double tot = area_of_overlap(a, R, b, R);
double s1 = area_of_overlap(a, R, b, r);
double s2 = area_of_overlap(a, r, b, r);
printf("Case #%d: %.6f\n", ++kase, tot - 2.0 * s1 + s2);
}
return ;
}
HDU 5120 Intersection(几何模板题)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- HDU 2544 最短路(模板题)
求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- 【网络流#1】hdu 3549 - 最大流模板题
因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 时间复杂度o(V*E^2) #include& ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
随机推荐
- php获取https下的内容
直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...
- 提供进销存、ERP系统快速开发框架源码 (C#+SQL)
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...
- python运维开发之路第一天
一.python安装及环境变量配置 1.windows7安装python 1)下载地址:https://www.python.org/downloads/windows/ 如下图: 注意:下载,用代理 ...
- 那些年被我坑过的Python——第十章Broker(rabbitMQ/redis)
基于RabbitMQ的direct任务驱动异步RPC程序实现: RPC_dispatcher指令分发器: #!/usr/bin/env python # -*- coding:utf-8 -*- __ ...
- [BZOJ - 2819] Nim 【树链剖分 / DFS序】
题目链接: BZOJ - 2819 题目分析 我们知道,单纯的 Nim 的必胜状态是,各堆石子的数量异或和不为 0 .那么这道题其实就是要求求出树上的两点之间的路径的异或和.要求支持单点修改. 方法一 ...
- Javascript 层次
1. HTML5, Tool, Framework ---------------------------UI: Liger UI, jQuery UI, jQuery Mobile -------- ...
- Robot Motion
Description A robot has been programmed to follow the instructions in its path. Instructions for the ...
- Spark应用程序的运行框架
几个基本概念: (1)job:包含多个task组成的并行计算,往往由action催生. (2)stage:job的调度单位. (3)task:被送到某个executor上的工作单元. (4)taskS ...
- 【转】JAVA程序中Float和Double精度丢失问题
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...