求两圆环相交部分的面积

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 ≤ 10 5), 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 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


这道题就是告诉两个圆环,阴影部分为圆环的实体,要求两个圆环实体部分相交的面积,画出图后发现(用A,a记第一个圆环, B,b记第二个圆环, area函数为求两圆的

相交面积),area(A, B) - area(A, b)- area(B, a) + area(a, b)(因为多减了两小圆相交的面积,所以后面要再加上),第一次提交时用的c++ 的输入输出流输

入数据,结果超时,改为scanf()函数后就过了。。


#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

const double PI = acos(-1);

// 圆的结构 
struct circle {
       double x; // 圆心横坐标 
       double y; // 圆心纵坐标 
       double r; // 半径 
};

// 计算圆心距 
double dist(circle a, circle b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

// 计算两相交圆的面积 
double area(circle a, circle b) {

       if((dist(a, b)+min(a.r,b.r))<=max(a.r,b.r)) // 内含或重合 
        { 
            if(a.r<b.r) 
                 return PI*a.r*a.r;
            else
                 return PI*b.r*b.r;
        }
        else if(dist(a, b)>=(a.r+b.r)) // 相离或相切 
            return 0.0;
        else // 相交 
        {
            double length=dist(a, b);
            // 利用三角形余弦定理求圆心角 
            double d1=2*acos((a.r*a.r+length*length-b.r*b.r)/(2*a.r*length)); 
            double d2=2*acos((b.r*b.r+length*length-a.r*a.r)/(2*b.r*length));
            // 利用圆心角求得扇形面积再减去三角形面积后两部分相加就是相交部分面积 
            double area1=a.r*a.r*d1/2-a.r*a.r*sin(d1)/2;
            double area2=b.r*b.r*d2/2-b.r*b.r*sin(d2)/2;
            double area=area1+area2;
            return area;
        }
}
int main()
{
    int T;
    circle a, b, A, B;
    double r, R;
    scanf("%d", &T) ;
    for(int i=1; i<=T; i++)
    {
        scanf("%lf%lf", &r, &R);   
        a.r = b.r = r;
        A.r = B.r = R;  
        scanf("%lf%lf", &a.x, &a.y);
        A.x = a.x;
        A.y = a.y;
        scanf("%lf%lf", &b.x, &b.y);
        B.x = b.x;
        B.y = b.y;     
        double sec = area(A, B) - area(A, b) - area(B, a) + area(a, b);
        printf("Case #%d: %.6lf\n", i, sec);
          
    }
    
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值