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 xi and yi (|xi, yi| <= 10), indicating the three wizards‘ positions. Then a single line with two numbers qx and qy (|qx,
qy| <= 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

2013 ACM/ICPC Asia Regional Online —— Warmup2

题意:

判断点是否在三点形成的最小圆内,在圆外输出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;
}
时间: 2024-10-19 13:28:04

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

ACM学习历程—HDU4720 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

hdu 4720 Naive and Silly Muggles(几何)

题目链接:hdu 4720 Naive and Silly Muggles 题目大意:给出三点,找出一个圆,要求面积尽量小,并且三点必须在园内,如果可以找到一个圆,使得说第4个点不在圆内则是安全的. 解题思路:面积最小即三个点外切圆,根据三角形两条边的垂直平分线求出圆心.判断第4个点是否在圆内只要计算距离即可. 然后还要考虑说面积和外切圆相同,但是圆心不同的圆. #include <cstdio> #include <cstring> #include <cmath>

计算几何 HDOJ 4720 Naive and Silly Muggles

题目传送门 /* 题意:给三个点求它们的外接圆,判断一个点是否在园内 计算几何:我用重心当圆心竟然AC了,数据真水:) 正解以后补充,http://www.cnblogs.com/kuangbin/archive/2013/09/11/3315055.html */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <string

HDU 4720 :Naive and Silly Muggles

题目:HDU 4720 :Naive and Silly Muggles 题目大意:这题的意思是给出三个点, 然后在给出另一个点,问这个点会不会在覆盖前面三个点的最小的圆里面(包括边界), 在里面最输出danger, 如果任何情况下这个点都不在圆里面,那么就输出safe. 解题思路:三个点最小的覆盖的圆是三角形的外接圆,这样的圆面积一定是最小的. 但是相同面积的圆,所在的位置,覆盖的点会是不一样的.例如垂心关于三条边的对称点,以某个对称点为圆心的圆用之前的半径做圆,如果能够覆盖原来的三个点(点可

HDOJ 4720 Naive and Silly Muggles 三角形外接圆

当是钝角三角形时,就是最长边为直径的圆最小. 否则,求三角形的外接圆 Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 700    Accepted Submission(s): 451 Problem Description Three wizards are doing a exper

hdu4720Naive and Silly Muggles

链接 一直理解的最小覆盖圆就是外接圆..原来还要分钝角和锐角... 钝角的话就为最长边的中点,对于这题分别枚举一下外接圆以及中点的圆,判一下是不是在园外. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmat

ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。

应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了. 下面提供两条思路: 法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的年.月.日,然后再拼上时.分.秒,然后再将拼接后的字符串转为NSDate,最后用当前的时间跟自己生成的俩NSDate的时间点比较.(该方法比较笨,也不难,但看起来有点太菜了,看上去不怎么

js判断变量初始化的三种形式

<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> //js判断变量初始化有三种形式 var x; if (x == null) { alert("x为null"); } if (typeof (x) == "un

Android判断当前系统时间是否在指定时间的范围内(免消息打扰)

/** * 判断当前系统时间是否在指定时间的范围内 * * @param beginHour * 开始小时,例如22 * @param beginMin * 开始小时的分钟数,例如30 * @param endHour * 结束小时,例如 8 * @param endMin * 结束小时的分钟数,例如0 * @return true表示在范围内,否则false */ public static boolean isCurrentInTimeScope(int beginHour, int beg