190 - Circle Through Three Points
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=126
The solution is to be printed as an equation of the form
and an equation of the form
Each line of input to your program will contain the x and y coordinates of three points, in the order , , , , , . These coordinates will be real numbers separated from each other by one or more spaces.
Your program must print the required equations on two lines using the format given in the sample below. Your computed values for h, k, r, c, d, and e in Equations 1 and 2 above are to be printed with three digits after the decimal point. Plus and minus signs in the equations should be changed as needed to avoid multiple signs before a number. Plus, minus, and equal signs must be separated from the adjacent characters by a single space on each side. No other spaces are to appear in the equations. Print a single blank line after each equation pair.
Sample input
7.0 -5.0 -1.0 1.0 0.0 -6.0 1.0 7.0 8.0 6.0 7.0 -2.0
Sample output
(x - 3.000)^2 + (y + 2.000)^2 = 5.000^2 x^2 + y^2 - 6.000x + 4.000y - 12.000 = 0 (x - 3.921)^2 + (y - 2.447)^2 = 5.409^2 x^2 + y^2 - 7.842x - 4.895y - 7.895 = 0
设圆心为O(x,y),由点A和B到O距离相等可得一线性方程,由点A和C到O距离相等可得另一线性方程,解之得圆心。
完整代码:
/*0.019s*/
#include<cstdio>
#include<cmath>
void calc(double x0, double y0, double x1, double y1, double x2, double y2)
{
double A1 = x1 - x0, B1 = y1 - y0, C1 = x1 * x1 - x0 * x0 + y1 * y1 - y0 * y0;
double A2 = x2 - x0, B2 = y2 - y0, C2 = x2 * x2 - x0 * x0 + y2 * y2 - y0 * y0;
double c = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
double d = (A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1);///解方程
double e = -x1 * x1 - y1 * y1 - x1 * c - y1 * d;
double h = c / -2.0, k = d / -2.0; ///圆心坐标
double r = hypot(x1 - h, y1 - k);
printf("(x ");
if (h > 0) printf("- %.3lf)^2 + (y ", h);
else printf("+ %.3lf)^2 + (y ", -h);
if (k > 0) printf("- %.3lf)^2 = %.3lf^2\n", k, r);
else printf("+ %.3lf)^2 = %.3lf^2\n", -k, r);
printf("x^2 + y^2 ");
if (c > 0) printf("+ %.3lfx ", c);
else printf("- %.3lfx ", -c);
if (d > 0) printf("+ %.3lfy ", d);
else printf("- %.3lfy ", -d);
if (e > 0) printf("+ %.3lf = 0\n\n", e);
else printf("- %.3lf = 0\n\n", -e);
}
int main()
{
double x0, y0, x1, y1, x2, y2;
while (~scanf("%lf%lf%lf%lf%lf%lf", &x0, &y0, &x1, &y1, &x2, &y2))
calc(x0, y0, x1, y1, x2, y2);
return 0;
}