Naive and Silly Muggles(计算几何 判断点是否在三点形成的最小圆内)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720


Problem 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
 

Source


题意:

判断点是否在三点形成的最小圆内,在圆外输出Danger,圆内输出Safe!


PS:

首先确定这三个点构成的是否是钝角三角形?

如果是,则形成的那个圆的半径是前面三个点形成的距离中最远的距离的一半,圆心就是那两个点的中点,然后判断第四个点到圆心的距离与半径进行比较;

如果这三个点形成的不是钝角三角形,圆心就是三角形的外接圆的圆心,然后进行判断;

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
struct point
{
    double x, y;
} a[4];
double rr;//半径
point C, p;
double dis(point a, point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int judge(int d1, int d2, int d3)
{
    if(d1*d1 > d2*d2+d3*d3)
    {
        C.x = (a[1].x+a[2].x)/2.0;
        C.y = (a[1].y+a[2].y)/2.0;
        rr = d1/2.0;
        return 1;
    }
    else if(d2*d2 > d1*d1+d3*d3)
    {
        C.x = (a[1].x+a[3].x)/2.0;
        C.y = (a[1].y+a[3].y)/2.0;
        rr = d2/2.0;
        return 1;
    }
    else if(d3*d3 > d1*d1+d2*d2)
    {
        C.x = (a[2].x+a[3].x)/2.0;
        C.y = (a[2].y+a[3].y)/2.0;
        rr = d3/2.0;
        return 1;
    }
    return 0;
}
int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d: ",++cas);
        for(int i = 1; i <= 3; i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        scanf("%lf%lf",&p.x,&p.y);
        double d1 = dis(a[1], a[2]);
        double d2 = dis(a[1], a[3]);
        double d3 = dis(a[2], a[3]);
        if(judge(d1,d2,d3))//如果是钝角
        {
            double disl = dis(p,C);
            if(disl > rr)
            {
                printf("Safe\n");
            }
            else
            {
                printf("Danger\n");
            }
        }
        else
        {
            double x1 = a[1].x, y1 = a[1].y;
            double x2 = a[2].x, y2 = a[2].y;
            double x3 = a[3].x, y3 = a[3].y;
            C.x = ((y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1)+(y2-y1)*(y1*y1-y3*y3+x1*x1-x3*x3))/(2*(x2-x1)*(y3-y1)-2*(x3-x1)*(y2-y1));

            C.y = ((x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1)+(x2-x1)*(x1*x1-x3*x3+y1*y1-y3*y3))/(2*(y2-y1)*(x3-x1)-2 *(y3-y1)*(x2-x1));

            double disl = dis(p,C);
            rr = dis(C,a[1]);
            if(disl > rr)
            {
                printf("Safe\n");
            }
            else
            {
                printf("Danger\n");
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值