HDU 4720 Naive and Silly Muggles 平面几何

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720

解题报告:给出一个三角形的三个顶点坐标,要求用一个最小的圆将这个三个点都包含在内,另外输入一个点,问这个点是否在这个圆的圆外,如果在圆外,输出Safe,否则输出Danger。

现在的主要目的其实就是求这个最小的三角形的圆心坐标,很显然,当这个三角形是锐角三角形的时候,这个最小的圆就是这个锐角三角型的外接圆,否则就是以这个三角形最长的那条边的中点为圆心,以这条边的一半为半径的圆。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 int judge(double x1,double y1,double x2,double y2,double x3,double y3)
 8 {
 9     double X1 = x2 - x1;
10     double Y1 = y2 - y1;
11     double X2 = x3 - x1;
12     double Y2 = y3 - y1;
13     return (X1*X2 + Y1*Y2 > 0);
14 }
15 
16 double dis(double x1,double y1,double x2,double y2)
17 {
18     return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
19 }
20 
21 int main()
22 {
23     int T,Case = 1;
24     double x1,y1,x2,y2,x3,y3,x4,y4,a,b,c;
25     scanf("%d",&T);
26     while(T--)
27     {
28         scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
29         printf("Case #%d: ",Case++);
30         if(judge(x1,y1,x2,y2,x3,y3) && judge(x2,y2,x1,y1,x3,y3) && judge(x3,y3,x1,y1,x2,y2))
31         {
32             double x0 = (x1 + x2 + x3) / 3.0;
33             double y0 = (y1 + y2 + y3) / 3.0;
34             if(dis(x0,y0,x4,y4) > dis(x0,y0,x1,y1))
35             printf("Safe\n");
36             else printf("Danger\n");
37         }
38         else 
39         {
40             a = dis(x1,y1,x2,y2);
41             b = dis(x1,y1,x3,y3);
42             c = dis(x2,y2,x3,y3);
43             if(a >= b && a >= c)
44             {
45                 double x0 = (x1 + x2) / 2.0;
46                 double y0 = (y1 + y2) / 2.0;
47                 if(dis(x0,y0,x4,y4) > a / 2.0)
48                 printf("Safe\n");
49                 else printf("Danger\n");
50             }
51             else if(b >= a && b >= c)
52             {
53                 double x0 = (x1 + x3) / 2.0;
54                 double y0 = (y1 + y3) / 2.0;
55                 if(dis(x0,y0,x4,y4) > b / 2.0)
56                 printf("Safe\n");
57                 else printf("Danger\n");
58             }
59             else if(c >= a && c >= b)
60             {
61                 double x0 = (x2 + x3) / 2.0;
62                 double y0 = (y2 + y3) / 2.0;
63                 if(dis(x0,y0,x4,y4) > c / 2.0)
64                 printf("Safe\n");
65                 else printf("Danger\n");
66             }
67         }
68     }
69     return 0;
70 }
View Code

 

posted @ 2013-09-17 11:48  xiaxiaosheng  阅读(339)  评论(0编辑  收藏  举报