HDU 4720(计算几何+最小圆覆盖)

来源:互联网 发布:u盘必装软件 编辑:程序博客网 时间:2024/04/30 06:42

问题描述:

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 iand 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

30 02 01 21 -0.50 02 01 21 -0.60 03 01 11 -1.5
Sample Output

Case #1: DangerCase #2: SafeCase #3: Safe

题目题意:题目可以给我们四个点,问第四个点,在不在前三个点的最小覆盖圆里。

题目分析:直接求出圆心和半径,然后用圆心到第四个点的距离与半径比较就行。

有覆盖圆的模板就是好!

代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const double epx=1e-7;struct  Point{    double x,y;};struct Point a[1005],d;double r;double  get_dis(Point p1,Point  p2)   //两点间距离{    return (sqrt((p1.x-p2.x)*(p1.x -p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));}double get_muti(Point p1, Point p2,Point p0){    return   ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));}void get_o(Point p,Point q,int n){    d.x=(p.x+q.x)/2.0;    d.y=(p.y+q.y)/2.0;    r=get_dis(p,q)/2;    int k;    double c1,c2,t1,t2,t3;    for(k=1;k<=n;k++) {        if(get_dis(d,a[k])<=r)continue;        if(get_muti(p,q,a[k])!=0.0) {            c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;            c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;            d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));            d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));            r=get_dis(d,a[k]);        }        else {            t1=get_dis(p,q);            t2=get_dis(q,a[k]);            t3=get_dis(p,a[k]);            if(t1>=t2&&t1>=t3) {                d.x=(p.x+q.x)/2.0;                d.y=(p.y+q.y)/2.0;r=get_dis(p,q)/2.0;            }            else if(t2>=t1&&t2>=t3) {                d.x=(a[k].x+q.x)/2.0;                 d.y=(a[k].y+q.y)/2.0;                 r=get_dis(a[k],q)/2.0;            }            else {                d.x=(a[k].x+p.x)/2.0;                d.y=(a[k].y+p.y)/2.0;                r=get_dis(a[k],p)/2.0;            }        }    }}void solve(Point pi,int n)//求最小覆盖圆{    d.x=(pi.x+a[1].x)/2.0;    d.y=(pi.y+a[1].y)/2.0;    r=get_dis(pi,a[1])/2.0;    int j;    for(j=2;j<=n;j++){    if(get_dis(d,a[j])<=r)continue;        else            get_o(pi,a[j],j-1);    }}int main(){    int t,icase=1;    scanf("%d",&t);    while (t--) {        for (int i=1;i<=3;i++) {            scanf("%lf%lf",&a[i].x,&a[i].y);        }        struct Point p;        scanf("%lf%lf",&p.x,&p.y);        r=get_dis(a[1],a[2])/2.0;        d.x=(a[1].x+a[2].x)/2.0;        d.y=(a[1].y+a[2].y)/2.0;        for(int i=3;i<=3;i++){            if(get_dis(d,a[i])<=r)continue;            else            solve(a[i],i-1);        }        if (get_dis(p,d)-r<epx)            printf("Case #%d: Danger\n",icase++);        else            printf("Case #%d: Safe\n",icase++);    }    return 0;}