虽然是题看错了,并不用这么做……贴上模板以防万一:
(x1,y1,x2,y2)为线段的两个端点,(x3,y3)为圆心坐标,r为半径:
const double inf = -2139062144000.0; const double pi = acos(-1.0); double re[4]; const double eps = 1e-7; bool oncircle(double xx,double yy,double xr,double yr,double r) { if(fabs(sqrt((xx-xr) * (xx-xr) + (yy-yr) * (yy-yr)) - r) <= eps) { return true; } return false; } bool incircle(double xx,double yy,double xr,double yr,double r) { if(sqrt((xx-xr) * (xx-xr) + (yy-yr) * (yy-yr)) <= r + eps) { return true; } return false; } void jiao(double x1,double y1,double x2,double y2,double x3,double y3,double r) { re[0] = inf; re[1] = inf; re[2] = inf; re[3] = inf; if(x1 > x2) { swap(x1,x2); swap(y1,y2); } double A = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1); double B = 2*((x2-x1)*(x1-x3)+(y2-y1)*(y1-y3)); double C = x3*x3+y3*y3+x1*x1+y1*y1-2*(x3*x1+y3*y1)-r*r; double u1,u2; if(B*B-4.0*A*C >= 0) { u1 = (-B-sqrt(B*B-4.0*A*C))/(2.0*A); u2 = (-B+sqrt(B*B-4.0*A*C))/(2.0*A); double x5 = x1 + u1*(x2-x1); double y5 = y1 + u1*(y2-y1); double x6 = x1 + u2*(x2-x1); double y6 = y1 + u2*(y2-y1); // cout << x5 << " " << y5 << " " << x6 << " " << y6 << " " <<endl; if(x6 < x5) { swap(x5,x6); swap(y5,y6); } int coun = 0; if(x5 >= x1 && x5 <= x2) { re[coun*2] = x5; re[coun*2+1] = y5; coun++; } if(x6 >= x1 && x6 <= x2) { re[coun*2] = x6; re[coun*2+1] = y6; coun++; } if(coun == 2) { if(re[0] > re[2]) { swap(re[0],re[2]); swap(re[1],re[3]); } return; } if(coun == 0) { if(incircle(x1,y1,x3,y3,r) && incircle(x2,y2,x3,y3,r)) { re[0] = x1; re[1] = y1; re[2] = x2; re[3] = y2; } if(re[0] > re[2]) { swap(re[0],re[2]); swap(re[1],re[3]); } return; } if(coun == 1) { if((x2-x3) * (x2-x3) + (y2-y3) * (y2-y3) > (x1-x3) * (x1-x3) + (y1-y3) * (y1-y3)) { re[2] = x1; re[3] =y1; } else { re[2] = x2; re[3] =y2; } if(re[0] > re[2]) { swap(re[0],re[2]); swap(re[1],re[3]); } return; } } else { return; } }