[zoj1608]Two Circles and a Rectangle--计算几何

题目描述

Give you two circles and a rectangle, your task is to judge wheather the two circles can be put into the rectangle with no part of circles outside the retangle.

Input

There are multiple test cases. In every test cast, there are four float-point numbers:

a, b, r1, r2

where, a and b are two sides of the rectangle, r1 and r2 are radii of the two circls.

Output

Print a “Yes”, if the circles can be put into the rectangle. Otherwise, print a “No”.

You can safely assume x < y, where x and y are float-point numbers, if x < y + 0.01.

Sample Input

5 4 1 1
5 4 1.5 2

Sample Output

Yes
No

题解

这道题目的意思就是给一个长宽分别为a和b的矩形,要你判断其中是否能放进两个半径为r1和r2的圆。

这道题目可以说是最简单的计算几何了。。。
首先你肯定需要满足一个圆放得进去,也就是要满足 2×max(r1,r2)<=min(a,b) 2 × m a x ( r 1 , r 2 ) <= m i n ( a , b )
然后我们可以想象一下,相切的状态肯定是一个临界状态,所以说我们只需要考虑相切的情况即可。
在相切的情况下,我们可以将两个圆心连起来,然后一个向a作垂线,一个向b作垂线,这样就构成了一个直角三角形。观察一下,它的两个直角边是 x=ar1r2 x = a − r 1 − r 2 x=br1r2 x = b − r 1 − r 2 ,而它的斜边是 z=r1+r2 z = r 1 + r 2 ,而相切的时候是满足 x2+y2=z2 x 2 + y 2 = z 2 的,那么我们就可以考虑一下,如果满足条件是个什么情况。可以想到应该是 x2+y2z2 x 2 + y 2 ≤ z 2 的。

于是结合上述两个条件,这道题目就解决啦。。

下面是代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
double a,b,r1,r2;
bool check()
{
    double tmp1=min(a,b);
    double tmp2=max(r1,r2);
    if(tmp1<tmp2*2)  return false;
    double p1=a-r1-r2,p2=b-r1-r2,p3=r1+r2;
    if(p1*p1+p2*p2>=p3*p3)  return true;
    return false; 
}
int main()
{
    while(~scanf("%lf%lf%lf%lf",&a,&b,&r1,&r2))
    {
        if(check()==true)  puts("Yes");
        else               puts("No"); 
    }
    return 0;
}

第一篇博客。。。
如果有什么写得不好的地方,可以直接在下面留,谢谢大家!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值