My code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int t;
double x1, y1, x2, y2, r, d, d1, d2, t1, t2, pl, m, rl, theta, arc;
scanf("%d", &t);
while(t--){
scanf("%lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &r);
if(x1 == x2) pl = fabs(x1);
else if(y1 == y2) pl = fabs(y1);
else{
m = (y2 - y1) / (x2 - x1);
pl = fabs((- m*x1 + y1) / sqrt(pow(m, 2) + 1));
}
if(pl > r)rl = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
else{
t1 = sqrt(pow(x1, 2) + pow(y1, 2) - pow(r, 2));
t2 = sqrt(pow(x2, 2) + pow(y2, 2) - pow(r, 2));
d1 = sqrt(pow(x1, 2) + pow(y1, 2));
d2 = sqrt(pow(x2, 2) + pow(y2, 2));
d = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
theta = (acos((pow(d1, 2) + pow(d2, 2) - pow(d, 2)) / (2*d1*d2))) - ((acos((pow(d1, 2) + pow(r, 2) - pow(t1, 2)) / (2*d1*r))) + (acos((pow(d2, 2) + pow(r, 2) - pow(t2, 2)) / (2*r*d2))));
arc = theta * r;
rl = t1 + arc + t2;
}
printf("%.3lf\n", rl);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int ch(double a, double b)
{
if((a > 0 && b < 0) || (a < 0 && b > 0))return 1;
else return 2;
}
int main()
{
int t;
double x1, y1, x2, y2, r, d, d1, d2, t1, t2, pl, m, rl, theta, arc;
scanf("%d", &t);
while(t--){
scanf("%lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &r);
if(x1 == x2){
if(fabs(x1) <= r && fabs(y1) >= r && fabs(y2) >= r && ch(y1, y2) == 2)pl = fabs(y1);
else pl = fabs(x1);
}
else if(y1 == y2){
if(fabs(y1) <= r && fabs(x1) >= r && fabs(x2) >= r && ch(x1, x2) == 2)pl = fabs(x1);
else pl = fabs(y1);
}
else{
m = (y2 - y1) / (x2 - x1);
pl = fabs((- m*x1 + y1) / sqrt(pow(m, 2) + 1));
}
if(pl >= r)rl = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));