HDU 4720--Naive and Silly Muggles (关于圆)

本文介绍了一个经典的几何问题:如何找到一个最小的圆来覆盖三个给定点,并判断另一点是否位于该圆内或圆上。文章提供了详细的算法实现,包括确定圆心位置和半径的方法。

Description

Three wizards are doing a experiment. To avoid from bothering, a special magic is set around them. The magic forms a circle, which covers those three wizards, in other words, all of them are inside or on the border of the circle. And due to save the magic power, circle's area should as smaller as it could be.
Naive and silly "muggles"(who have no talents in magic) should absolutely not get into the circle, nor even on its border, or they will be in danger.
Given the position of a muggle, is he safe, or in serious danger?
 

Input

The first line has a number T (T <= 10) , indicating the number of test cases.
For each test case there are four lines. Three lines come each with two integers x i and y i (|x i, y i| <= 10), indicating the three wizards' positions. Then a single line with two numbers q x and q y (|q x, q y| <= 10), indicating the muggle's position.
 

Output

For test case X, output "Case #X: " first, then output "Danger" or "Safe".
 

Sample Input

3 0 0 2 0 1 2 1 -0.5 0 0 2 0 1 2 1 -0.6 0 0 3 0 1 1 1 -1.5
 

Sample Output

Case #1: Danger Case #2: Safe Case #3: Safe

题意: 简单了说就是给你三个点 求一个最小的圆, 判断另一个点在圆上还是圆内...

思路: 应该注意的是钝角三角形不是外界圆最小, 处理这个问题 只需每次对一个三角形都求出外接圆的半径 和 符合要求的最长边 (直径) 作比较 ~~

         这是我写过的最长的代码了>.<..不过1 A 还是很开心^.^

CODE:

#include<iostream>
#include<stdio.h>
using namespace std;

double X,Y;
double a1,a2,b1,b2,c1,c2;
double ab1,ab2,bc1,bc2,ac1,ac2;
double x,y;
double xx,yy;
double  d[4];

void middlepoint()
{
    ab1=(a1+b1)/2;
    ab2=(a2+b2)/2;
    bc1=(b1+c1)/2;
    bc2=(b2+c2)/2;
    ac1=(a1+c1)/2;
    ac2=(a2+c2)/2;
}
void work()
{
    double ac,ab;
    if(b2==a2)
    {
        X=(a1+b1)/2;
        ac=1.0*(a1-c1)/(a2-c2);
        Y=ac*(ac1-X)+ac2;
    }
    else if(a2==c2)
    {
        X=(a1+c1)/2;
        ab=1.0*(b1-a1)/(b2-a2);
        Y=ab*(ab1-X)+ab2;

    }
    else
    {
        ab=1.0*(b1-a1)/(b2-a2);
        ac=1.0*(a1-c1)/(a2-c2);
        X=(ac*ac1-ab*ab1+ac2-ab2)/(ac-ab);
        Y=ab*(ab1-X)+ab2;
    }
}

double R()
{
    double t1=1.0*(X-a1),t2=1.0*(Y-a2);
    return  t1*t1+t2*t2;
}
double dis(double a,double aa,double b,double bb)
{
    double t1=1.0*(a-b),t2=1.0*(aa-bb);
    return t1*t1+t2*t2;
}
double disnn()
{
    double t1=1.0*(x-X),t2=1.0*(y-Y);
    return t1*t1+t2*t2;
}
int chose_short(int tt)
{
    if(tt==0)
    {
        xx=(a1+b1)/2;
        yy=(a2+b2)/2;
        if(dis(c1,c2,xx,yy)*4<=d[tt]) return 1;
    }
    if(tt==1)
    {
        xx=(a1+c1)/2;
        yy=(a2+c2)/2;
        if(dis(b1,b2,xx,yy)*4<=d[tt]) return 1;
    }
    if(tt=2)
    {
        xx=(c1+b1)/2;
        yy=(c2+b2)/2;
        if(dis(a1,a2,xx,yy)*4<=d[tt]) return 1;
    }
    return 0;
}
int main()
{
    //freopen("in.in","r",stdin);
    int t;
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++)
    {
        cin>>a1>>a2>>b1>>b2>>c1>>c2;
        scanf("%lf%lf",&x,&y);
        middlepoint();
        work();
        double r=R();
        double maxn=0.0;
        int t;
        d[0]=dis(a1,a2,b1,b2);
        d[1]=dis(a1,a2,c1,c2);
        d[2]=dis(b1,b2,c1,c2);
        for(int i=0;i<3;i++)
        {
            if(maxn<d[i])
            {maxn=d[i];t=i;}
        }
        if(chose_short(t)&&maxn<4*r)
        {
            maxn=maxn/4;
            X=xx,Y=yy;
        }
        else maxn=r;
        if(maxn>=disnn()) printf("Case #%d: Danger\n",tt);
        else printf("Case #%d: Safe\n",tt);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值