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 Source
Southern California 1989,UVA 190
|
[Submit] [Go Back] [Status] [Discuss]
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
typedef double PointType;
const int maxn=1000+100;
struct point{
PointType x,y;
point(double x=0,double y=0):x(x),y(y) {}
};
typedef point Vector ;
Vector operator + (Vector A , Vector B){
return Vector(A.x + B.x , A.y + B.y);
}
Vector operator - (point A , point B){
return Vector(A.x - B.x , A.y - B.y);
}
Vector operator * (Vector A , double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A , double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const point& a, const point& b){
return a.x<b.x||(a.x==b.x && a.y<b.y);
}
int dcmp(double x){
if(fabs(x) < eps) return 0;else return x<0 ?-1:1;
}
double Dot(Vector A , Vector B){
return A.x*B.x+A.y*B.y;
}
double Length(Vector A){
return sqrt(Dot(A,A));
}
double Cross(Vector A , Vector B){
return A.x*B.y - A.y*B.x;
}
char sign1(double x)
{
return x>0 ? '-':'+';
}
double sign2(double x)
{
return x>0 ? x:-x;
}
char sign3(double x)
{
return x>0 ? '+':'-';
}
int main()
{
point a,b,c;
while(scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y)!=EOF)
{
double s=0.5*fabs( (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y) );
double l1=Length(a-b);
double l2=Length(b-c);
double l3=Length(a-c);
double r=l1*l2*l3/(4*s);
double c1=(a.x*a.x+a.y*a.y-b.x*b.x-b.y*b.y)/2;
double c2=(a.x*a.x+a.y*a.y-c.x*c.x-c.y*c.y)/2;
point centre;
centre.x=(c1*(a.y-c.y)-c2*(a.y-b.y))/((a.x-b.x)*(a.y-c.y)-(a.x-c.x)*(a.y-b.y));
centre.y=(c1*(a.x-c.x)-c2*(a.x-b.x))/((a.y-b.y)*(a.x-c.x)-(a.y-c.y)*(a.x-b.x));
printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n",sign1(centre.x),sign2(centre.x),sign1(centre.y),sign2(centre.y),r);
double e=centre.x*centre.x+centre.y*centre.y-r*r;
double c=-2.0*centre.x;
double d=-2.0*centre.y;
printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n\n",sign3(c),sign2(c),sign3(d),sign2(d),sign3(e),sign2(e));
}
return 0;
}