题意: 问三点的最小包络圆 是否包含第四个点?
分析:模板题,但需要注意分锐角,钝角两种情况。
代码:
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
#define PI 3.141592653589793
struct point
{
double x;
double y;
};
struct Line
{
point a;
point b;
};
point intersection(Line u,Line v)
{
point res=u.a;
double k=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
res.x+=(u.b.x-u.a.x)*k;
res.y+=(u.b.y-u.a.y)*k;
return res;
}
point circumcenter(point a,point b,point c)
{
Line u,v;
u.a.x=(a.x+b.x)/2;
u.a.y=(a.y+b.y)/2;
u.b.x=u.a.x-a.y+b.y;
u.b.y=u.a.y+a.x-b.x;
v.a.x=(a.x+c.x)/2;
v.a.y=(a.y+c.y)/2;
v.b.x=v.a.x-a.y+c.y;
v.b.y=v.a.y+a.x-c.x;
return intersection(u,v);
}
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 main()
{
point A,B,C,O,Q;
int T,cas=1;
cin>>T;
while(T--)
{
double r;
cin>>A.x>>A.y
>>B.x>>B.y
>>C.x>>C.y
>>Q.x>>Q.y;
double a=dis(B,C),b=dis(A,C),c=dis(A,B);
bool flag=true;
if(a*a+b*b<c*c)
{
flag=false;
O.x=(A.x+B.x)/2.0;
O.y=(A.y+B.y)/2.0;
r=c/2.0;
}
else if(b*b+c*c<a*a)
{
flag=false;
O.x=(B.x+C.x)/2.0;
O.y=(B.y+C.y)/2.0;
r=a/2;
}
else if(a*a+c*c<b*b)
{
flag=false;
O.x=(A.x+C.x)/2.0;
O.y=(A.y+C.y)/2.0;
r=b/2;
}
if(flag)
{
O=circumcenter(A,B,C);
r=dis(O,A);
}
if(dis(Q,O)>r) printf("Case #%d: Safe\n",cas++);
else printf("Case #%d: Danger\n",cas++);
}
return 0;
}