zoukankan      html  css  js  c++  java
  • ZOJ 3058 Circle and Ring【圆与环相交面积】【圆与圆相交面积模板】

    Circle and Ring

    Time Limit: 1 Second      Memory Limit: 32768 KB

    Given a circle and a ring, your task is to calculate the area of their intersection.

    Input

    This problem contains multiple test cases, process to the end of file.

    For each case, there are two lines. The first line contains three real numbers x'y' and r' (0 <= r' <= 1024) representing the circle. The second line contains four real numbers xyr and R (0 <= r <= R <= 1024) representing the ring.

    Output

    For each case, output the area with the accuracy of three digits after decimal point in a signal line.

    Never output "-0.000"!

    Sample Input

    10 0 20
    -10 0 10 20
    20 30 15
    40 30 0 30
    

    Sample Output

    351.041
    608.366
    

    Author: WU, Zejun
    Source: ZOJ Monthly, November 2008

    题意:给出一个圆的圆心坐标和半径,以及一个圆环的坐标和内外圆的半径,求这个圆和圆环的相交面积。

    经分析可得,一共有如下几种情况:

    通过计算可得出:

    圆和圆环的相交面积 = 圆和圆环中大圆的相交面积 - 圆和圆环中小圆的相交面积。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <map>
    using namespace std;
    const int maxn = 100010;
    const double pi = acos(-1.0);
    
    struct point {
    	double x, y;
    };
    
    double x1, y11, r1, x2, y2, r2, R2;
    
    double D(point a, point b)	//两点间距离
    {
    	return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
    }
    
    double calc(point a, double r1, point b, double r2)	//计算面积
    {
    	double d = D(a, b);
    	double cosaa = (r1*r1 + d*d - r2*r2) / (2 * r1*d);
    	double cosa = acos(cosaa);
    	double l = r1*cosa * 2;
    	double s = l*r1 / 2;
    	double s2 = r1*r1*sin(cosa)*cos(cosa);
    	return s - s2;
    }
    
    bool ainb(point a, double r1, point b, double r2)	//判断a是否在b中
    {
    	double d = D(a, b);
    	if (d <= r2 - r1) return 1;
    	return 0;
    }
    
    bool ainstb(point a, double r1, point b, double r2)	//判断a和b是否有相交的区域
    {
    	double d = D(a, b);
    	if (d >= fabs(r1 - r2) && d <= r1 + r2)
    		return 1;
    	return 0;
    }
    
    double S(point a, double r1, point b, double r2)	//计算相交面积
    {
    	if (ainb(a, r1, b, r2))
    		return pi*r1*r1;
    	if (ainb(b, r2, a, r1))
    		return pi*r2*r2;
    	if (!ainstb(a, r1, b, r2)) return 0;
    	return calc(a, r1, b, r2) + calc(b, r2, a, r1);
    }
    
    
    
    int main()
    {
    	point a, b;
    	double r1, r2, r3;
    	while (~scanf("%lf%lf%lf", &a.x, &a.y, &r1))
    	{
    		scanf("%lf%lf%lf%lf", &b.x, &b.y, &r2, &r3);
    		printf("%.3f
    ", S(a, r1, b, r3) - S(a, r1, b, r2));
    	}
    	return 0;
    }




    Fighting~
  • 相关阅读:
    Eclipse 常用快捷键
    asp.net中如何计算中英文混合字符串长度
    图片水印的类库
    开源asp.net SNS源码下载地址
    eclipse修改编辑器字体 + Eclipse自动提示(Content Assist) + Eclipse汉化
    eclipse内部Javadoc怎么汉化成中文
    Eclipse设置输入智能提示
    Oracle建立表空间和用户
    王福朋asp.net2.0 petshop4.0系列视频教程的下载地址
    【转】在Eclipse中设置中文JavaDOC
  • 原文地址:https://www.cnblogs.com/Archger/p/12774712.html
Copyright © 2011-2022 走看看