Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 61 Accepted Submission(s): 39
1 通俗算法 2 定义:设平面上的三点A(x1,y1),B(x2,y2),C(x3,y3),定义 3 S(A,B,C) = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) 4 5 已知三角形的三个顶点为A(x1,y1),B(x2,y2),C(x3,y3),则该三角形的外心为: 6 S((x1*x1+y1*y1, y1), (x2*x2+y2*y2, y2), (x3*x3+y3*y3, y3)) 7 x0 = ----------------------------------------------------------- 8 2*S(A,B,C) 9 10 S((x1,x1*x1+y1*y1), (x2, x2*x2+y2*y2), (x3, x3*x3+y3*y3)) 11 y0 = ----------------------------------------------------------- 12 2*S(A,B,C)
代码形式:
1 //求外接圆的圆心 2 double S(double x1,double y1,double x2,double y2,double x3,double y3){ 3 return ((x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) ); 4 } 5 6 double getx(double x1,double y1,double x2,double y2,double x3,double y3){ 7 return (S(x1*x1+y1*y1,y1, x2*x2+y2*y2, y2,x3*x3+y3*y3,y3)/(2*S(x1,y1,x2,y2,x3,y3)) ); 8 } 9 10 double gety(double x1,double y1,double x2,double y2,double x3,double y3){ 11 return (S(x1, x1*x1+y1*y1, x2, x2*x2+y2*y2, x3, x3*x3+y3*y3) / (2*S(x1,y1,x2,y2,x3,y3))); 12 }
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 bool isline(double *a,double *b,double *c) 7 { 8 if(fabs((b[1]-a[1])*(c[0]-a[0])-(c[1]-a[1])*(b[0]-a[0]))<1e-8) 9 return 1; 10 else 11 return 0; 12 } 13 //求外接圆的圆心 14 double S(double x1,double y1,double x2,double y2,double x3,double y3){ 15 return ((x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) ); 16 } 17 18 double getx(double x1,double y1,double x2,double y2,double x3,double y3){ 19 return (S(x1*x1+y1*y1,y1, x2*x2+y2*y2, y2,x3*x3+y3*y3,y3)/(2*S(x1,y1,x2,y2,x3,y3)) ); 20 } 21 22 double gety(double x1,double y1,double x2,double y2,double x3,double y3){ 23 return (S(x1, x1*x1+y1*y1, x2, x2*x2+y2*y2, x3, x3*x3+y3*y3) / (2*S(x1,y1,x2,y2,x3,y3))); 24 } 25 //求两条边的夹角 26 bool iftrue(double *a,double *b,double *c ) 27 { 28 return (a[0]-b[0])*(c[0]-b[0])+(a[1]-b[1])*(c[1]-b[1])>0?0:1; //不是锐角时yes 29 } 30 //求两点间的距离 31 double distan(double *a,double *b) 32 { 33 return sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]))/2.0; 34 } 35 36 int main() 37 { 38 int t,count,i; 39 double po[4][2],r,save[2][2],x,y; 40 scanf("%d",&t); 41 for(count=1;count<=t;count++) 42 { 43 for(i=0;i<4;i++) 44 { 45 scanf("%lf%lf",&po[i][0],&po[i][1]); 46 if(i==0||save[1][0]*save[1][0]+save[1][1]*save[1][1]<po[i][0]*po[i][0]+po[i][1]*po[i][1]) 47 save[1][1]=po[i][1],save[1][0]=po[i][0]; 48 if(i==0||save[0][0]*save[0][0]+save[0][1]*save[0][1]>po[i][0]*po[i][0]+po[i][1]*po[i][1]) 49 save[0][1]=po[i][1],save[0][0]=po[i][0]; 50 } 51 if(isline(po[0],po[1],po[2])) 52 { 53 r=sqrt((save[1][0]-save[0][0])*(save[1][0]-save[0][0])+(save[1][1]-save[0][1])*(save[1][1]-save[0][1]))/2.0; 54 x=(save[0][0]+save[1][0])/2.0; 55 y=(save[0][1]+save[1][1])/2.0; 56 } 57 else 58 { 59 bool judge[3]; 60 judge[0]=iftrue(po[0],po[1],po[2]); 61 judge[1]=iftrue(po[1],po[0],po[2]); 62 judge[2]=iftrue(po[1],po[2],po[0]); 63 if(judge[0]||judge[1]||judge[2]) 64 { 65 if(judge[0]) 66 { 67 x=(po[0][0]+po[2][0])/2.0; 68 y=(po[0][1]+po[2][1])/2.0; 69 r=distan(po[0],po[2]); 70 } 71 else if(judge[1]) 72 { 73 x=(po[1][0]+po[2][0])/2.0; 74 y=(po[1][1]+po[2][1])/2.0; 75 r=distan(po[1],po[2]); 76 } 77 else if(judge[2]) 78 { 79 x=(po[1][0]+po[0][0])/2.0; 80 y=(po[1][1]+po[0][1])/2.0; 81 r=distan(po[0],po[1]); 82 } 83 } 84 else 85 { 86 //当为锐角时,求其外接圆,否者不求 87 x=getx(po[0][0],po[0][1],po[1][0],po[1][1],po[2][0],po[2][1]); 88 y=gety(po[0][0],po[0][1],po[1][0],po[1][1],po[2][0],po[2][1]); 89 r=sqrt((po[2][0]-x)*(po[2][0]-x)+(po[2][1]-y)*(po[2][1]-y)); 90 } 91 } 92 double temp=sqrt((po[3][0]-x)*(po[3][0]-x)+(po[3][1]-y)*(po[3][1]-y)); 93 if(r>temp-1e-8) 94 printf("Case #%d: Danger\n",count); 95 else 96 printf("Case #%d: Safe\n",count); 97 } 98 return 0; 99 }