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
 
#include <iostream>
#include <vector>
#include <math.h>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
#define PI acos(-1)
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
};
struct Circle
{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r){}
    Point point(double a)
    {
        return Point(c.x+cos(a)*r,c.y+sin(a)*r);
    }
};
Point operator+(Point A,Point B)
{
    return Point(A.x+B.x,A.y+B.y);
}
Point operator-(Point A,Point B)
{
    return Point(A.x-B.x,A.y-B.y);
}
Point operator*(Point A,double p)
{
    return Point(A.x*p,A.y*p);
}
Point operator/(Point A,double p)
{
    return Point(A.x/p,A.y/p);
}
bool operator<(const Point&a,const Point&b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Dot(Point A,Point B)
{
    return A.x*B.x+A.y*B.y;
}

double Length(Point A)//向量的长度
{
    return sqrt(Dot(A,A));
}

int dcmp(double x)
{
    if(fabs(x)<1e-12)return 0;
    else return x<0?-1:1;
}

Circle CircumscribedCircle(Point p1,Point p2,Point p3)//求三角形的外接圆,保证3点不共线
{
    double Bx=p2.x-p1.x,By=p2.y-p1.y;//向量p1p2
    double Cx=p3.x-p1.x,Cy=p3.y-p1.y;//向量p1p3
    double D=2*(Bx*Cy-By*Cx);//叉积求所围成的四边形的面积的两倍
    double cx=(Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D+p1.x;
    double cy=(Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D+p1.y;
    Point p=Point(cx,cy);
    return Circle(p,Length(p1-p));//圆心,点到点的距离
}
int main()
{
    int T,cases=1;
    cin>>T;
    while(T--)
    {
        Point P[3],P1;
        for(int i=0;i<3;i++)
           cin>>P[i].x>>P[i].y;
        cin>>P1.x>>P1.y;
        Circle Cir=CircumscribedCircle(P[0],P[1],P[2]);
        cout<<"Case #"<<cases++<<": ";
        if(Dot(P1-Cir.c,P1-Cir.c)>Cir.r*Cir.r)
            cout<<"Safe"<<endl;
        else
        {
            int flag1=0,flag2=0,flag3=0;
            Point T1=Point((P[0].x+P[1].x)/2,(P[0].y+P[1].y)/2);
            double L1=Dot(P[0]-T1,P[0]-T1);
            if(Dot(P[2]-T1,P[2]-T1)<=L1&&Dot(P1-T1,P1-T1)>L1)
             {cout<<"Safe"<<endl;flag1=1;}
            if(flag1==0)
            {
                 T1=Point((P[0].x+P[2].x)/2,(P[0].y+P[2].y)/2);
                 L1=Dot(P[0]-T1,P[0]-T1);
                 if(Dot(P[1]-T1,P[1]-T1)<=L1&&Dot(P1-T1,P1-T1)>L1)
                 {cout<<"Safe"<<endl;flag2=1;}
            }
            if(flag2==0)
            {
                 T1=Point((P[1].x+P[2].x)/2,(P[1].y+P[2].y)/2);
                 L1=Dot(P[1]-T1,P[1]-T1);
                 if(Dot(P[0]-T1,P[0]-T1)<=L1&&Dot(P1-T1,P1-T1)>L1)
                 {cout<<"Safe"<<endl;flag3=1;}
            }
            if(flag1==0&&flag2==0&&flag3==0)
                {cout<<"Danger"<<endl;}
        }
    }
    return 0;
}


posted on 2015-05-07 15:26  星斗万千  阅读(130)  评论(0编辑  收藏  举报