POJ1329-Circle Through Three Points

Circle Through Three Points
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3262   Accepted: 1384

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

//AC代碼
/*
題意:有三個點A,B,C組成三角形,求這三角形的外接圓用兩個公式表示
三角形的外接圓圓心:任意兩條邊的垂直平分線的交點,半徑該交點到任意三個頂點的距離

另外此題有直接求外接圓的圓心和半徑的公式:http://blog.csdn.net/ecjtu_yuweiwei/article/details/38350587
我是直接模擬
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=20001;
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
typedef struct Node
{
    double x;
    double y;
    Node(const double &_x=0, const double &_y=0) : x(_x), y(_y) {}
    void input()
    {
        cin>>x>>y;
    }
    void output()
    {
        cout<<x<<" "<<y<<endl;
    }
}point;
point A,B,C;
point AB,BC;
point Center;
double k1,k2,k3,k4,R;
double b1,b2;
double xmult(point p0,point p1,point p2)
{
	return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dmult(point p0,point p1,point p2)
{
	return(p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
double Distance(point p1,point p2)// 返回兩點之間歐氏距離
{
	return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
}
double slope(point u,point v)
{
    if(sign(v.x-u.x)==0)
        return INF;
    else
        return (v.y-u.y)/(v.x-u.x);
}
int main()
{
    int n,m,i,j;
    while(cin>>A.x>>A.y)
    {
        B.input();
        C.input();
        AB.x=(A.x+B.x)/2;
        AB.y=(A.y+B.y)/2;
        BC.x=(B.x+C.x)/2;
        BC.y=(B.y+C.y)/2;
        k1=slope(A,B);
        k2=slope(B,C);
        Center.x=INF;
        Center.y=INF;
        if(k1==INF)
        {
            k3=0;
            Center.y=AB.y;
        }
        if(k2==INF)
        {
            k4=0;
            Center.y=BC.y;
        }
        if(k1==0)
        {
            k3=INF;
            Center.x=AB.x;
        }
        if(k2==0)
        {
            k4=INF;
            Center.x=BC.x;
        }
        if(Center.x!=INF&&Center.y!=INF)
        {
            //cout<<"111"<<endl;
            R=Distance(Center,A);
            char a,b,c;
            double res;
            res=pow(Center.x,2)+pow(Center.y,2)-pow(R,2);
            if(Center.x<0)
                a='+';
            else
                a='-';
            if(Center.y<0)
                b='+';
            else
                b='-';
            if(res<0)
                c='-';
            else
                c='+';
            cout<<"(x "<<a<<" ";
            cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.x)<<")^2 + (y "<<b<<" ";
            cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.y)<<")^2 = ";
            cout<<setprecision(3)<<setiosflags(ios::fixed)<<R<<"^2"<<endl;

            cout<<"x^2 + y^2 "<<a<<" "<<fabs(2*Center.x)<<"x "<<b<<" "<<fabs(2*Center.y)<<"y "<<c<<" "<<fabs(res)<<" = 0"<<endl;
        }
        else
        {
            if(Center.x==INF&&Center.y==INF)
            {
                k3=-1.0/k1;
                k4=-1.0/k2;
                //cout<<"222"<<endl;
               // AB.output();
               // BC.output();
                b1=AB.y-k3*AB.x;
                b2=BC.y-k4*BC.x;
                //cout<<k3<<" "<<k4<<" "<<b1<<" "<<b2<<endl;
                Center.x=(b2-b1)/(k3-k4);
                Center.y=k3*Center.x+b1;
                //cout<<Center.x<<" "<<Center.y<<endl;
                R=Distance(Center,A);
                //cout<<"ok:: "<<Distance(Center,B)<<" "<<Distance(Center,C)<<endl;
                char a,b,c;
                double res;
                res=pow(Center.x,2)+pow(Center.y,2)-pow(R,2);
                if(Center.x<0)
                    a='+';
                else
                    a='-';
                if(Center.y<0)
                    b='+';
                else
                    b='-';
                if(res<0)
                    c='-';
                else
                    c='+';
                cout<<"(x "<<a<<" ";
                cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.x)<<")^2 + (y "<<b<<" ";
                cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.y)<<")^2 = ";
                cout<<setprecision(3)<<setiosflags(ios::fixed)<<R<<"^2"<<endl;

                cout<<"x^2 + y^2 "<<a<<" "<<fabs(2*Center.x)<<"x "<<b<<" "<<fabs(2*Center.y)<<"y "<<c<<" "<<fabs(res)<<" = 0"<<endl;
            }
            else
            {
                if(Center.x==INF&&Center.y!=INF)
                {
                    if(k3==0)
                    {
                        k4=-1.0/k2;
                        b2=BC.y-k4*BC.x;
                        Center.x=(Center.y-b2)/k4;
                    }
                    if(k4==0)
                    {
                        k3=-1.0/k1;
                        b1=AB.y-k3*AB.x;
                        Center.x=(Center.y-b1)/k3;
                    }
                    //cout<<"222"<<endl;
                    //AB.output();
                    //BC.output();
                    //cout<<k3<<" "<<k4<<" "<<b1<<" "<<b2<<endl;
                    //Center.x=(b2-b1)/(k3-k4);

                    //cout<<Center.x<<" "<<Center.y<<endl;
                    R=Distance(Center,A);
                    //cout<<"ok:: "<<Distance(Center,B)<<" "<<Distance(Center,C)<<endl;
                    char a,b,c;
                    double res;
                    res=pow(Center.x,2)+pow(Center.y,2)-pow(R,2);
                    if(Center.x<0)
                        a='+';
                    else
                        a='-';
                    if(Center.y<0)
                        b='+';
                    else
                        b='-';
                    if(res<0)
                        c='-';
                    else
                        c='+';
                    cout<<"(x "<<a<<" ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.x)<<")^2 + (y "<<b<<" ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.y)<<")^2 = ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<R<<"^2"<<endl;

                    cout<<"x^2 + y^2 "<<a<<" "<<fabs(2*Center.x)<<"x "<<b<<" "<<fabs(2*Center.y)<<"y "<<c<<" "<<fabs(res)<<" = 0"<<endl;
                }
                else if(Center.x!=INF&&Center.y==INF)
                {
                    if(k3==INF)
                    {
                        k4=-1.0/k2;
                        b2=BC.y-k4*BC.x;
                        Center.y=k4*Center.x+b2;
                    }
                    if(k4==INF)
                    {
                        k3=-1.0/k1;
                        b1=AB.y-k3*AB.x;
                        Center.y=k3*Center.x+b1;
                    }
                    R=Distance(Center,A);
                    //cout<<"ok:: "<<Distance(Center,B)<<" "<<Distance(Center,C)<<endl;
                    char a,b,c;
                    double res;
                    res=pow(Center.x,2)+pow(Center.y,2)-pow(R,2);
                    if(Center.x<0)
                        a='+';
                    else
                        a='-';
                    if(Center.y<0)
                        b='+';
                    else
                        b='-';
                    if(res<0)
                        c='-';
                    else
                        c='+';
                    cout<<"(x "<<a<<" ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.x)<<")^2 + (y "<<b<<" ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<fabs(Center.y)<<")^2 = ";
                    cout<<setprecision(3)<<setiosflags(ios::fixed)<<R<<"^2"<<endl;

                    cout<<"x^2 + y^2 "<<a<<" "<<fabs(2*Center.x)<<"x "<<b<<" "<<fabs(2*Center.y)<<"y "<<c<<" "<<fabs(res)<<" = 0"<<endl;
                }
            }
        }
        cout<<endl;
    }
    return 0;
}

發佈了243 篇原創文章 · 獲贊 12 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章