HDU4720+三角形外接圆

 1 /*
 2 几何
 3 求给定三角形的外接圆圆心 
 4 方法:求解二元方程组 
 5 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 #include<math.h>
 9 #include<stdlib.h>
10 const double pi = acos(-1.0);
11 const double eps = 1e-8;
12 struct Point{
13     double x,y;
14 };
15 struct Circle{
16     Point center;
17     double r;
18 };
19 Point a,b,c,tp;
20 Circle cc;
21 
22 double dis( Point a,Point b ){
23     return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
24 }
25 
26 bool JudgeTriangle( Point a,Point b,Point c ){
27     double len1 = dis( a,b );
28     len1 *= len1;
29     double len2 = dis( a,c );
30     len2 *= len2;
31     double len3 = dis( b,c );
32     len3 *= len3;
33     if( len1+len2-len3<=0||len1+len3-len2<=0||len2+len3-len1<=0 ) return false;
34     else return true;
35 }/*判断是否是钝角三角形*/
36 
37 void GetCircle( Point a,Point b,Point c ){
38     bool judge = JudgeTriangle( a,b,c );
39     if( judge==false ){
40         double ans;
41         int op ;
42         double len1 = dis( a,b );
43         ans = len1;
44         op = 1;
45         double len2 = dis( a,c );
46         if( len2>ans ){
47             ans = len2;
48             op = 2;
49         }
50         double len3 = dis( b,c );
51         if( len3>ans ){
52             ans = len3;
53             op = 3;
54         }
55         cc.r = ans / 2.0;
56         if( op==1 ){
57             cc.center.x = ( a.x+b.x )/2.0;
58             cc.center.y = ( a.y+b.y )/2.0;
59         }
60         else if( op==2 ){
61             cc.center.x = ( a.x+c.x )/2.0;
62             cc.center.y = ( a.y+c.y )/2.0;
63         }
64         else {
65             cc.center.x = ( b.x+c.x )/2.0;
66             cc.center.y = ( b.y+c.y )/2.0;
67         }
68         return ;
69     }/*钝角三角形*/
70     double x1 = a.x,x2 = b.x,x3 = c.x;
71     double y1 = a.y,y2 = b.y,y3 = c.y;
72     cc.center.x=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2*(x3-x1)*(y2-y1)-2*((x2-x1)*(y3-y1)));    
73     cc.center.y=((x2-x1)*(x3*x3-x1*x1+y3*y3-y1*y1)-(x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1))/(2*(y3-y1)*(x2-x1)-2*((y2-y1)*(x3-x1)));   
74     cc.r = dis( cc.center,a );
75     return ;    
76 }/*求外接圆圆心*/ 
77 
78 int main(){
79     int T;
80     scanf("%d",&T);
81     int ca = 1;
82     while( T-- ){
83         scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&tp.x,&tp.y);
84         GetCircle( a,b,c );
85         printf("Case #%d: ",ca++);
86         if( dis( tp,cc.center )<=cc.r ) printf("Danger\n");
87         else printf("Safe\n");
88     }
89     return 0;
90 }
View Code

 

posted @ 2013-12-07 21:35  xxx0624  阅读(257)  评论(0编辑  收藏  举报