Naive and Silly Muggles

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

 

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const double eps = 1e-8;

int sgn(double x)
{
    if(fabs(x) < eps) return 0 ;
    if ( x < 0) return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(double _x = 0, double _y = 0)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
};
double dist(Point a,Point b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
//过三点求圆心坐标
Point waixin(Point a,Point b,Point c)
{
    double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1*a1 + b1*b1)/2;
    double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/2;
    double d = a1*b2 - a2*b1;
    return Point(a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 -a2*c1)/d);
}
//圆心要么是三条线段的中点,或外心
int main()
{
    int T;
    int cnt = 0;
    Point p[4];
    scanf("%d",&T);
    while(T--)
    {
        for(int i = 0; i < 4; i++) p[i].input();
        Point res;
        double tmp = 1e20;
        for(int i = 0; i < 3; i++) //求出三条线段一半最长值
        {
            Point t = Point((p[i].x+p[(i+1)%3].x)/2,(p[i].y+p[(i+1)%3].y)/2);
            double dd = max(dist(p[0],t),max(dist(p[1],t),dist(p[2],t)));
            if(dd < tmp)
            {
                tmp = dd;
                res = t;
            }
        }
        if(sgn( (p[1]-p[0])^(p[2]-p[0]) ) != 0)
        {
            Point t = waixin(p[0],p[1],p[2]);
            double dd = max(dist(p[0],t),max(dist(p[1],t),dist(p[2],t)));
            if(dd < tmp) tmp = dd;
        }
        printf("Case #%d: ",++cnt);
        if(sgn(tmp - dist(res,p[3])) >= 0) printf("Danger\n");
        else printf("Safe\n");
    }
    return 0;
}
计算机几何(圆的外心)

 

posted @ 2013-09-11 19:31  1002liu  阅读(283)  评论(0编辑  收藏  举报