题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720
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
Case #1: Danger Case #2: Safe Case #3: Safe
题意:
判断点是否在三点形成的最小圆内,在圆外输出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;
}