poj 1329 Circle Through Three Points(求圆心+输出)

题目链接:http://poj.org/problem?id=1329

 

输出很蛋疼,要考虑系数为0,输出也不同

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

const double eps = 1e-8;
const double PI = acos(-1.0);
const double INF = 1000000000000000.000;

struct Point
{
    double x,y;
    Point(double x=0, double y=0) : x(x),y(y) { }   //构造函数
};
typedef Point Vector;

struct Circle
{
    Point c;
    double r;
    Circle() {}
    Circle(Point c,double r): c(c),r(r) {}
};
Vector operator + (Vector A , Vector B)
{
    return Vector(A.x+B.x,A.y+B.y);
}
Vector operator - (Vector A , Vector 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;
}
bool operator == (const Point& a, const Point& b)
{
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

///向量(x,y)的极角用atan2(y,x);
inline double Dot(Vector A, Vector B)
{
    return A.x*B.x + A.y*B.y;
}
inline double Length(Vector A)
{
    return sqrt(Dot(A,A));
}
inline double Angle(Vector A, Vector B)
{
    return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A, Vector B)
{
    return A.x*B.y - A.y * B.x;
}
Vector vecunit(Vector v)
{
    return v / Length(v);   //单位向量
}

Point read_point()
{
    Point A;
    scanf("%lf %lf",&A.x,&A.y);
    return A;
}
Vector Normal(Vector A)
{
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
Point GetLineIntersecion(Point P, Vector v,Point Q,Vector w)
{
    Vector u = P - Q;
    double t = Cross(w,u)/Cross(v,w);
    return P + v*t;
}

//多边形
//求面积
double PolygonArea(Point* p,int n)    //n个点
{
    double area = 0;
    for(int i=1; i<n-1; i++)
    {
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    }
    return area/2;
}

/*************************************分 割 线*****************************************/

int main()
{
    //freopen("E:\\acm\\input.txt","r",stdin);

    Point A,B,C,O;
    double R;
    while(scanf("%lf %lf %lf %lf %lf %lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y) == 6)
    {

        Point mid1 = (A+B)/2;
        Point mid2 = (B+C)/2;
        Vector v1 = Normal(A-B);
        Vector v2 = Normal(B-C);

        O = GetLineIntersecion(mid1,v1,mid2,v2);
        R = Length(O-A);

        if(dcmp(O.x)>0)          printf("(x - %.3f)^2 + ",O.x);
        else if(dcmp(O.x) == 0)  printf("x^2 + ",O.x);
        else                     printf("(x + %.3f)^2 + ",-O.x);
        if(dcmp(O.y)>0)          printf("(y - %.3f)^2 = ",O.y);
        else if(dcmp(O.y) == 0)  printf("y^2 = ",O.y);
        else                     printf("(y + %.3f)^2 = ",-O.y);
        printf("%.3f^2\n",R);

        if(dcmp(O.x)>0)          printf("x^2 + y^2 - %.3fx ",2*O.x);
        else if(dcmp(O.x) == 0)  printf("x^2 + y^2 ");
        else                     printf("x^2 + y^2 + %.3fx ",-2*O.x);

        if(dcmp(O.y)>0)          printf("- %.3fy ",2*O.y);
        else if(dcmp(O.y) < 0)   printf("+ %.3fy ",-2*O.y);

        if(dcmp(O.x*O.x+O.y*O.y-R*R) > 0)
            printf("+ %.3f = 0\n",O.x*O.x+O.y*O.y-R*R);
        else if(dcmp(O.x*O.x+O.y*O.y-R*R) == 0)
            printf("= 0\n");
        else
            printf("- %.3f = 0\n",-O.x*O.x-O.y*O.y+R*R);
        printf("\n");
    }
}
View Code

 

 

posted @ 2013-10-05 19:19  等待最好的两个人  阅读(272)  评论(0编辑  收藏  举报