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?
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.
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-; int sgn(double x)
{
if(fabs(x) < eps) return ;
if ( x < ) return -;
else return ;
}
struct Point
{
double x,y;
Point(double _x = , double _y = )
{
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)/;
double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/;
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 = ;
Point p[];
scanf("%d",&T);
while(T--)
{
for(int i = ; i < ; i++) p[i].input();
Point res;
double tmp = 1e20;
for(int i = ; i < ; i++) //求出三条线段一半最长值
{
Point t = Point((p[i].x+p[(i+)%].x)/,(p[i].y+p[(i+)%].y)/);
double dd = max(dist(p[],t),max(dist(p[],t),dist(p[],t)));
if(dd < tmp)
{
tmp = dd;
res = t;
}
}
if(sgn( (p[]-p[])^(p[]-p[]) ) != )
{
Point t = waixin(p[],p[],p[]);
double dd = max(dist(p[],t),max(dist(p[],t),dist(p[],t)));
if(dd < tmp) tmp = dd;
}
printf("Case #%d: ",++cnt);
if(sgn(tmp - dist(res,p[])) >= ) printf("Danger\n");
else printf("Safe\n");
}
return ;
}
计算机几何(圆的外心)