Language:
Circle Through Three Points
Description
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 (x - h)^2 + (y - k)^2 = r^2 (1) and an equation of the form x^2 + y^2 + cx + dy - e = 0 (2) Input
Each line of input to your program will contain the x and y coordinates of three points, in the order Ax, Ay, Bx, By, Cx, Cy. These coordinates will be real numbers separated from each other by one or more spaces.
Output
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 |
题意:求三个点构成的圆并写出该圆的标准方程和一般方程输出格式真恶心
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
using namespace std;
struct point{
double x,y;
}A[3];
int sgn(double n){
if(fabs(n)<eps)return 0;
else if(n<0)return -1;
else return 1;
}
double dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void getcircle(point p1,point p2,point p3,point &circle){
double Bx=p2.x-p1.x,By=p2.y-p1.y;
double Cx=p3.x-p1.x,Cy=p3.y-p1.y;
double D=2.0*(Bx*Cy-By*Cx);
circle.x=(Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D+p1.x;
circle.y=(Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D+p1.y;
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&A[0].x,&A[0].y,&A[1].x,&A[1].y,&A[2].x,&A[2].y)!=EOF){
point circle;
getcircle(A[0],A[1],A[2],circle);
double c,d,e;c=-2.0*circle.x;d=-2.0*circle.y;e=circle.x*circle.x+circle.y*circle.y-dist(circle,A[0])*dist(circle,A[0]);
if(sgn(circle.x)>=0&&sgn(circle.y)>=0){
printf("(x - %.3lf)^2 + (y - %.3lf)^2 = %.3lf^2\n",circle.x,circle.y,dist(circle,A[0]));
}
else if(sgn(circle.x)>=0&&sgn(circle.y)<0){
printf("(x - %.3lf)^2 + (y + %.3lf)^2 = %.3lf^2\n",circle.x,fabs(circle.y),dist(circle,A[0]));
}
else if(sgn(circle.x)<0&&sgn(circle.y)>=0){
printf("(x + %.3lf)^2 + (y - %.3lf)^2 = %.3lf^2\n",fabs(circle.x),circle.y,dist(circle,A[0]));
}
else if(sgn(circle.x)<0&&sgn(circle.y)<0){
printf("(x + %.3lf)^2 + (y + %.3lf)^2 = %.3lf^2\n",fabs(circle.x),fabs(circle.y),dist(circle,A[0]));
}
if(sgn(c)>=0&&sgn(d)>=0&&sgn(e)>0){
printf("x^2 + y^2 + %.3lfx + %.3lfy + %.3lf = 0\n\n",c,d,e);
}
else if(sgn(c)>=0&&sgn(d)>=0&&sgn(e)<=0){
printf("x^2 + y^2 + %.3lfx + %.3lfy - %.3lf = 0\n\n",c,d,fabs(e));
}
else if(sgn(c>=0)&&sgn(d)<0&&sgn(e)<=0){
printf("x^2 + y^2 + %.3lfx - %.3lfy - %.3lf = 0\n\n",c,fabs(d),fabs(e));
}
else if(sgn(c)>=0&&sgn(d)<0&&sgn(e)>0){
printf("x^2 + y^2 + %.3lfx - %.3lfy + %.3lf = 0\n\n",c,fabs(d),e);
}
else if(sgn(c)<0&&sgn(d)>=0&&sgn(e)>0){
printf("x^2 + y^2 - %.3lfx + %.3lfy + %.3lf = 0\n\n",fabs(c),d,e);
}
else if(sgn(c)<0&&sgn(d)>=0&&sgn(e)<=0){
printf("x^2 + y^2 - %.3lfx + %.3lfy - %.3lf = 0\n\n",fabs(c),d,fabs(e));
}
else if(sgn(c)<0&&sgn(d)<0&&sgn(e)>0){
printf("x^2 + y^2 - %.3lfx - %.3lfy + %.3lf = 0\n\n",fabs(c),fabs(d),e);
}
else if(sgn(c)<0&&sgn(d)<0&&sgn(e)<=0){
printf("x^2 + y^2 - %.3lfx - %.3lfy - %.3lf = 0\n\n",fabs(c),fabs(d),fabs(e));
}
}
return 0;
}