题目链接:http://poj.org/problem?id=1329
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x, y = _y;
}
bool operator == (Point b) const{
return sgn(x - b.x) == && sgn(y - b.y) == ;
}
bool operator < (Point b)const{
return sgn(x - b.x) == ? sgn(y - b.y < ) : x < b.x;
}
Point operator - (const Point &b)const{
return Point(x - b.x, y - b.y);
}
//叉积
double operator ^(const Point &b){
return x * b.y - y * b.x;
}
//点积
double operator *(const Point &b){
return x * b.x + y * b.y;
}
double len(){
return hypot(x, y);
}
double len2(){
return x * x + y * y;
}
double distant(Point p){
return hypot(x - p.x, y - p.y);
}
Point operator + (const Point &b)const{
return Point (x + b.x, y + b.y);
}
Point operator * (const double &k)const{
return Point(x * k, y * k);
}
Point operator / (const double &k)const{
return Point(x / k, y / k);
}
//逆时针旋转90度
Point rotleft(){
return Point(- y, x);
}
//顺时针旋转90度
Point rotright(){
return Point(y, -x);
}
};
struct Line{
Point s, e;
Line(){}
Line(Point _s, Point _e){s = _s, e = _e;}
Point crosspoint(Line v){
double a1 = (v.e - v.s)^(s - v.s);
double a2 = (v.e - v.s)^(e - v.s);
return Point((s.x*a2 - e.x*a1)/(a2 - a1),(s.y*a2 - e.y*a1)/(a2 - a1));
}
};
struct circle{
Point p;
double r;
circle(){}
circle(Point _p, double _r){
p = _p, r = _r;
}
circle(double x, double y, double _r){
p = Point(x, y);
r = _r;
}
//三角形外接圆
circle(Point a, Point b, Point c){
Line u = Line( (a + b) / ,((a + b) / ) + ((b - a).rotleft()) );
Line v = Line( (b + c) / ,((b + c) / ) + ((c - b).rotleft()) );
p = u.crosspoint(v);
r = p.distant(a);
}
};
void print(double num) {
if (sgn(num) < ) {
printf("- "); num = -num;
}
else printf("+ ");
printf("%.3f", num);
}
int main()
{
double x1, x2, x3, y1, y2, y3;
while(~scanf("%lf%lf%lf%lf%lf%lf",&x1, &y1, &x2, &y2, &x3, &y3))
{
circle a = circle(Point(x1, y1), Point(x2, y2), Point(x3, y3));
double c = a.r * a.r - (a.p.x * a.p.x + a.p.y * a.p.y);
if (sgn(a.p.x) == )printf("x^2");
else { printf("(x "); print(-a.p.x); printf(")^2"); }
printf(" + ");
if (sgn(a.p.y) == )printf("y^2");
else { printf("(y "); print(-a.p.y); printf(")^2"); }
printf(" = %.3f^2\n", a.r);
printf("x^2 + y^2 ");
print(-a.p.x * 2.0);
printf("x ");
print(-a.p.y * 2.0);
printf("y ");
print(-c);
printf(" = 0\n");
printf("\n");
}
return ;
}