hdu5120 Intersection(求两个圆环相交的面积)

本文介绍了一种计算两个相同大小圆环相交面积的方法,通过分解为外圆相交面积、内外圆相交面积及内圆相交面积进行求解,并提供了具体的算法实现。

Intersection

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 206    Accepted Submission(s): 86


Problem Description
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.
 

Input
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.
 

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
 

题意:已经说完了,求两个圆环的相交面积。

题解:如图所示,两圆环相交面积即使外圆相交面积S1减去两倍外圆和内圆相交面积S2加上内圆相交面积S3.

求两圆相交面积可根据余弦定理求出两扇形面积减去四边形面积即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
struct point{
	double x,y;
};
struct circle{ 
	point c;
	double r;
};
double CS(circle a,circle b){    //求两圆相交面积 
	double d=sqrt((a.c.x-b.c.x)*(a.c.x-b.c.x)+(a.c.y-b.c.y)*(a.c.y-b.c.y));
	if (a.r<b.r){
		circle g;
		g=a;
		a=b;
		b=g;
	}
	if (a.r+b.r<=d) return 0; //两圆相离 
	if (a.r-b.r>=d){
		return (pi*b.r*b.r);
	}  //大圆包含小圆 
	double a1=acos((d*d+a.r*a.r-b.r*b.r)/(2*a.r*d));
	double b1=acos((d*d+b.r*b.r-a.r*a.r)/(2*b.r*d));
	double s=a1*a.r*a.r+b1*b.r*b.r;
	double s1=a.r*d*sin(a1);
	return s-s1;
}
int main(){
	int T;
	scanf("%d",&T);
	for (int cas=1;cas<=T;cas++){
		circle a,b;
		scanf("%lf%lf",&a.r,&b.r);
		scanf("%lf%lf",&a.c.x,&a.c.y);
		scanf("%lf%lf",&b.c.x,&b.c.y);
		circle c=a;
		c.r=b.r;
		circle d=b;
		d.r=a.r;
		double s1=CS(c,b);
		double s2=CS(a,b);
		double s3=CS(a,d);
		printf("Case #%d: %f\n",cas,s1-2*s2+s3);
	}
	return 0;
}



HDU(Hangzhou Dianzi University)OJ 中经常涉及到几何计算的问题,其中“判断两条线段是否相交”是一个经典的算法问题。以下是关于如何判断两线段是否相交的基本思路及其实现步骤: ### 判断两条线段相交的核心思想 可以利用向量叉积以及端点位置的关系来确定两条线段是否相交。 #### 具体步骤: 1. **定义基本概念** - 假设两条线段分别为 `AB` 和 `CD`。 - 使用二维平面中的坐标表示各顶点:A(x₁,y₁), B(x₂,y₂),C(x₃,y₃) ,D(x₄,y₄)。 2. **叉积的作用** 叉积可以帮助我们了解两点相对于一条直线的位置关系。 对于三个点 P、Q、R ,我们可以用叉乘 `(Q-P)x(R-P)` 来检测 R 是否在 QP 直线的一侧还是另一侧。 如果结果为正数,则表明顺时针;如果负则逆时针;若等于0则共线。 3. **快速排斥实验** 首先做一个矩形包围盒测试——即检查两个线段所在的最小外接矩形是否有重叠区域。如果没有重叠直接判定为不相交。 4. **跨立试验 (Cross-over Test)** 确认每个线段的两端分别位于另一个线段两侧即可认为它们交叉了。这通过上述提到过的叉积运算完成。 5. **特殊情况处理** 包含但不限于如下的几种情况需要单独讨论: - 完全重合的部分; - 存在一个公共端点但并不完全穿过等边缘状况。 6. **代码框架示例(Pseudo code):** ```python def cross_product(p1,p2,p3): return (p2[0]-p1[0])*(p3[1]-p1[1])-(p2[1]-p1[1])*(p3[0]-p1[0]) def on_segment(p,q,r): if ((q[0] <= max(p[0], r[0])) and (q[0] >= min(p[0], r[0])) and (q[1] <= max(p[1], r[1])) and (q[1] >= min(p[1], r[1]))): return True; return False; def do_segments_intersect(A,B,C,D): # 计算四个方向的叉积值 o1 = cross_product(A, C, B) o2 = cross_product(A, D, B) o3 = cross_product(C, A, D) o4 = cross_product(C, B, D) # 标准情况判断 if(o1 !=o2 && o3!=o4): return True # 特殊情况逐一验证... ``` 7. 最终结合所有条件得出结论。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值