1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<algorithm> 5 using namespace std; 6 7 struct node 8 { 9 double x,y; 10 } point[5]; 11 double disA_B,disA_C,disB_C,Maxdis;//分别记录三边长和最长边 12 int a,b;//记录最长边的两个端点 13 double center_x,center_y;//最小圆的圆心坐标 14 double d;//记录圆心到该点的距离,与半径比较; 15 double radiu;//锐角三角形外接圆半径; 16 17 void Distance() 18 { 19 disA_B = sqrt((point[0].x-point[1].x)*(point[0].x-point[1].x)+ 20 (point[0].y-point[1].y)*(point[0].y-point[1].y)); 21 disA_C = sqrt((point[0].x-point[2].x)*(point[0].x-point[2].x)+ 22 (point[0].y-point[2].y)*(point[0].y-point[2].y)); 23 disB_C = sqrt((point[2].x-point[1].x)*(point[2].x-point[1].x)+ 24 (point[2].y-point[1].y)*(point[2].y-point[1].y)); 25 26 if(disA_B >= disA_C && disA_B >= disB_C) 27 { 28 Maxdis = disA_B; 29 a = 0; 30 b = 1; 31 } 32 if(disA_C >= disA_B && disA_C >= disB_C) 33 { 34 Maxdis = disA_C; 35 a = 0; 36 b = 2; 37 } 38 if(disB_C >= disA_B && disB_C >= disA_C) 39 { 40 Maxdis = disB_C; 41 a = 1; 42 b = 2; 43 } 44 45 } 46 //计算圆心到顶点的距离 47 double check(double x1,double y1,double x2,double y2) 48 { 49 double d; 50 d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 51 return d; 52 } 53 //判断三角形类型 54 int judge(double r1, double r2,double r3) 55 { 56 double res; 57 res = r1*r1+r2*r2-r3*r3; 58 if(res <= 0) 59 return true;//钝角或直角三角形 60 else return false;//锐角三角形 61 } 62 63 int main() 64 { 65 int test; 66 scanf("%d",&test); 67 for(int t = 1; t <= test; t++) 68 { 69 for(int i = 0; i < 4; i++) 70 scanf("%lf %lf",&point[i].x,&point[i].y); 71 72 Distance(); 73 74 printf("Case #%d: ",t); 75 76 //三点在一条直线上,圆心在最长边的1/2处; 77 if(point[0].x==point[1].x && point[0].x==point[2].x) 78 { 79 center_x = (point[a].x+point[b].x)/2.0; 80 center_y = point[a].y; 81 d = check(center_x,center_y,point[3].x,point[3].y); 82 //printf("圆心(%lf,%lf),距离 = %lf,半径 = %lf\n",center_x,center_y,d,Maxdis/2.0); 83 if(d > Maxdis/2.0) 84 printf("Safe\n"); 85 else printf("Danger\n"); 86 //printf("在一条直线上\n"); 87 } 88 else if(point[0].y==point[1].y && point[0].y==point[2].y) 89 { 90 center_x = point[a].x; 91 center_y = (point[a].y+point[b].y)/2.0; 92 d = check(center_x,center_y,point[3].x,point[3].y); 93 //printf("圆心(%lf,%lf),距离 = %lf,半径 = %lf\n",center_x,center_y,d,Maxdis/2.0); 94 if(d > Maxdis/2.0) 95 printf("Safe\n"); 96 else printf("Danger\n"); 97 //printf("在一条直线上\n"); 98 } 99 100 //钝角或直角三角形,圆心是最长边的1/2处,半径是最长边的一半; 101 else if(judge(disA_B,disA_C,disB_C) || judge(disA_B,disB_C,disA_C) || judge(disA_C,disB_C,disA_B)) 102 { 103 center_x = (point[a].x+point[b].x)/2.0; 104 center_y = (point[a].y+point[b].y)/2.0; 105 d = check(center_x,center_y,point[3].x,point[3].y); 106 //printf("圆心(%lf,%lf),距离 = %lf,半径 = %lf\n",center_x,center_y,d,Maxdis/2.0); 107 if(d > Maxdis/2.0) 108 printf("Safe\n"); 109 else printf("Danger\n"); 110 //printf("是直角或钝角\n"); 111 } 112 113 //锐角三角形,圆心是外接圆的圆心; 114 else if(!judge(disA_B,disA_C,disB_C) && !judge(disA_B,disB_C,disA_C) && !judge(disA_C,disB_C,disA_B)) 115 { 116 center_x = 0; 117 center_y = 0; 118 double x1 = point[0].x; 119 double x2 = point[1].x; 120 double x3 = point[2].x; 121 double y1 = point[0].y; 122 double y2 = point[1].y; 123 double y3 = point[2].y; 124 125 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))); 126 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))); 127 radiu = sqrt((point[0].x-center_x)*(point[0].x-center_x)+(point[0].y-center_y)*(point[0].y-center_y)); 128 129 d = check(center_x,center_y,point[3].x,point[3].y); 130 //printf("圆心(%lf,%lf),距离 = %lf,半径 = %lf\n",center_x,center_y,d,radiu); 131 if(d > radiu) 132 printf("Safe\n"); 133 else printf("Danger\n"); 134 //printf("是锐角\n"); 135 136 } 137 138 } 139 return 0; 140 }
1 void circle_center(point[3]) 2 { 3 double x1,x2,x3,y1,y2,y3; 4 double radiu;//外接圆半径 5 double x = 0; 6 double y = 0; 7 8 x1 = point[0].x; 9 x2 = point[1].x; 10 x3 = point[2].x; 11 y1 = point[0].y; 12 y2 = point[1].y; 13 y3 = point[2].y; 14 15 //圆心坐标(x,y); 16 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))); 17 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))); 18 //半径 19 radiu = sqrt((point[0].x - x)*(point[0].x - x) + (point[0].y - y)*(point[0].y - y)); 20 }