[UVA][外心座標] 190 - Circle Through Three Points
Circle Through Three Points
Circle Through Three Points |
Your team is to write a program that, given the Cartesian coordinates of three points on a plane, will find the equation of the circle through them all. The three points will not be on a straight line.
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 theformat 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 printedwith three digits after the decimal point. Plus and minus signs inthe equations should be changed as needed to avoid multiple signsbefore a number. Plus, minus, and equal signs must be separated fromthe adjacent characters by a single space on each side. No other spacesare to appear in the equations. Print a single blank line after eachequation pair.
Sample input
7.0 -5.0 -1.0 1.0 0.0 -6.01.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
用了一堆公式, 算中垂線的那條直線, 求兩元一次方程式的公式
#include <stdio.h>
#include <math.h>
struct Point {
double x, y;
};
Point circle(Point &a, Point &b, Point &c) {
static Point ab, ac, o;
static double a1, b1, c1, a2, b2, c2, D, D1, D2;
ab.x = (a.x+b.x)/2, ab.y = (a.y+b.y)/2;
ac.x = (a.x+c.x)/2, ac.y = (a.y+c.y)/2;
a1 = a.x-b.x, b1 = a.y-b.y;
c1 = a1*ab.x + b1*ab.y;
a2 = a.x-c.x, b2 = a.y-c.y;
c2 = a2*ac.x + b2*ac.y;
D = a1*b2-a2*b1;
D1 = c1*b2-c2*b1;
D2 = a1*c2-a2*c1;
o.x = D1/D;
o.y = D2/D;
return o;
}
int main() {
Point a, b, c;
while(scanf("%lf %lf", &a.x, &a.y) == 2) {
scanf("%lf %lf", &b.x, &b.y);
scanf("%lf %lf", &c.x, &c.y);
Point o = circle(a, b, c);
double r2 = (a.x-o.x)*(a.x-o.x)+(a.y-o.y)*(a.y-o.y);
char c1, c2, c3;
if(o.x >= 0) c1 = '-';
else c1 = '+';
if(o.y >= 0) c2 = '-';
else c2 = '+';
if(r2-o.x*o.x-o.y*o.y >= 0)
c3 = '-';
else c3 = '+';
printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n", c1, fabs(o.x), c2, fabs(o.y), sqrt(r2));
printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n", c1, fabs(2*o.x), c2, fabs(2*o.y), c3, fabs(r2-o.x*o.x-o.y*o.y));
puts("");
}
return 0;
}