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<iostream>
using namespace std;
struct Point{
 double x;
 double y;
};
struct Traingle{
 struct Point p[3];
};
struct Circle{
 struct Point center;
 double r;
};
double Dis(struct Point p,struct Point q)
{
 double dx=p.x-q.x;
 double dy=p.y-q.y;
 return sqrt(dx*dx+dy*dy);
}
double Area(struct Traingle ct)
{
 return fabs((ct.p[1].x-ct.p[0].x)*(ct.p[2].y-ct.p[0].y)-(ct.p[2].x-ct.p[0].x)*(ct.p[1].y-ct.p[0].y))/2.0;
}
double maxi(double a,double b)
{
 return a>b?a:b;
}
struct Circle CircumCircle(struct Traingle t)
{
 struct Circle tmp;
 int flag=0;
 double a,b,c,c1,c2,tt;
 double xA,yA,xB,yB,xC,yC;
 a=Dis(t.p[0],t.p[1]);
 b=Dis(t.p[1],t.p[2]);
 c=Dis(t.p[2],t.p[0]);
    tt=maxi(c,maxi(a,b));
    if(tt==a)
 {
       if(a*a>b*b+c*c)
    {
     tmp.r=a/2;
     tmp.center.x=(t.p[0].x+t.p[1].x)/2;
     tmp.center.y=(t.p[0].y+t.p[1].y)/2;
     flag=1;
    }
 }
 else if(tt==b)
 {
  if(b*b>a*a+c*c)
  {
   tmp.r=b/2;
   tmp.center.x=(t.p[1].x+t.p[2].x)/2;
   tmp.center.y=(t.p[1].y+t.p[2].y)/2;
   flag=1;
  }
 }
 else if(tt==c)
 {
  if(c*c>a*a+b*b)
  {
   tmp.r=c/2;
   tmp.center.x=(t.p[2].x+t.p[0].x)/2;
   tmp.center.y=(t.p[2].y+t.p[0].y)/2;
   flag=1;
  }
 }
 if(flag==0)
 {
 tmp.r=(a*b*c)/(Area(t)*4.0);
 xA=t.p[0].x;
 yA=t.p[0].y;
 xB=t.p[1].x;
 yB=t.p[1].y;
 xC=t.p[2].x;
 yC=t.p[2].y;
 c1=(xA*xA+yA*yA-xB*xB-yB*yB)/2;
 c2=(xA*xA+yA*yA-xC*xC-yC*yC)/2;
 tmp.center.x=(c1*(yA-yC)-c2*(yA-yB))/((xA-xB)*(yA-yC)-(xA-xC)*(yA-yB));
 tmp.center.y=(c1*(xA-xC)-c2*(xA-xB))/((yA-yB)*(xA-xC)-(yA-yC)*(xA-xB));
 }
 return tmp;
}
int main()
{
    Circle cr;
 Traingle s;
 int i,ca=0;
 int test;
 double a,b;
 scanf("%d",&test);
    while(test--)
 {
  ca++;
  for(i=0;i<3;i++)
  {
   scanf("%lf %lf",&s.p[i].x,&s.p[i].y);
  }
  cr=CircumCircle(s);
  scanf("%lf %lf",&a,&b);
  printf("Case #%d: ",ca);
  if((a-cr.center.x)*(a-cr.center.x)+(b-cr.center.y)*(b-cr.center.y)<=cr.r*cr.r) printf("Danger\n");
  else printf("Safe\n");
 }
 return 0;
}
posted @ 2013-09-13 00:08  forevermemory  阅读(373)  评论(0编辑  收藏  举报