欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

题解:Circle Through Three Points(三角形外心板子)

程序员文章站 2022-04-01 12:48:11
...

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) 

ut
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

RT

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
struct Point{
 double x,y;
}a,b,c;
double X,Y,R;
void Calculate_cicular(Point px1, Point px2, Point px3)
{
    double x1,y1,x2,y2,x3,y3;
    double a,b,c,g,e,f;
    x1=px1.x;
    y1=px1.y;
    x2=px2.x;
    y2=px2.y;
    x3=px3.x;
    y3=px3.y;
    e=2*(x2-x1);
    f=2*(y2-y1);
    g=x2*x2-x1*x1+y2*y2-y1*y1;
    a=2*(x3-x2);
    b=2*(y3-y2);
    c=x3*x3-x2*x2+y3*y3-y2*y2;
    X=(g*b-c*f)/(e*b-a*f);
    Y=(a*g-c*e)/(a*f-b*e);
    R=sqrt((X-x1)*(X-x1)+(Y-y1)*(Y-y1));
}
int main()
{ 
    while(cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y)
    {
     Calculate_cicular(a,b,c);
     if(X)
     {
      cout<<"(x ";
   if(X>=0) cout<<"- ";
   else cout<<"+ ";
   printf("%.3lf)^2 + ",abs(X));
  }
  else cout<<"x^2 + ";
  if(Y)
  {
   cout<<"(y ";
   if(Y>=0) cout<<"- ";
   else cout<<"+ ";
   printf("%.3lf)^2 = ",abs(Y));
  }
  else cout<<"y^2 = ";
  if(R)
  printf("%.3lf^2\n",R);
  else cout<<0<<endl;////////
  printf("x^2 + y^2 ");
  if(X)
  {
   if(X>0) printf("- %.3fx ",2*X);
   else printf("+ %.3fx ",-2*X);
  } 
  if(Y)
  {
   if(Y>0) printf("- %.3fy ",2*Y);
   else printf("+ %.3fy ",-2*Y);
  }
  double e=R*R-X*X-Y*Y;
  if(e>0) printf("- %.3f ",e);
  else if(e<0) printf("+ %.3f ",-e);
  cout<<"= 0"<<endl<<endl;
 }
    return 0;
}